Ejemplo n.º 1
0
 def write(self,fname,scene):
     """ write an ascii point file """
     print("Write "+fname)
     d = alg.Discretizer()
     f = file(fname,'w')
     isptsfile = ('.pts' in fname)
     ispwnfile = ('.pwn' in fname)
     isxyz = ('.xyz' in fname)
     if isptsfile or ispwnfile:
         nbpoints =  0
         for i in scene:
             if i.apply(d):
                 p = d.discretization
                 if isinstance(p,sg.PointSet) :
                     nbpoints += len(p.pointList)
         f.write(str(nbpoints)+'\n')
     for i in scene:
         if i.apply(d):
             p = d.discretization
             if isinstance(p,sg.PointSet) :
                 hasColor = not p.colorList is  None and len(p.colorList) > 0
                 col = i.appearance.ambient
                 for i,pt in enumerate(p.pointList):
                     f.write(str(pt.x)+' '+str(pt.y)+' '+str(pt.z)+' ')                        
                     if not isxyz and not ispwnfile:
                         if hasColor:                          
                             col = p.colorList[i]
                         if isptsfile:
                             f.write(str(rgb2intensity(col)))
                         f.write(str(col.red)+' '+str(col.green)+' '+str(col.blue)+'\n')
                     else: f.write('\n')
     f.close()
Ejemplo n.º 2
0
    def write(self, fname, scene):
        """ Write an OBJ file from a plantGL scene graph.

        This method will convert a PlantGL scene graph into an OBJ file.
        It does not manage  materials correctly yet.

        :Examples:
            import openalea.plantgl.scenegraph as sg
            scene = sg.Scene()"""
        print("Write " + fname)
        d = alg.Discretizer()
        f = file(fname, 'w')

        line = '# File generated by PlantGL'
        f.write(line + '\n')

        vertices = []  # List of point List
        normals = []  # List of normal List
        texcoords = []  # List of texture List
        faces = []  # list  of tuple (offset,index List)

        counter = 0
        for i in scene:
            if i.apply(d):
                p = d.discretization
                pts = p.pointList
                ns = p.normalList
                ts = p.texCoordList
                indices = p.indexList
                n = len(p.pointList)
                if n > 0:
                    vertices.append(pts)
                    if ns:
                        normals.append(ns)
                    if ts:
                        texcoords.append(ts)
                    faces.append(Faces(i.name, counter + 1, p))
                counter += n

        for pts in vertices:
            for x, y, z in pts:
                f.write('v    %f %f %f\n' % (x, y, z))
            f.write('\n')
        for pts in normals:
            for x, y, z in pts:
                f.write('vn    %f %f %f\n' % (x, y, z))
            f.write('\n')

        for pts in texcoords:
            for x, y in pts:
                f.write('vt    %f %f \n' % (x, y))
            f.write('\n')

        mtl_file = os.path.basename(fname)
        mtl_file = os.path.splitext(mtl_file)[0] + '.mtl'
        f.write('mtllib %s' % (mtl_file))
        for face in faces:
            face.obj(f)

        f.close()
# Need to get an AxialTree from the Lpy to be able to convert it to a scene.
lsys = Lsystem('175_3_mod_noColors.lpy')

lString = lsys.derive()
# interpretedString is an AxialTree
interpretedString = lsys.interpret(lString)
# The AxialTree can be converted to a Scene
scene = lsys.sceneInterpretation(interpretedString)

# Entities in the Scene can be discretized to generate polygons.
# Most of this code block was obtained from: https://github.com/pradal/scanalea/blob/master/src/scanalea/vtk.py
vertices = []  # List of point List
normals = []  # List of normal List
texcoords = []  # List of texture List
faces = []  # list  of tuple (offset,index List)
d = alg.Discretizer()
counter = 0
pointCounter = 0
faceCounter = 0
for i in scene:
    if i.apply(d):
        p = d.discretization
        pts = p.pointList
        ns = p.normalList
        ts = p.texCoordList
        indices = p.indexList
        n = len(p.pointList)
        if n > 0:
            vertices.append(pts)
            pointCounter += 1
            if ns: