Graphics Reference
In-Depth Information
The Class Definition
Writing these definitions in this order is intuitive, because the class calls the function. However, it's a bit easier
to understand what's going on if we look at these definitions in reverse order. So first, let's look at the class
definition, beginning on line 41. Later, we'll return to line 21 to look at the function definition.
The first line begins the definition of the
OBJECT_OT_add_object
class:
41 class OBJECT_OT_add_object(bpy.types.Operator, AddObjectHelper):
The keyword
class
indicates that this is a class definition. The arguments are the classes from which this
class inherits its properties and methods. Python allows multiple inheritance, so
OBJECT_OT_add_object
inherits the functionality of both
bpy.types.Operator
and
AddObjectHelper
. The former is the gen-
eral class for operators in Blender, so we know that this new class will be an operator. The latter is something
that was imported from the
add_utils
module. The
AddObjectHelper
class contains a variety of useful
shortcuts and built-in features to make it easier to build operators that (guess what?) add objects to the scene.
Among these features is an automatically generated interface for interactively setting the rotation and location
of the object you create, as you'll see shortly.
Thenextfewlinessetvariablesthatarenecessaryforregisteringoperations,whichyou'lllearnaboutshortly.
The
bl_idname
will be the identifier for the Python call of the operator, and the
bl_description
will
appear on the tool tip in the interface, as shown in
Figure 13-8
.
The
bl_label
value sets the text label on the
interactive panel, and
bl_options
sets the values for available operator options. In this case, the option to
register the operator and the option to make the operation reversible are set.
42 """Add a Mesh Object"""
43 bl_idname = "mesh.add_object"
44 bl_label = "Add Mesh Object"
45 bl_description = "Create a new Mesh Object"
46 bl_options = {'REGISTER', 'UNDO'}
Figure 13-8
The Add Object tooltip
Thenextfewlines,between line48andline53,setthe
scale
property.Unlikelocation androtation,which
are handled automatically on the object level (thanks to the
AddObjectHelper
), the effect of the
scale
value on the mesh data (i.e., the location of vertices) needs to be written out explicitly in your script. For this
reason, this value will be represented as a property of the
OBJECT_OT_add_object
class.
Settingaclasspropertyissimplyamatterofassigningavaluetoavariablewithintheclassdefinition.Inthis
case, the
scale
variable is assigned an object of class
FloatVectorProperty
, which is a class that was
imported in the first few lines of the script from the
bpy.props
module. The arguments for the constructor
of the object are mostly self-explanatory. The
name
,
default
value, and
description
are all what you'd
expect them to be. The
subtype
value is used to organize properties and help the
AddObjectHelper
to
know how to handle this property.