I downloaded Blender 2.8 last week and was looking into adding my own scripts to custom keyboard hotkeys, but there are gaps in my understanding that the
Blender documentation, or
these conversations I found, isn't explaining well enough to a dumbass like me.
If I use these three code lines, I can call the subobject mode of an object like I want.
- <div>import bpy</div><div>
- bpy.ops.object.editmode_toggle()<br></div><div>bpy.ops.mesh.select_mode(type="EDGE")</div>
So now I want to be able to put this into an addon package of scripts, or at least as an addon, so I can choose in Preferences>Keymap a keyboard shortcut for this command. Thing is, apparently the syntax doesnt work like this if I want to install these code lines, because I get compiler errors. I tried modifying them, but I still can't find the solution after several tries and hours of searching. Can someone tell me what am I missing?
- <div>bl_info = {<br></div><div> "name": "Work Macro",
- "category": "Object",
- }
-
- import bpy
-
-
- class WorkMacro(bpy.types.Operator):
- """Work Macro"""
- bl_idname = "object.work_macro"
- bl_label = "Work Macro"
- bl_options = {'REGISTER', 'UNDO'}
-
- def execute(self, context):
-
- for o in sel:
- obj = bpy.data.objects.get(o)
- if obj: obj.select_set(True)
- bpy.ops.mesh.select_mode(type="EDGE")
-
-
- return {'FINISHED'}
-
-
- # store keymaps here to access after registration
- addon_keymaps = []
-
- def register():
- bpy.utils.register_class(WorkMacro)
-
- # 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(WorkMacro.bl_idname, 'NUMPAD_0', 'PRESS', ctrl=False, shift=False)
- addon_keymaps.append(km)
-
-
- def unregister():
- bpy.utils.unregister_class(WorkMacro)
-
- # handle the keymap
- wm = bpy.context.window_manager
- for km in addon_keymaps:
- wm.keyconfigs.addon.keymaps.remove(km)
- # clear the list
- del addon_keymaps[:]
-
-
-
- if __name__ == "__main__":
- register()
- </div>
Replies
The indentation is wrong, the "def execute(...)" starts one indent level, so everything below it, up to the "return {'FINISHED'}", should have the same indentation. The "return" line is correct, the rest isn't.</code><br></li><li><code> for o in sel:
obj = bpy.data.objects.get(o)
if obj: obj.select_set(True)
bpy.ops.mesh.select_mode(type="EDGE")
</code><br></li><li><code>
return {'FINISHED'}
</code><pre><li><code> def execute(self, context):- def execute(self, context):<br> for o in sel:<br> obj = bpy.data.objects.get(o)<br> if obj:<br> obj.select_set(True)<br> bpy.ops.mesh.select_mode(type="EDGE")<br> return {'FINISHED'}
Python doesn't use curly brackets, like C++ or Javascript do, to know when a block scope starts/ends, it only relies on indentation.