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?
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.
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.
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.
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.