How to make PyCollada output multiple meshes to the same scene?

435 views Asked by At

So I am using pyCollada to try to export multiple meshes to the same scene. Alas, whenever I try to do so, I can only see one of the meshes I have loaded in. Am I doing something wrong when I create the file? Each individual mesh renders perfectly if I separate them into their own file, but they fail when I attempt to output them to the same file. I have looked through the API, but the documentation is very limited. Any help would be appreciated.

My code is listed shown below.

# -*- 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_())
1

There are 1 answers

3
jterrace On BEST ANSWER

Your code to add the geometry to the scene is outside your inner loop. You're only adding the last geometry to the scene, rather than all of them. You should be creating multiple GeometryNode and adding all of them to the Scene.