I’m following an outdated guide trying to write my first plugin in blender 2.8 , I tried the following code and it doesn’t work for reasons I do not understand , can anyone shed light on how to fix it ? or at least where I can find a proper 2.8 tutorial that would tell me how to properly do the hotkeys thing?
Please help me , you are my only hope…
def Zplus(context):
bpy.context.object.location[2] += 1
class SwitchToCursorMove(bpy.types.Operator):
"""Test of crusor move"""
bl_idname = "OBJECT_OT_SwitchToCursorMove"
bl_label = "Switch"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Zplus(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SwitchToCursorMove)
keymapname = 'Edit mode'
wm = bpy.context.window_manager
kc = wm.keyconfigs
if keymapname in kc.addon.keymaps:
km = kc.addon.keymaps[keymapname]
else:
km = kc.addon.keymaps.new(name = keymapname, space_type = 'VIEW_3D')
ki = km.keymap_items.new(SwitchToCursorMove.bl_idname, 'NUMPAD_ASTERIX', 'PRESS', ctrl = True, head = True)
def unregister():
if km and ki:
km.keymap_items.remove(ki)
bpy.types.VIEW3D_MT_object.remove(menu_func)
bpy.utils.unregister_module(__name__)
bpy.utils.unregister_class(SwitchToCursorMove)
if __name__ == "__main__":
register()
I’ve added some in-line comments with the corrected parts.
import bpy
def Zplus(context):
context.object.location[2] += 1 # < you're already passing 'context' to this function, so it don't need bpy.context
# OBJECT_OT_switch_to_cursor_move should be the
# class name, but blender doesn't care either way.
class SwitchToCursorMove(bpy.types.Operator):
"""Test of crusor move"""
bl_idname = "object.switch_to_cursor_move" # < needs to be something.something
bl_label = "Switch"
@classmethod
def poll(cls, context):
# 'is not None' is redundant for poll.
# it automatically evaluates to True if it isn't None (False)
return context.active_object
def execute(self, context):
Zplus(context)
return {'FINISHED'}
# add keys to this list. this helps blender keep track
# of them until the addon at some point is unregistered.
addon_keymaps = []
def register():
bpy.utils.register_class(SwitchToCursorMove)
keymapname = 'Mesh' # for edit-mode use 'Mesh'
wm = bpy.context.window_manager
kc = wm.keyconfigs
km = kc.addon.keymaps.get(keymapname) # < simpler to assign like this since km functions like dictionaries.
if not km:
km = kc.addon.keymaps.new(name=keymapname, space_type='VIEW_3D')
# keymap items are by convention shortened to kmi
kmi = km.keymap_items.new(SwitchToCursorMove.bl_idname, 'NUMPAD_ASTERIX', 'PRESS', ctrl = True, head = True)
addon_keymaps.append((km, kmi)) # < km, kmi are added as a tuple of two to the list. note the double parentheses.
def unregister():
bpy.types.VIEW3D_MT_object.remove(menu_func)
bpy.utils.unregister_module(__name__)
bpy.utils.unregister_class(SwitchToCursorMove)
# now we can conveniently use a single for loop to access them both.
for km, kmi in addon_keymaps:
km.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
register()