def convertObject(self, obj): """Converts an object into a polyhedron or trimesh if necessary. The return value is a GeomObject (TriMeshGeom or PolyhedronGeom) or None. """ geom = obj.geom if isinstance(geom, TriMeshGeom): return geom if not isinstance(geom, PolyhedronGeom): # Try to convert into a polyhedron... pg = PolyhedronGeom() try: geom.convert(pg) geom = pg except: pass # Is it a PolyhedronGeom that has no polys with holes? then return # the geom... if isinstance(geom, PolyhedronGeom) and not geom.hasPolysWithHoles(): return geom # Try to convert into a triangle mesh... tm = TriMeshGeom() try: geom.convert(tm) return tm except: pass return None
def __init__( self, name="Polyhedron", # dynamics = False, # static = False, verts=[], polys=[], **params): WorldObject.__init__(self, name=name, **params) self.geom = PolyhedronGeom() # self.dynamics = dynamics # self.static_slot = BoolSlot(static) ph = self.geom if len(verts) > 0: ph.verts.resize(len(verts)) i = 0 for v in verts: ph.verts.setValue(i, vec3(v)) i += 1 if len(polys) > 0: ph.setNumPolys(len(polys)) i = 0 for poly in polys: ph.setPoly(i, poly) i += 1
def __init__(self, parent=None): lwob.LWOBReader.__init__(self) # Parent node for the Lightwave object self.parent = parent # The TriMeshGeom that receives the triangle mesh self.trimeshgeom = TriMeshGeom() # The PolyhedronGeom that receives the mesh (if it is no triangle mesh) self.polyhedrongeom = PolyhedronGeom() # The generated WorldObject self.worldobj = None # The number of surfaces in the file self.numsurfaces = 0 # A mapping from surface name to material id # Key: Surface name / Value: Material id (0-based) self.surface_ids = {} # Message flags so that warning messages are only output once self.crv_msg = False self.patch_msg = False
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