2
$\begingroup$

I'm using a script to enter edit mode and select a component selection based on a hotkey press.

it works fine when I paste the code in the text editor and run it from there, but on restart the addon looks loaded in the addon section yet the command doesn't seem to be available and the hotkeys don't work.

I've also tried pasting the file in the startup directory with no luck.

how can I make this available everytime I run blender without manually running the code?

 bl_info = {
    "name": "Edit mode with face select", "author": "G",
#    "version": (1, 0), "blender": (2, 75, 0),
    "location": "blender", "category": "Object",
    "description": "Enter edit mode and set selection type .",
    }

import bpy

class EditSelectOperator(bpy.types.Operator):
    bl_idname = 'object.edit_face_select'
    bl_label = 'Enter edit mode and set face selection.'

    def execute(self, context):
        if context.object.mode != 'EDIT':
            # if we aren't in edit mode goto edit and set face select
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
            bpy.ops.mesh.select_mode(type='FACE')

        return {'FINISHED'}
 
class EditSelectOperator2(bpy.types.Operator):
    bl_idname = 'object.edit_vert_select'
    bl_label = 'Enter edit mode and set vertex selection.'

    def execute(self, context):
        if context.object.mode != 'EDIT':
            # if we aren't in edit mode goto edit and set face select
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
            bpy.ops.mesh.select_mode(type='VERT')

        return {'FINISHED'}
 
class EditSelectOperator3(bpy.types.Operator):
    bl_idname = 'object.edit_edge_select'
    bl_label = 'Enter edit mode and set edge selection.'

    def execute(self, context):
        if context.object.mode != 'EDIT':
            # if we aren't in edit mode goto edit and set face select
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
            bpy.ops.mesh.select_mode(type='EDGE')

        return {'FINISHED'}
    
     

def register():
    bpy.utils.register_class(EditSelectOperator)
    bpy.utils.register_class(EditSelectOperator2)
    bpy.utils.register_class(EditSelectOperator3)

def unregister():
    bpy.utils.unregister_class(EditSelectOperator)
    bpy.utils.unregister_class(EditSelectOperator2)
    bpy.utils.unregister_class(EditSelectOperator3)
    

if __name__ == "__main__":
    register()

also on a side note is there a better way to achieve entering edit mode and component selection at the same time?

$\endgroup$
1
  • $\begingroup$ Just make it in addon. Install it and save preferences. $\endgroup$
    – X Y
    Commented Apr 11, 2022 at 1:25

1 Answer 1

2
$\begingroup$
bl_info = {
    "name" : "Addon name",
    "author" : "Someone",
    "version" : (1, 0),
    "blender" : (2, 92, 0),
    "location" : "View3d > Tool",
    "warning" : "",
    "description" : "",
    "wiki_url" : "",
    "category" : "3D View",
}


import bpy

class EditSelectOperator(bpy.types.Operator):
    bl_idname = 'object.edit_face_select'
    bl_label = 'Enter edit mode and set face selection.'

    def execute(self, context):
        if context.object.mode != 'EDIT':
            # if we aren't in edit mode goto edit and set face select
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
            bpy.ops.mesh.select_mode(type='FACE')

        return {'FINISHED'}

class EditSelectOperator2(bpy.types.Operator):
    bl_idname = 'object.edit_vert_select'
    bl_label = 'Enter edit mode and set vertex selection.'

    def execute(self, context):
        if context.object.mode != 'EDIT':
            # if we aren't in edit mode goto edit and set face select
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
            bpy.ops.mesh.select_mode(type='VERT')

        return {'FINISHED'}

class EditSelectOperator3(bpy.types.Operator):
    bl_idname = 'object.edit_edge_select'
    bl_label = 'Enter edit mode and set edge selection.'

    def execute(self, context):
        if context.object.mode != 'EDIT':
            # if we aren't in edit mode goto edit and set face select
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
            bpy.ops.mesh.select_mode(type='EDGE')

        return {'FINISHED'}

addon_keymaps = []

def register():
    bpy.utils.register_class(EditSelectOperator)
    bpy.utils.register_class(EditSelectOperator2)
    bpy.utils.register_class(EditSelectOperator3)
    # Add the hotkey
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')

        kmi = km.keymap_items.new(EditSelectOperator.bl_idname, type='F10', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))
        kmi = km.keymap_items.new(EditSelectOperator2.bl_idname, type='F11', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))
        kmi = km.keymap_items.new(EditSelectOperator3.bl_idname, type='F12', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))


def unregister():
    bpy.utils.unregister_class(EditSelectOperator)
    bpy.utils.unregister_class(EditSelectOperator2)
    bpy.utils.unregister_class(EditSelectOperator3)
    
    # Remove the hotkey
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()


if __name__ == "__main__":
    register()

Choose your py file to install
Choose zip file for multiple py file

enter image description here

If you don't know how to install, just go to some addon location see them how to do.

enter image description here

$\endgroup$
4
  • $\begingroup$ thank you, having the shortcuts in the script is an improvement already as I was setting them manually after. however I still face the same issue. the addon is installed like you suggest, but when I close and reopen blender the commands are not available. when I go to Keymap editor, the shortcuts are not there, they get added only after I manually run it from the text editor. (?) also it would appear that shortcuts don't get removed when I untick the addon from the list $\endgroup$
    – Max
    Commented Apr 11, 2022 at 11:02
  • $\begingroup$ You need install a file that inclue all of your code. Not need run the script manually every time. $\endgroup$
    – X Y
    Commented Apr 11, 2022 at 12:25
  • $\begingroup$ @X Y thanks I have been saving my script in .py and installing it as described from the begginning so I think this part is correct. I realise its not supposed to do this but its the behaviour I'm getting for some reason..not sure if there's anything else I could try. $\endgroup$
    – Max
    Commented Apr 11, 2022 at 12:32
  • $\begingroup$ ok I figured it out. the issue was happening because I was commenting out the "blender " version in the info section. I typed in the correct version and seems to be working as expected. thanks a lot! $\endgroup$
    – Max
    Commented Apr 11, 2022 at 13:16

You must log in to answer this question.

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