Aug 25, 2014

29. 3D Structure

We will use a Python script to create a pyramid structure for the next tutorial.




There are many ways to create structures, such as box modeling. With a Python script (or alternatively, using an addon), we can create structures depending on a set of parameters.




To create a 3D structure, we need a list of 3D points. These points are (x,y,z) numbers. Besides these, our scripts also has to have list of all faces.




Faces are 3-vertex or 4-vertex points arranged in a right-handed loop. When the mesh is loaded into jMonkey, any quads will be converted to triangles.




This shows a planes. In addition to giving the locations of 1, 2, 3 and 4, we have to give the face. We can give the quad (1,2,3,4) or triangles (1,2,3) and (1,3,4). It can also be broken as (1,2,4) and (2,3,4) triangles.




By running the python script (pyramid.py), in the text editor and then clicking run script, a 3D structure is created. Save the blender file, as pyramid.blend as it will be used later.


# pyramid.py

import bpy

def createMesh(name, origin, verts, faces):
    me = bpy.data.meshes.new(name+'Mesh')
    ob = bpy.data.objects.new(name, me)
    ob.location = origin
    ob.show_name = True
    bpy.context.scene.objects.link(ob)
    me.from_pydata(verts, [], faces)
    me.update(calc_edges=True)
    return ob
 
def run(s,d,h,Level,origin):
    verts = []
    faces = []
    for lev in range(Level):
        # Outer Square (z = lev*h)
        sd = s - lev*d
        z = lev*h
        verts.append((-sd/2,-sd/2,z)) # 0+lev*8
        verts.append((sd/2,-sd/2,z)) # 1+lev*8
        verts.append((sd/2,sd/2,z)) # 2+lev*8
        verts.append((-sd/2,sd/2,z)) # 3+lev*8
        # Inner Square (do not do for last level)
        if lev!=Level-1:
            verts.append((-(sd-d)/2,-(sd-d)/2,z)) # 4+lev*8
            verts.append(((sd-d)/2,-(sd-d)/2,z)) # 5+lev*8
            verts.append(((sd-d)/2,(sd-d)/2,z)) # 6+lev*8
            verts.append((-(sd-d)/2,(sd-d)/2,z)) # 7+lev*8
        # add Faces
        if lev!=0:
            # not first level
            a1 = 4+(lev-1)*8, 5+(lev-1)*8, 1+lev*8, 0+lev*8
            a2 = 5+(lev-1)*8, 6+(lev-1)*8, 2+lev*8, 1+lev*8
            a3 = 6+(lev-1)*8, 7+(lev-1)*8, 3+lev*8, 2+lev*8
            a4 = 7+(lev-1)*8, 4+(lev-1)*8, 0+lev*8, 3+lev*8
            faces.extend((a1,a2,a3,a4))
        if lev!=Level-1:
            a1 = 3+lev*8, 0+lev*8, 4+lev*8, 7+lev*8
            a2 = 0+lev*8, 1+lev*8, 5+lev*8, 4+lev*8
            a3 = 1+lev*8, 2+lev*8, 6+lev*8, 5+lev*8
            a4 = 2+lev*8, 3+lev*8, 7+lev*8, 6+lev*8
            faces.extend((a1,a2,a3,a4))
        else:
            a1 = 0+lev*8, 1+lev*8, 2+lev*8, 3+lev*8
            faces.append(a1)
    ob1 = createMesh('Pyramid', origin, verts, faces)
    
if __name__ == "__main__":
    run(s = 5, # base side
        d = 1, # side decrement
        h = 0.5, # height increment
        Level = 5, # levels
        origin = (0,0,0)) # location





No comments:

Post a Comment