1
$\begingroup$

I would like to create an operator that would trigger a pie menu containing the "pivot center modes" in it (median point, individual origins, 3d cursor, ...).

Now I could get started with the code below (from that topic: bpy: how do I list the "pivot center for rotation/scaling" items?

import bpy

def piebut(self, context):
    layout = self.layout
    op = layout.operator("wm.context_pie_enum")
    op.data_path = "space_data.pivot_point"

def register():
    bpy.types.VIEW3D_HT_header.prepend(piebut)

def unregister():
    bpy.types.VIEW3D_HT_header.remove(piebut)


if __name__ == "__main__":
    register()

The code above will add a button to the 3dView header, and when you press that button, it will display a pie menu with the "pivot center modes" items in it.

However, I would like to convert this to an operator, so that I can ultimately press a shortcut key to open the pie menu directly (without having to click a button).

Now I've tried this without luck:

import bpy


class LING_OT_PIEMENU_PivotCenterModes(bpy.types.Operator):
    """A pie menu to select the pivot center for rotation and scaling"""
    bl_idname = "view3d.ling_piemenu_pivot_center_modes"
    bl_label = "Pivot center for rotation/scaling"

    def execute(self, context):
        bpy.ops.wm.context_pie_enum("space_data.pivot_point" )
        return {'FINISHED'}


# just for quick testing inside the blend file
bpy.utils.register_class(LING_OT_PIEMENU_PivotCenterModes)

I've changed those lines:

layout = self.layout
op = layout.operator("wm.context_pie_enum")

to this line:

bpy.ops.wm.context_pie_enum("space_data.pivot_point" )

because layout = self.layout was returning a None type, so I thought the layout property was not available from the operator class, and so I tried to call the context_pie_enum method in a different manner, but now I've got this error:

TypeError: Calling operator "bpy.ops.wm.context_pie_enum" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN')
$\endgroup$
2
$\begingroup$

It already is an operator.

There is no need to create another operator to call an operator, so it can be given a shortcut.

Can add a keymap to it directly. The field to data_path ignore if appears disabled, select and type in the required RNA path from context.

enter image description here shortcut set to ⇧ Shiftz (disabled the toggle render shading)

or created via code (example)

import bpy

wm = bpy.context.window_manager
kc = wm.keyconfigs.user

km = kc.keymaps['3D View']
kmi = km.keymap_items.new('wm.context_pie_enum', 'Z', 'PRESS', shift=True)
kmi.properties.data_path = "space_data.pivot_point"

Alternatively it can be added as an expanding pie menu item in your original question code via

op = pie.operator("wm.context_pie_enum")
op.data_path = "space_data.pivot_point"

To call an operator from code, all operator properties must be passed as keyword arguments

bpy.ops.wm.context_pie_enum(data_path="space_data.pivot_point")  

The first argument of an operator call can be a string if it is one of the operator context enums, eg 'INVOKE_DEFAULT'. See error message in question for full list. Another type of argument is a dictionary of context override members.

bpy.ops.wm.context_pie_enum('INVOKE_DEFAULT', data_path="space_data.pivot_point") 
c = {"area": context.screen.areas[4]} # will need more than just area.
bpy.ops.wm.context_pie_enum(c, data_path="space_data.pivot_point")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.