Graphics Reference
In-Depth Information
make sense, you might want to bone up on your trig functions (always a good idea if you want to do graphics
programming). The z coordinate is set to zero to make the base of the cone flat. These variable assignments
look like this:
x = (sin(j*pi*2/(cverts-1))*self.radius)*scale_x
y = (cos(j*pi*2/(cverts-1))*self.radius)*scale_y
z = 0
Each vertex is appended to the list of vertices like this:
verts.append(Vector((x,y,z)))
The append() function is standard Python. It adds elements to a list.
For each vertex around the base after the first one, two triangular faces are created: one upward towards the
apexoftheconeandoneinwardtoformthesolidbaseofthecone.Thisisdoneusingthefollowingconditional
block of code:
if j > 0:
faces.extend([[0,j+1,j+2],[1,j+1,j+2]])
The extend() functionisanotherstandardPythonlistfunction.Itissimilarto append() ,butratherthan
adding elements to a list on their own, it adds a list of elements to the list. In this case, two faces are added at a
time.
When this loop exits, the mesh's geometry has all been established. Next, we'll create the mesh datablock
object, pass the geometry data to that datablock, and add the mesh to the scene just as you saw in the template
example:
mesh = bpy.data.meshes.new(name='Bouncing Cone')
mesh.from_pydata(verts, edges, faces)
add_object_data(context, mesh, operator=self)
Youmighthavenoticedinthetemplateexamplethattheedgesofthedefaultaddedobjectarenothighlighted
when the object is first created, as is usual for selected objects. You can make sure that they are selected by
calling the update() function on the mesh as follows:
mesh.update(calc_edges=True)
A final point: The cone whose mesh we're creating from scratch starts out with mixed normals, the base
facing outward and the cone facing inward. Additionally, one vertex isn't connected, so we'll remove doubles
while in edit mode. First, we enter into edit mode to affect our mesh data:
obj = bpy.context.selected_objects
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
Then, we can recalculate normals and remove doubles before returning to object mode:
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.mesh.remove_doubles(mergedist=0.0001)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
Setting a Colored Material
At this point, the basic functionality of placing the mesh object in the scene has been completed, but there's
more to be done. The script should create colored bouncing cones, so we need to give the cones color and an-
imate their movement. There are a number of options for making them colored, but the most useful is probably
to give them a colored material.
Search WWH ::




Custom Search