def GenerateCollisionEgg(heightmap, output, input='data/collision3k.egg', scale=100.0): input_egg = EggData() input_egg.read(input) output_egg = EggData() output_egg.setCoordinateSystem(CS_default) output_egg.stealChildren(input_egg) VertexPool = output_egg.getChildren()[1] print("Generating mesh, this may take a while...", end="") for i in range(VertexPool.getHighestIndex()): if i%20000 == 0: try: base.graphicsEngine.renderFrame() print('.', end="") except: pass vert = VertexPool.getVertex(i) x0, y0, _ = vert.getPos3() #x, y = int(floor(x0+0.5)), int(floor(y0+0.5)) x, y = int(x0), int(y0) if x==512: x=511 elif x==0: x=1 if y==512: y=511 elif y==0: y=1 vert.setPos(LPoint3d(x0, y0, heightmap.getBright(x,512-y)*scale)) output_egg.writeEgg(output)
def fbx2egg(fbx_path, egg_path): manager, scene = FbxCommon.InitializeSdkObjects() FbxCommon.LoadScene(manager, scene, fbx_path) prepare_scene(scene) data = EggData() data.addChild(EggCoordinateSystem(CSYupRight)) # TODO: read coordinate system from fbx group = EggGroup("walking") # TODO: get name from fbx anim layer group.setDartType(EggGroup.DTDefault) traverse_joints(scene, scene.GetRootNode(), group) data.addChild(group) data.writeEgg(egg_path)
def fbx2egg_animation(fbx_path, egg_path): manager, scene = FbxCommon.InitializeSdkObjects() FbxCommon.LoadScene(manager, scene, fbx_path) prepare_scene(scene) data = EggData() data.addChild(EggCoordinateSystem(CSYupRight)) skeleton = EggTable("<skeleton>") walking = EggTable("walking") # TODO: get name from animation layer walking.setTableType(EggTable.stringTableType("bundle")) walking.addChild(skeleton) table = EggTable() table.addChild(walking) for layer in get_anim_layers(scene): traverse_animation_curves(scene, scene.GetRootNode(), layer, skeleton) data.addChild(table) data.writeEgg(egg_path)
def makeEgg(filename): # Egg data vp = EggVertexPool('sky') data = EggData() data.addChild(vp) data.setCoordinateSystem(1) # CS_zup_right # Vertices vmap = {} for idx, (x, y, z) in enumerate(vertices): vmap[idx] = makeVertex(vp, x, y, z) # Faces for (i1, i2, i3) in indices: v1 = vmap[i1] v2 = vmap[i2] v3 = vmap[i3] makePoly(data, [v1, v2, v3]) # Save data.recomputePolygonNormals() data.writeEgg(Filename(filename))
class STL2EGG(): def __init__(self, in_file, out_file): #New container for data: self.data = EggData() #New Container for vertex: self.vertexPool = EggVertexPool('model') #Adding vertexPool to the main data container: self.data.addChild(self.vertexPool) self.load(in_file) self.data.setCoordinateSystem(CSZupRight) self.data.writeEgg(out_file) def load(self, filename): f = open(filename, 'r') header = f.read(6) isASCII = ( header== 'solid ') if isASCII: self.name = f.readline() self.parseASCII(f) else: f.close f = open(filename, 'rb') self.parseBin(f) def getName(self): return self.name def readVecteur(self, data): v = EggVertex() x,y,z = struct.unpack('fff', data) v.setPos(Point3D(x,y,z)) return v def parseBin(self, f): print "Parsing Bin File" Header = f.read(80) Nb_Face = struct.unpack('i', f.read(4))[0] print "Nombre de face : %i" % (Nb_Face) for i in range(0,Nb_Face): self.poly = EggPolygon() self.data.addChild(self.poly) # Normal struct normal = self.readVecteur(f.read(12)) u = self.readVecteur(f.read(12)) v = self.readVecteur(f.read(12)) w = self.readVecteur(f.read(12)) attrib = struct.unpack('H' , f.read(2))[0] # Attributes #Adding Vertex to Triangle self.poly.addVertex(self.vertexPool.addVertex(u)) self.poly.addVertex(self.vertexPool.addVertex(v)) self.poly.addVertex(self.vertexPool.addVertex(w)) self.poly.recomputePolygonNormal() self.poly.setColor( VBase4( 0.0, 0.0, 0.5, 0.5) ) def parseASCII(self,f ): for line in f: line = line.lower().strip() commande = line.split(" ") Nb_Param = len(commande) try: {'facet' : self.facet, 'outer' : self.outer, 'vertex' : self.vertex, 'endloop' : self.endloop, 'endfacet' : self.endfacet}[commande[0]](commande,Nb_Param) except KeyError: pass def facet(self,commande,Nb_Param): if (Nb_Param==5 and commande[1] == "normal"): #We are Ignoring normal --> will be computed later #Creating a new polygon : self.poly = EggPolygon() self.data.addChild(self.poly) def outer(self,commande,Nb_Param): pass def vertex(self,commande,Nb_Param): if (Nb_Param==4): x,y,z = float(commande[1]),float(commande[2]),float(commande[3]) if (self.poly != None): #Creating a new vertex with coords : v = EggVertex() v.setPos(Point3D(x,y,z)) #Adding the new Vertex to the polygon : self.poly.addVertex(self.vertexPool.addVertex(v)) def endloop(self,commande,Nb_Param): #End of the Loop : self.poly.recomputePolygonNormal() #As STL files don't contain colors: self.poly.setColor( VBase4( 0.0, 0.0, 0.5, 0.5) ) def endfacet(self,commande,Nb_Param): pass
def write(filename: str, egg_data: egg.EggData) -> None: """write egg data to a file""" egg_data.writeEgg(core.Filename(filename))