Graphics Reference
In-Depth Information
In this case the edges are implied by the face, so it is not necessary to define any edges. The face is a list of
lists of vertices. In this case, there is only one list of vertices, corresponding to the one face of the plane.
In the next two lines, a mesh datablock object is created and the data is generated from the lists of vertices,
edges, and faces, just as you did in the example of the single vertex mesh previously.
33 mesh = bpy.data.meshes.new(name='New Object Mesh')
35 mesh.from_pydata(verts, edges, faces)
Lines 36 and 37 are commented out, so they don't do anything by default. If you are working with more
complexmeshesandyouneeddebugginginformationwithrespecttothevalidityofthemeshstructure,youcan
uncomment line 37.
36 # useful for development when the mesh may be invalid.
37 # mesh.validate(verbose=True)
In line 38, some magic happens:
38 add_object_data(context, mesh, operator=self)
The add_object_data() function is a function imported from the add_utils module that automat-
ically produces the interactive interface to enable you to tweak the Location, Rotation, and Scale values using
the sliders in the tool shelf when you add the object. You don't have to do anything at all for this. Of course,
you may want to control how things appear in the interface. This is possible to do by overriding the automatic
interface generation. You'll see how this is done later in this chapter.
Registration
The next few function definitions are somewhat boilerplate definitions that need to be present for add-ons. The
add_object_button() function takes care of placing the function in the menu. The text argument is the
text that will be on the menu entry and the icon value lets you choose the icon to go with the functionality.
def add_object_button(self, context):
self.layout.operator(
OBJECT_OT_add_object.bl_idname,
text="Add Object",
icon="PLUGIN")
(Re)using Icons
You can use any icon from Blender's icon set in your interface. Unfortunately, the icon set is fixed and it is
not trivial to add to it. This is why you will sometimes see new or altered functionality coupled with some-
what unintuitive icons. An example of this was the temporary use of a speaker button to represent “muting”
the behavior of animation curves in the nonlinear animation editor. It's not that anybody thought a speaker
was an optimal symbol for this functionality; it was just that the selection of icons was limited to ones that
already exist.
The register() function calls the register_class() function for your newly created object and
then adds the add_object_button function to the list of panels and menu items that need to be displayed
in the interface:
def register():
bpy.utils.register_class(OBJECT_OT_add_object)
bpy.types.INFO_MT_mesh_add.append(add_object_button)
 
Search WWH ::




Custom Search