0

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.

andio
  • 2,094
  • 6
  • 34
  • 70

2 Answers2

3

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()

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 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 ? – andio Nov 09 '20 at 14:10
  • 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? – brockmann Nov 09 '20 at 14:12
  • Thanks, I'm trying to find out how to install this code to Blender to make it loaded everytime i open Blender. – andio Nov 09 '20 at 14:18
  • great it works. – andio Nov 09 '20 at 14:53
1

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()

Psyonic
  • 2,319
  • 8
  • 13
  • 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. – andio Nov 09 '20 at 09:15
  • 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 – Psyonic Nov 09 '20 at 10:06
  • 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 ? – andio Nov 09 '20 at 12:47
  • 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 – andio Nov 09 '20 at 13:10
  • The bl_info requires a "blender" member with value at least (2, 80, 0) to remove the warning. https://docs.blender.org/api/current/bpy.types.AddonPreferences.html – batFINGER Nov 09 '20 at 14:14
  • Ok, I've updated the script to work as an addon, it just needed the correct stuff in the bl_info – Psyonic Nov 10 '20 at 07:15