Graphics Reference
In-Depth Information
col.prop(self, 'rotation')
col.prop(self, 'scale')
That completes the draw() method, which will give you your complete interactive interface for the operat-
or.
The execute() method
The next method that needs to be defined for this class is the execute() method, which contains the code
the operator will run when activated. As in the previous example, we'll write the operator's execution code in a
separate function, called addBouncingCone() , and call that function from the execute method like this:
def execute(self, context):
addBouncingCone(self, context)
return {'FINISHED'}
This completes the Bouncing_Cone class definition.
Adding the Cone
Now, we'll define the addBouncingCone() function. This definition starts off in a familiar way:
def addBouncingCone(self, context):
We'll retain the code from the original template to pass the scale properties of the object to local variables
(this is not necessary; the values can be accessed through self.scale at any time, but it helps to keep things
a bit tidier):
scale_x = self.scale.x
scale_y = self.scale.y
scale_z = self.scale.z
Next, we'll create the set of vertices with two initial vertices. We'll place one of these vertices at the origin
of the space (this will be where the cursor is). This vertex will represent the center point of the base of the cone.
We'll place the other vertex directly above the first at a height of self.height multiplied by scale_z .
These values will be interactively adjustable, and the height of the cone will change depending on their values.
verts = [Vector((0,0,0)),Vector((0,0,self.height*scale_z))]
We'll set edges and faces to empty lists. Later, we'll add faces to the faces list. It wouldn't make sense to
do that at this point, because there are only two vertices, and they don't form any part of a single face.
edges = []
faces = []
The next block of text runs through a loop ranging from zero to the number of vertices you chose previously
for the base of the cone (recall that this value was set to 10). The loop is initialized like this:
for j in range(0,cverts,1):
This is a standard Python for loop. The arguments of range represent the minimum, the (exclusive) max-
imum, and the interval. The function returns a list, which j iterates over.
For each of these 10 iterations, the coordinates of the corresponding vertex need to be calculated. We use a
littlesimpletrigonometrytopositiontheverts.The x and y valuesarecalculatedfromthesineandcosineofthe
angle of the position of the vertex multiplied by the radius value and the scale value for the axis. If this doesn't
Search WWH ::




Custom Search