3
$\begingroup$

I want to set the 3d cursor location using bpy.ops.view3d.cursor3d() operator before calling my own operator with a shortcut.
This is what I have tried-

class Add_obj(Operator):
    bl_idname = 'ob.add_object'
    bl_label = 'Add object'
    bl_options = {'REGISTER','UNDO' }

    def execute(context,self):
        bpy.ops.view3d.cursor3d()  #place 3d cursor on mouse click just like pressing SHIFT+RIGHT click
        bpy.ops.mesh.primitive_cube_add()  #add object
        return {'FINISHED'}

addon_keymaps = []


def register():
    register_class(Add_obj)
    wm = bpy.context.window_manager
# Adding a Shortcut for the operator
    kc = wm.keyconfigs.addon
    if kc:
        km = kc.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new("ob.add_object", type= 'RIGHTMOUSE', value='PRESS',
        shift=True,alt=True)
        addon_keymaps.append((km,kmi))

def unregister():
    for km,kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()
    unregister_class(Add_obj)

if __name__ == '__main__':
    register()

But when I press the shortcut SHIFT+ALT+RIGHT click it gives this error and adds a cube-
ERROR (wm.operator): C:\b\buildbot-worker-windows\windows_290\blender.git\source\blender\windowmanager\intern\wm_event_system.c:1317 wm_operator_invoke: invalid operator call 'VIEW3D_OT_cursor3d

$\endgroup$
2
  • $\begingroup$ Possibly easier to make this a modal operator, capture some mouse event, set cursor, and add the cube. See the modal examples in Text Editor > Templates > Python > Modal Operator * Consider in future adding imports etc to make a minimal working script. $\endgroup$
    – batFINGER
    Commented Aug 23, 2020 at 18:25
  • $\begingroup$ @batFINGER Thank you! I managed to achieve what I wanted by following the raycast modal example from templates. $\endgroup$
    – Me BMan
    Commented Aug 27, 2020 at 16:33

1 Answer 1

0
$\begingroup$

Just had the same problem. Add an 'INVOKE_DEFAULT' to the 3d cursor operator:

    def execute(context,self):
        bpy.ops.view3d.cursor3d('INVOKE_DEFAULT') # put it at the start, before any arguments
        bpy.ops.mesh.primitive_cube_add()
        return {'FINISHED'}
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .