what is the fastest (easiest?) way to create a shortcut for a custom script

Hello all!

Occasionally i’ll write a few lines of basic code which i’ll run in the text editor, however, it would be handy to be able to create a shortcut to run it from the 3dview instead (or essentially another window that isn’t the text editor)

What is the fastest (easiest?) way to create a shortcut for this?

Would it need to be an addon?

If a simple script was (in theory of course i realise this wouldn’t be an actually useful thing to activate with a shortcut key)…

a=5
b=4
print(a+b)

And then that was it, obviously the system console window would have to be open to see the result in this case but is there an easy way to trigger with a shortcut?

Also could it be assigned a shortcut from inside the text scripting window or would it need to show up in the user preferences > input before you could assign a key to it? If so how would you go about doing that?

Many thanks,
Aidy.

I have two methods for when I need quick access to a script. The super-easy version is to just add a tiny text window to my current screen and then I can just quickly hover my mouse over the text window and “Alt+P” to run the script.

If that gets annoying then adding a simple operator is fairly trivial. Once you have added it then you can run it by hitting “Space” in your 3D view and accessing the name of your operator just like anything else.

To add an operator go to your Text window and use the menu “Template->Operator Simple”

I’ve added some additional comments on what you need to change and why:


import bpy

# *** CHANGE THIS ***
# Put the logic for your script in this function body.  This is effectively
# what is executed when your operator is called.
def main(context):
    for ob in context.scene.objects:
        print(ob)

# *** CHANGE THIS ***
# You want to change the class name to something unique and descriptive for
# your operator.
class SimpleOperator(bpy.types.Operator):
    '''Tooltip'''

    # *** CHANGE THIS ***
    # This is "the magic" that ties your operator into the Python "bpy.ops" structure.
    # The line below would create an operator that could be called by
    # bpy.ops.object.simple_operator.  The first part defines the context that the
    # operator works in so you can replace this with "view3d.my_op" and you would
    # get an operator called bpy.ops.view3d.my_op that runs in the 3D View.
    bl_idname = "object.simple_operator"

    # *** CHANGE THIS ***
    # This is the string that shows up for the "operator name" when you hit the
    # spacebar and search though all the defined operators in the system.
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    # *** CHANGE THIS ***
    # 'SimpleOperator' should be replaced with whatever you named your class above.
    bpy.utils.register_class(SimpleOperator)


def unregister():
    # *** CHANGE THIS ***
    # Replace 'SimpleOperator' with your class name.
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

    # ** CHANGE THIS ***
    # This string should be "bpy.ops" + bl_idname.  If your bl_idname is:
    # bl_idname = "view3D.my_op"
    # then your line below should be:
    # bpy.ops.view3d.my_op
    bpy.ops.object.simple_operator()

There are a few ways to make this accessible to your .blend files.

  1. Open the text block in your .blend and run it once. Then it is registered, the down side is you have to run it once from the text window every time you open your .blend.
  2. Check-box the “Register” button in the text block. This automatically registers your class when you open your .blend file. The down side is that you have to keep that text block saved in your .blend file but that is not that big of a deal.
  3. You can add it to your “…\Blender Foundation\Blender<version>\scripts\startup\bl_operators” directory and add the name of your .py file to “…\bl_operators__init.__.py” with the other “modules”. This will load your operator every time you start Blender. That makes the command available to all your blend files.

Forgot to mention, if you add it as an operator, then you can tie a hot-key to it though the standard User Preferences window.

This is great, thanks so much! That is exactly all the info i was hoping for! :smiley:

I think the neatest tie in might be to go the operator route in the style of option 2 that you made…

"2) Check-box the “Register” button in the text block. This automatically registers your class when you open your .blend file. "

But I think I might like to tinker with the init file as well for a complete understanding.

Thanks again!

preface: I prefer to use an external text editor.

For simple, personal-use scripts, instead of going the route of an addon, I just place scripts in the startup folder.
example:


import bpy

class LAPTOPFIX_OT_laptop_fix(bpy.types.Operator):
    bl_idname = "fix.for_laptop"
    bl_label = "Toggle laptop mode"
    def execute(self,context):
        context.user_preferences.inputs.use_mouse_emulate_3_button ^= 1
        context.user_preferences.inputs.use_emulate_numpad ^= 1
        return {"FINISHED"}

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()

Then the spacebar-menu to call it… unless i want a hotkey, in which case something like this:


bpy.context.window_manager.keyconfigs.active.keymaps['Blah'].keymap_items.new('op.idname',type='A',ctrl=True,alt=True,shift=True,value='PRESS')

would go in the register function, after the bpy.utils.register_module(name) line.

dustractor!

That’s a great idea too, thanks for that!

I like the concept of having a script for updating preferences between versions and so forth.

Very cool! :slight_smile: