It’s not difficult to load Python scripts into Blender’s text editor, but there’s something reassuring about just getting them to populate in the space bar menu.
How do you get Blender to do that, you ask?
You make your script into an add-on.
I’m stealing the code for the fireflies add-on from an old post on generating random spheres in Blender.
Step One: The Add-On Info Dictionary
This is the easy part. It’s a dictionary with all your code’s information, like a birth certificate for your tool. Name, author, description, etc. Just pop all that in between brackets, like so:
bl_info = \ { "name" : "Fireflies" "author" : "GifGuide2Code" "version" : (1, 0, 0), "blender" : (2, 5, 7) "description" : "This stuff happens." "category" : "Object", }
The only thing that’s a little odd here is the category. I saw that the first time and thought, “But where, Blender, do you get the categories from?” Here, it turns out. It has to be one of the categories in the Add-ons tab.
In between the ad-on dictionary and the next block, you’ll want to import any necessary modules. For this particular code to work, I’ll also be needing the random module.
import bpy import random
Step 2: Create an Operator Class
Operators are the tools you use to make things happen in Blender. You can access a complete list of them by opening the text editor, going to Help > Operator Cheat Sheet, and then selecting OperatorList.txt from the drop-down menu.
To make your own operator (or tool), you’ll need to make it into it’s own class. I’m calling this one fireflies.
class fireflies(bpy.types.Operator):
After that, the class will need a bl_idname class attribute, formatted as:
CategoryOfOperator.NameOfOperator
Here are your options for category of operator.
Below that, you can specify a bl_label, which will be the name the user sees in the spacebar menu.
bl_idname = "object.fireflies" bl_label = "Create fireflies"
Step 3: Execute the Things
This is where the meat of your code will live. Start off with def execute(self, context), and finish off with return{‘FINISHED’}.
def execute(self, context): all the code return{'FINISHED'}
At the end of the script, you can add a register_class call so that blender will add the operator to its collection, and if you include those last two lines, you can test the add-on without installing it in your user preferences.
def register(): bpy.utils.register_class(fireflies) if __name__ == "__main__": register()
That’s a good time so save the python. Make sure you remember where it’s stashed for later.
Step 4: Run the Script
After you run it, you should be able to access your code in the space bar menu.
Cool, right? That tells you at least your code is functional. Unless it doesn’t function, in which case you’ll probably want to look into debugging before installing it as an add-on.
Step 5: Install the Add-on
Ok, navigate over to the Add-ons tab (File > User Preferences > Add-Ons). There’ll be a button at the bottom that says “Install Add-on from File…” Click that and track down your .py file.
Hit the checkbox next to your operator and then save user settings. Now even if you close out Blender, you’ll still see your tool in the space bar menu.
Here’s a template for your own add-on:
bl_info = { "name": "NameOfThing", "author": "YourName", "version": (1,0,0), "category": "SelectFromAddOnCategories", } import bpy #Any Other Modules You Might Need class NameOfThing(bpy.types.Operator): """NameOfThing""" bl_idname = "object.NameOfThing" bl_label = "Short Description of Thing" <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span> def execute(self, context): #Your Code Here return{'FINISHED'} def register(): bpy.utils.register_class(NameOfThing) def unregister(): bpy.utils.register_class(NameOfThing) if __name__ == "__main__": register()
The complete code for the firefly add-on is over at gist.