Graphics Reference
In-Depth Information
Creating an Interactive Add-on
You'venowseenthebasisofhowthePythonAPIworkstoenableyourscripttoaccessBlender'soperatorsand
data.Butnobodywantstohavetojumpthroughthehoopsofloadingupascriptinthetexteditorandexecuting
it every time they want to trigger some custom functionality. Fortunately, the Blender 2.5 API enables you to
write your script as an add-on so that its functionality can be seamlessly integrated with the rest of the Blender
interface. The add-on system is one of the biggest new developments to come from the recode. It's a great way
to add Python functionality, particularly if you want to share it with other users.
Making add-ons is especially simple to do if you start with one of the ready-made templates available from
the text editor, which is what you will do now.
The Addon Add Object Template
In this section, you'll create an add-on to add a single-vertex mesh to the 3D scene. You have already seen the
nuts and bolts of how that would be done in Python. When you use a template, the hard work is done for you
and the resulting script is much more elegant and useful. You'll only need to change a few lines.
Tousethetemplate,openupanewtexteditorwindowandselectTemplates>AddonAddObject.Thescript
will appear in the text editor window. You can edit this, change it, save it, or rename it as you like. The Tem-
plates menu can always be used to create a fresh, unchanged copy of the original template script.
Before you get to actually editing the script, let's run it as an add-on and then read though the template in
some detail, to see exactly what the code is doing.
To run the script, of course, you could simply click the Run Script button. That would work, but it's not the
way add-ons are natively executed, so let's do this in a more representative way:
1.
Save the script in the appropriate add-ons folder on your operating system; then click F8 to make
Blender update its scripts.
Where to Save Your Add-on
The correct place to save your add-on depends on your operating system. One way to do it is to save your script
anywhere and then to import it to the correct directory by clicking the Install Add-on button in the header of the
Add-ons area. Alternatively, you can save the script directly in your user Add-ons directory. To find out where
this is, you can run the System Info script from the Help menu as you learned to do in Chapter 1, “Working in
Blender.” The
Addons_Contrib
folder contains experimental scripts that can be seen by turning on Testing.
On the 64-bit Windows 7 machine I'm working with now, the directory is
(Home directory)\AppData\Roaming\Blender Foundation\Blender\2.60\scripts\addons
2.
When you've saved the script, press F8 to reload all scripts. Be sure to watch the System Console to
make sure there are no complaints. If not, your add-on should have loaded smoothly. Check the Add-ons
panel in the User Preferences window to verify that the Add Mesh: New Object add-on is there.
3.
Click the check box to the right of the add-on listing to activate the add-on, as shown in
Figure 13-5
.