如何使PyCollada输出多个网格到同一场景?

2024-09-27 22:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我使用pyCollada尝试将多个网格导出到同一个场景。唉,每当我尝试这样做,我只能看到一个网格我已加载。创建文件时我是否做错了什么?如果我将每个网格分离到它们自己的文件中,每个网格都能完美地渲染,但是当我试图将它们输出到同一个文件时,它们会失败。我已经看过API,但是文档非常有限。任何帮助都将不胜感激。在

我的代码如下所示。在

# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 14:43:05 2015

@author: skylion
"""

# -*- coding: utf-8 -*-

"""

Created on Thu Jun 11 11:01:48 2015


@author: danaukes

"""


import sys
import popupcad_deprecated
import popupcad_manufacturing_plugins
import popupcad

from popupcad.filetypes.design import Design

import PySide.QtGui as qg


#Draws Collada stuff
from collada import *
import numpy

geom_index = 0;

def exportBodyToMesh(output):
#    csg = output.csg    
    generic = output.generic_laminate()
#    layers = generic.layers()
    layerdef = d.return_layer_definition()
    layerdef.refreshzvalues()
#    layers = layerdef.layers
    mesh = Collada()
    nodes = []
    for layer in layerdef.layers:
        shapes = generic.geoms[layer]#TODO Add it in for other shapes         
        zvalue = layerdef.zvalue[layer]        
        height = zvalue * 1/ popupcad.internal_argument_scaling
        print zvalue
        if (len(shapes) == 0) : #In case there are no shapes.
            print "No shapes skipping"            
            continue
        print shapes
        for s in shapes:
            geom = createMeshFromShape(s, height, mesh)
            mesh.geometries.append(geom) 
        effect = material.Effect("effect" + str(geom_index), [], "phone", diffuse=(1,0,0), specular=(0,1,0))
        mat = material.Material("material" + str(geom_index), "mymaterial", effect)    
        matnode = scene.MaterialNode("materialref" + str(geom_index), mat, inputs=[])
        mesh.effects.append(effect)
        mesh.materials.append(mat)
        geomnode = scene.GeometryNode(geom, [matnode])
        node = scene.Node("node" + str(geom_index), children=[geomnode])    
        nodes.append(node)
    print nodes
    myscene = scene.Scene("myscene", nodes)
    mesh.scenes.append(myscene)
    mesh.scene = myscene
#    layer_num = layer_num + 1 #Add the layer thicknes instead of simply + 1    
    filename = str(output) + '.dae'
    mesh.write(filename)
    #TODO Add handling in case rigid body has already been selected.
    print filename + " has been saved"


def createMeshFromShape(s,layer_num, mesh):    
    s.exteriorpoints()
    a = s.triangles3()
    vertices = []
    global geom_index
    for coord in a: 
        for dec in coord:
            vertices.append(dec[0]) #x-axis
            vertices.append(dec[1]) #y-axis
            vertices.append(layer_num ) #z-axi

    #This scales the verticies properly.
    vert_floats = [x/popupcad.internal_argument_scaling for x in vertices] 
    vert_src = source.FloatSource("cubeverts-array" + str(geom_index), numpy.array(vert_floats), ('X', 'Y', 'Z'))
    geom = geometry.Geometry(mesh, "geometry" + str(geom_index), "mycube", [vert_src])    
    input_list = source.InputList()
    input_list.addInput(0, 'VERTEX', "#cubeverts-array" + str(geom_index))
    indices = numpy.array(range(0,(len(vertices) / 3)));    
    triset = geom.createTriangleSet(indices, input_list, "materialref")
    geom_index += 1        
    triset.generateNormals()    
    geom.primitives.append(triset)
    return geom

#Start of actual script
print sys.argv

app = qg.QApplication('exporter.py')

d = Design.open()

print "Loading..."

d.reprocessoperations()

operation = d.operations[3] #Identify bodies

for output in operation.output:
    exportBodyToMesh(output)

print "All objects printed"

#sys.exit(app.exec_())

Tags: inimportlayerforoutputindexlayersprint
1条回答
网友
1楼 · 发布于 2024-09-27 22:35:06

将几何体添加到场景的代码在内部循环之外。您只将最后一个几何体添加到场景中,而不是将它们全部添加到场景中。您应该创建多个GeometryNode,并将它们全部添加到Scene中。在

相关问题 更多 >

    热门问题