0
$\begingroup$

i have this simple function in the Blender text editor :

import bpy
bpy.ops.object.select_pattern(pattern="Cube?")
bpy.ops.object.select_pattern(pattern="Sphere?")

I just simply want to run this function using a shortcut. How can i bind a shortcut to it ?

Thanks.

$\endgroup$
4
  • $\begingroup$ What's the goal? Just running both operators using one shortcut? Or do you actually want to being able to select certain objects by their name like "Cube" and "Sphere"? Related: Create keyboard shortcut for an operator using python? $\endgroup$
    – brockmann
    Commented Nov 9, 2020 at 11:43
  • $\begingroup$ This Run text block addon might help you. $\endgroup$
    – HikariTW
    Commented Nov 9, 2020 at 14:25
  • $\begingroup$ What is the intention of the script? If you wish to select all objects with name starting with "Cube" or "Sphere", but not "Cube" and "Sphere" consider using pattern="Cube?*" and using extend=True to extend the selection. $\endgroup$
    – batFINGER
    Commented Nov 9, 2020 at 14:27
  • $\begingroup$ I asked that too (3 hours ago) and got no answer still @batFINGER $\endgroup$
    – brockmann
    Commented Nov 9, 2020 at 14:33

2 Answers 2

3
$\begingroup$

You would have to implement a custom Operator and add your code to its execute method:

class OBJECT_OT_CustomOp(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        
        # Your code here ...
        bpy.ops.object.select_pattern(pattern="Cube?")
        bpy.ops.object.select_pattern(pattern="Sphere?")
        
        return {'FINISHED'}

shortcut.py based on Create keyboard shortcut for an operator using python?

import bpy

bl_info = {
    "name": "Your Addon Name",
    "author": "Author Name",
    "version": (0, 1),
    "blender" : (2, 80, 0),
    "location": "",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": ""
}


class OBJECT_OT_CustomOp(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        
        # Your code here ...
        bpy.ops.object.select_pattern(pattern="Cube?")
        bpy.ops.object.select_pattern(pattern="Sphere?")
        
        return {'FINISHED'}


addon_keymaps = []

def register():
    bpy.utils.register_class(OBJECT_OT_CustomOp)
    
    # 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(OBJECT_OT_CustomOp.bl_idname, type='W', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))


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


if __name__ == "__main__":
    register()
$\endgroup$
4
  • $\begingroup$ About that shortcut link above: Do i have to add keymap/shortcut inside these python codes ? How about adding keymap in preference and attach the command there ? $\endgroup$
    – andio
    Commented Nov 9, 2020 at 14:10
  • $\begingroup$ You can do both. Either assign it directly (Ctrl+W in the example) and change it later on in the keymap @andio Does that answer your question? $\endgroup$
    – brockmann
    Commented Nov 9, 2020 at 14:12
  • $\begingroup$ Thanks, I'm trying to find out how to install this code to Blender to make it loaded everytime i open Blender. $\endgroup$
    – andio
    Commented Nov 9, 2020 at 14:18
  • $\begingroup$ great it works. $\endgroup$
    – andio
    Commented Nov 9, 2020 at 14:53
1
$\begingroup$

Assuming you're using >v2.8, this should work. Very basic and there might be a better way, (almost certainly!!) but it works. After you run this, a new menu item should show up in your "Select" menu. Right click this item and assign a shortcut as normal.

## Read the comments for a very basic explanation of what the various parts do

    bl_info = { ## stuff so Blender can find your code
    "name": "My Addon",
    "author": "Your Name",
    "version": (0, 1),
    "blender": (2, 90, 0),
    "location": "View3D > Select",
    "description": "Your addon descriptioin",
    "warning": "",
    "wiki_url": "",
    "category": "3D View",
    }

import bpy
from bpy.types import Operator

class MYADDON_OT_My_Class(Operator): ## stuff to be able to make a menu item
    bl_idname = "object.my_class"
    bl_label = "Test"
    bl_description = "My Description"
    bl_space_type = "VIEW_3D"
    bl_region_type = 'UI'
        
    def invoke(self, context, event):
        ## the main event, this will be ran when your button is pressed
        bpy.ops.object.select_pattern(pattern="Cube?")
        bpy.ops.object.select_pattern(pattern="Sphere?")
        
        return {'FINISHED'}


def menu_func(self, context): ## creating the menu item
    self.layout.operator(MYADDON_OT_My_Class.bl_idname, icon='MESH_CUBE')
    
def register(): ## register the functions so Blender can use them
    bpy.utils.register_class(MYADDON_OT_My_Class)
    bpy.types.VIEW3D_MT_select_object.append(menu_func)
    
def unregister(): ## unregister when the addon is removed later
    bpy.utils.unregister_class(MYADDON_OT_My_Class)
    bpy.types.VIEW3D_MT_select_object.remove(menu_func)
    
if __name__ == "__main__":
    register()
$\endgroup$
6
  • $\begingroup$ Thanks, the custom menu works fine but I have problem with the script : when tried to remove the menu by running the 'unregistered' function, i got error : RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMeta' instance (may not be registered) . Also, when i press the shortcut, it won't run the command. I check in keymap, the shortcust exists and the command is : object.my_class . But why shortcut doesn't do anything. $\endgroup$
    – andio
    Commented Nov 9, 2020 at 9:15
  • $\begingroup$ The unregister_class bit is really for when you save this as a file and add it in as a proper addon via the preferences page. If you're running it from the scripting tab simply closing Blender will remove it. The register/unregister stuff is just there because, well, I always put it there and install my addons :) As for the shortcut not working, it should, it works for me. Perhaps you are choosing a shortcut that is already in use and is causing a conflict. What shortcut did you use? I used CTRL-ALT-W $\endgroup$
    – Psyonic
    Commented Nov 9, 2020 at 10:06
  • $\begingroup$ Hi i tried to install as add-on. Save your script as .py and install via preferences. But it doesn't show up in the 'select' menu ? not like when i run it from text editor. What is wrong ? $\endgroup$
    – andio
    Commented Nov 9, 2020 at 12:47
  • $\begingroup$ Also , when i install as add-on : there's no error , but got warning : upgrade to 2.8x is required. In fact , i'm using 2.83.7 . Any clue ? Thanks $\endgroup$
    – andio
    Commented Nov 9, 2020 at 13:10
  • $\begingroup$ The bl_info requires a "blender" member with value at least (2, 80, 0) to remove the warning. docs.blender.org/api/current/bpy.types.AddonPreferences.html $\endgroup$
    – batFINGER
    Commented Nov 9, 2020 at 14:14

You must log in to answer this question.

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