def triMesh2Polyhedron(self, tm): """Convert a TriMeshGeom into a PolyhedronGeom. """ pg = PolyhedronGeom() # Copy the vertices... pg.verts.resize(tm.verts.size()) tm.verts.copyValues(0, tm.verts.size(), pg.verts, 0) # Allocate polygons... pg.setNumPolys(tm.faces.size()) # Copy primitive variables... # (tm must not have any facevarying or facevertex variables) for name, storage, type, mult in tm.iterVariables(): slot = tm.slot(name) pg.newVariable(name, storage, type, mult, slot.size()) newslot = pg.slot(name) slot.copyValues(0, slot.size(), newslot, 0) return pg
def createPolyhedron(self, parent=None, name=None): """Create a polyhedron from the current set of faces. Returns the Polyhedron object. """ # Build lookup tables (so that only the verts that are really # required are stored in the Polyhedron) # # Key: Original vertex index - Value: New vertex index vert_lut = {} has_normals = True has_tverts = True iv = 0 for f in self.faces: for v,tv,n in f: if v not in vert_lut: vert_lut[v] = iv iv+=1 if tv==None: has_tverts = False if n==None: has_normals = False numpolys = len(self.faces) numverts = len(vert_lut) pg = PolyhedronGeom() pg.verts.resize(numverts) pg.setNumPolys(numpolys) # Set vertices for v in vert_lut: newidx = vert_lut[v] pg.verts[newidx] = self.verts[v] # Set polys (this has to be done *before* any FACEVARYING variable # is created, otherwise the size of the variable wouldn't be known) idx = 0 for i,f in enumerate(self.faces): loop = [] for v,tv,n in f: loop.append(vert_lut[v]) pg.setPoly(i,[loop]) # Create variable N for storing the normals if has_normals: pg.newVariable("N", FACEVARYING, NORMAL) N = pg.slot("N") idx = 0 for f in self.faces: for v,tv,n in f: N[idx] = self.normals[n] idx += 1 # Set texture vertices if has_tverts: pg.newVariable("st", FACEVARYING, FLOAT, 2) st = pg.slot("st") idx = 0 for f in self.faces: for v,tv,n in f: st[idx] = self.tverts[tv] idx += 1 obj = Polyhedron(name=name, parent=parent) obj.geom = pg # Set the materials self.initMaterial(obj) return obj
def createPolyhedron(self, parent=None, name=None): """Create a polyhedron from the current set of faces. Returns the Polyhedron object. """ # Build lookup tables (so that only the verts that are really # required are stored in the Polyhedron) # # Key: Original vertex index - Value: New vertex index vert_lut = {} has_normals = True has_tverts = True iv = 0 for f in self.faces: for v, tv, n in f: if v not in vert_lut: vert_lut[v] = iv iv += 1 if tv == None: has_tverts = False if n == None: has_normals = False numpolys = len(self.faces) numverts = len(vert_lut) pg = PolyhedronGeom() pg.verts.resize(numverts) pg.setNumPolys(numpolys) # Set vertices for v in vert_lut: newidx = vert_lut[v] pg.verts[newidx] = self.verts[v] # Set polys (this has to be done *before* any FACEVARYING variable # is created, otherwise the size of the variable wouldn't be known) idx = 0 for i, f in enumerate(self.faces): loop = [] for v, tv, n in f: loop.append(vert_lut[v]) pg.setPoly(i, [loop]) # Create variable N for storing the normals if has_normals: pg.newVariable("N", FACEVARYING, NORMAL) N = pg.slot("N") idx = 0 for f in self.faces: for v, tv, n in f: N[idx] = self.normals[n] idx += 1 # Set texture vertices if has_tverts: pg.newVariable("st", FACEVARYING, FLOAT, 2) st = pg.slot("st") idx = 0 for f in self.faces: for v, tv, n in f: st[idx] = self.tverts[tv] idx += 1 obj = Polyhedron(name=name, parent=parent) obj.geom = pg # Set the materials self.initMaterial(obj) return obj