Graphics Reference
In-Depth Information
48 scale = FloatVectorProperty(
49 name='scale',
50 default=(1.0, 1.0, 1.0),
51 subtype='TRANSLATION',
52 description='scaling',
53 )
Finally, the
execute
method contains the actual code that gets called when the operator is executed in
Blender. It's common to have this be simply a single function call and have that function contain the code you
wanttorun.Inthiscase,thefunctionis
add_object()
,whichwasdefinedpreviouslyinthescriptandwhich
you'll look at shortly. The return value is
FINISHED
, indicating that this is a function that is called, runs, and
then exits when complete.
55 def execute(self, context):
57 add_object(self, context)
59 return {'FINISHED'}
The Function Definition
Now that you've seen the context in which it's called, it will be more meaningful to look at the
add_ob-
ject()
function's definition. Turn your attention back to line 21:
21 def add_object(self, context):
The first things to note are the arguments to the function call,
self
and
context
. The
self
variable will
carry the
self
value from the
OBJECT_OT_add_object
class. In Python,
self
is a special term represent-
ing an object itself. So in this case,
self
is the instance (object) of the
OBJECT_OT_add_object
class. The
context
value has been passed from one method call to the next, beginning with the registration of the class.
The context object carries the information about Blender's current state that the operator needs to know how to
execute.
Moving into the function definition, you can see that some local variables,
scale_x
and
scale_y
, are
given values that draw on the
x
and
y
components of the
scale
vector of the
OBJECT_OT_add_object
object (passed to the function as the variable
self
). Next, a list of three-dimensional vectors is assigned to the
verts
variable. These represent the locations in space of the four vertices that will make up the default plane
mesh object created by this operator. Note that the positions of the
x
and
y
values are multiplied by
scale_x
and
scale_y
, respectively. The position of a mesh's vertices in space depends on the scale of the mesh 3D
object.
22 scale_x = self.scale.x
23 scale_y = self.scale.y
25 verts = [Vector((-1 * scale_x, 1 * scale_y, 0)),
26 Vector((1 * scale_x, 1 * scale_y, 0)),
27 Vector((1 * scale_x, -1 * scale_y, 0)),
28 Vector((-1 * scale_x, -1 * scale_y, 0)),
29 ]
The next two lines define the list of edges and the list of faces:
31 edges = []
32 faces = [[0, 1, 2, 3]]