Graphics Reference
In-Depth Information
"location": "View3D > Add > Bouncing Cone",
"description": "Add a bouncing, colored cone.",
"warning": "",
"wiki_url": "http://wiki.blender.org",
"tracker_url": "https://projects.blender.org",
"category": "Add Object"}
All of this should be familiar from the previous description of the bl_info data structure. Only the values
for name, location, description, and category have changed.
The next five lines are necessary imports. Most of these you've seen before. Using the * statement imports
everything from a module. We'll discuss the imported property classes in more detail shortly.
import bpy
from bpy.props import FloatVectorProperty, FloatProperty
from add_utils import AddObjectHelper, add_object_data
from mathutils import Vector
from math import *
After the imports, we declare a global variable to determine the number of vertices that will compose the
circular base of the cone. As one of the exercises at the end of this chapter, you'll change this from a global
variable to an integer property and make this value interactive. For now we'll keep it simple and let it be an
ordinary variable:
cverts = 10
The rest of the script is structurally identical to the example from the template. The function and class defin-
itions are written in the following order:
def addBouncingCone(self, context):
class Bouncing_Cone(bpy.types.Operator, AddObjectHelper):
def menu_func(self, context):
def register():
def unregister():
Note that the five lines of code presented here, if run exactly as written, without any content in the function
definitions, will produce errors. Python does not allow empty control structures, so a line ending with a colon
must be followed by some properly indented code. If you want to write a placeholder function (or any control
structure) without any content, use the Python command pass to fulfill this requirement. As you fill in differ-
entpartsofthescript,theadd-onusuallywon'tworkuntilit'scomplete.Infact,Blenderwon'tevenletyouturn
it on from the Add-ons menu if certain functionality hasn't been coded. Stick with it until the whole add-on is
written, and use comments in your script to describe new tools as you learn them.
Let's look at the content of these definitions from the bottom up.
register() and unregister()
Let's begin with the register() and unregister() functions:
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_add.append(menu_func)
def unregister():
Search WWH ::




Custom Search