Hi,
This is my first script:
bl_info = {"name": "Snap Origin Here",
"category": "Object", }
import bpy
class SnapOrigin(bpy.types.Operator):
bl_idname = "object.snap_origin"
bl_label = "Snap Origin Here"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
current_area = bpy.context.area.type
bpy.context.area.type = "VIEW_3D"
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.ops.object.mode_set(mode="EDIT")
bpy.context.area.type = current_area
return {'FINISHED'}
addon_keymaps = []
def register():
bpy.utils.register_class(SnapOrigin)
# handle the keymap
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(SnapOrigin.bl_idname, 'BUTTON4MOUSE', 'PRESS')
addon_keymaps.append(km)
def unregister():
bpy.utils.unregister_class(SnapOrigin)
# handle the keymap
wm = bpy.context.window_manager
for km in addon_keymaps:
wm.keyconfigs.addon.keymaps.remove(km)
# clear the list
addon_keymaps.clear()
# clear the list
del addon_keymaps[:]
if __name__ == "__main__":
register()
It snap the 3D Cursor on the selected vertex and Move Origin Point.
If I call it whit space > search > Snap Origin Here, it work, but if instead call it with BUTTON4MOUSE it work only in object mode and not in edit mode
What is my mistake?