[英]How to create a collada file from an objects faces' vertices information?
I have a project that requires me to export a model in a .dae Collada format. 我有一个项目,要求我以.dae Collada格式导出模型。 I am struggling to figure out how to do this.
我正在努力弄清楚该如何做。 I have been trying to use pyCollada to do so, but I have limited experience with 3D modeling and the file structure is confusing me.
我一直在尝试使用pyCollada这样做,但是我在3D建模方面的经验有限,并且文件结构使我感到困惑。 Why does have to specify an array of "normal_floats" to build a polygon.
为什么必须指定一个“ normal_floats”数组来构建多边形。 Aren't the vertices themselves enough?
顶点本身不够吗?
I have the all the vertices of each face of the object and need to export the data into a collada format? 我具有对象每个面的所有顶点,并且需要将数据导出为collada格式吗? Is there an easy way to do this since each face is two dimensional?
因为每个面都是二维的,有没有简单的方法可以做到这一点? Is there an algorithm I can simply feed the vertices into to generate the appropriate faces of the object?
有没有一种算法可以将顶点简单地馈入以生成对象的适当面? Any help would be appreciated.
任何帮助,将不胜感激。
Additionally, I currently have an algorithm that draws the object in openGL. 另外,我目前有一种在openGL中绘制对象的算法。 Is there a way I can reuse code for this in generating the export file?
有什么方法可以在生成导出文件时重用此代码?
Update this is the tutorial I was attempting to follow to create the object: http://pycollada.github.io/creating.html 更新这是我尝试遵循的创建对象的教程: http : //pycollada.github.io/creating.html
#Allows the laminate to get exported as a DAE.
def toDAE(self):
"""
Exports the current lamiante to a DAE file format
"""
import collada
mesh = collada.Collada()
layerdef = self.layerdef
nodes = [] # Each node of the mesh scene. Typically one per layer.
for layer in layerdef.layers:
layer_thickness = layer.thickness
shapes = self.geoms[layer]
zvalue = layerdef.z_values[layer]
height = float(zvalue) #* 100 #*
if (len(shapes) == 0) : #In case there are no shapes.
continue
for s in shapes:
geom = self.createDAEFromShape(s, height, mesh, layer_thickness)
mesh.geometries.append(geom)
effect = collada.material.Effect("effect", [], "phone", diffuse=(1,0,0), specular=(0,1,0))
mat = collada.material.Material("material", "mymaterial" + str(s.id), effect)
matnode = collada.scene.MaterialNode("materialref" + str(s.id), mat, inputs=[])
mesh.effects.append(effect)
mesh.materials.append(mat)
geomnode = collada.scene.GeometryNode(geom, [matnode])
node = collada.scene.Node("node" + str(s.id), children=[geomnode])
nodes.append(node)
myscene = collada.scene.Scene("myscene", nodes)
mesh.scenes.append(myscene)
mesh.scene = myscene
filename = popupcad.exportdir + os.path.sep + str(self.id) + '.dae' #
mesh.write(filename)
def createDAEFromShape(self, s, layer_num, mesh, thickness): #TODO Move this method into the shape class.
import collada
vertices = s.extrudeVertices(thickness, z0=layer_num)
#This scales the verticies properly. So that they are in millimeters.
vert_floats = [float(x)/(popupcad.SI_length_scaling) for x in vertices]
vert_src_name = str(self.id) + '|' + str(s.id) + "-array"
vert_src = collada.source.FloatSource(vert_src_name, numpy.array(vert_floats), ('X', 'Y', 'Z'))
geom = collada.geometry.Geometry(mesh, "geometry-" + str(s.id), str(self.id), [vert_src])
input_list = collada.source.InputList()
input_list.addInput(0, 'VERTEX', "#" + vert_src_name)
indices = numpy.array(range(0,(len(vertices) // 3)));
triset = geom.createTriangleSet(indices, input_list, "materialref" + str(s.id))
triset.generateNormals()
geom.primitives.append(triset)
return geom
Apparently you can actually compute it without normals, but the documentation was not clear. 显然,您实际上可以在没有法线的情况下进行计算,但是文档尚不清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.