=============== Class: Mesh I =============== .. index:: class, mesh .. |a| image:: /images/all/blender-extended/pyramid-100.png Objectives ========== .. tabularcolumns:: | c | l | :border: none +-------+---------------------------------------------------------------------+ | |a| | In an object-oriented world, parts of the model are mapped into | | | classes. This has many advantages and the most important rules are | | | shown here. Here is a complex example of how to create a basic | | | shape, which does not exist in Blender. It is a pyramid. | | | | +-------+---------------------------------------------------------------------+ Instructions ============ :Tasks: 1. Repeat the sub-steps shown here to create a class until you have a working version. 2. Create a class of its own »primitive«, eg. a wedge or the base form of a hex key. 3. Add a material to the class, which is given to the new object. Design data for a mesh ====================== We start with the ground of a pyramid. The first step is defining the vertices an there location in a 3D scene. After that, then for each area, the index values ​​of each surface section summarizes (a face is defined). The latter determines whether an area is closed or remains open, if it is not defined. .. code-block:: python :emphasize-lines: 0 def pyramid_values(): """ This function takes inputs and returns vertex and face arrays. no actual mesh data creation is done here. """ verts = [(-1, +1, 0), (+1, +1, 0), (+1, -1, 0), (-1, -1, 0)] faces = [(0, 1, 2, 3),] return verts, faces The coordinates of each point are given by the focus of our future pyramid. The whole thing still possible in a clockwise sense. .. todo:: Picture with coordinats Class: AddPyramid ================= After the keyword *class* follows a self-selected class name *AddPyramid*. As in the example with the simple class, our class inherits from a class in Blender: *bpy.types.Operator*. We need tree variable, for the correkt integration of our new class into Blender, its menues or lists for selection. .. literalinclude:: files/pyramid.py :language: python :lines: 24-31 Properties ========== Also tree parameters are nessesary to place a new pyramid in a 3D-World. .. literalinclude:: files/pyramid.py :language: python :lines: 27-32 Method: execute =============== The methode *execute* is using the coordintate given by the function *pyramid_values()*. The values are transfered to locale variables: *verts_loc* and *faces*. Now two new objects are created mit the operator *new()*. - one *Mesh* for the coordinates - one bmesh for ... .. todo:: bmash description .. literalinclude:: files/pyramid1.py :language: python :lines: 35-39 Coordinates second part ======================= With two for loops the coordintes are added to the new objects. .. literalinclude:: files/pyramid1.py :language: python :lines: 41-45 Create objects ============== Afte the preparation, a new object can be created and placed on a given location. .. literalinclude:: files/pyramid1.py :language: python :lines: 47-49 Display the object ================== With *object_utils* the object gets visible in a szene. If all works fine, the return value *{'FINISHED'}* is given back. .. literalinclude:: files/pyramid1.py :language: python :lines: 51-55 Register/Unregister =================== Blender needs a registration and unregristreation command for the new class. .. literalinclude:: files/pyramid1.py :language: python :lines: 58 - 63 Test run ======== Lets test the creation of a new pyramid, so far we only have the base. .. literalinclude:: files/pyramid1.py :language: python :lines: 65-70 With the next step, we will finish the pyramid. The code for the class ====================== .. literalinclude:: files/pyramid1.py :language: python