def buildCompartmentsGeomRep(comp, collada_xml, root_node, mask=None): if comp.representation_file is None: return collada_xml, root_node nr = scene.Node(str(comp.name) + str("rep")) filename = autopack.retrieveFile(comp.representation_file, cache="geometries") #geometries gdic = helper.read(filename) for nid in gdic: matnode = oneMaterial(str(nid), collada_xml, color=gdic[nid]["color"]) master_node = colladaMesh(str(nid), gdic[nid]["mesh"][0], gdic[nid]["mesh"][1], gdic[nid]["mesh"][2], collada_xml, matnode=matnode) # master_node = colladaMesh(str(nid),comp.vertices,comp.vnormals,comp.faces,collada_xml,matnode=matnode) collada_xml.nodes.append(master_node) # mxmesh.setParent(nr) if len(gdic[nid]['instances']): geomnode = scene.NodeNode(master_node) # !n=scene.Node(str(nid)+str("instances")) # nri.setParent(nr) g = [] c = 0 for mat in gdic[nid]['instances']: geomnode = scene.NodeNode(master_node) # instance = scene.createInstancement(str(nid)+"_"+str(c),mxmesh) mat = numpy.array(mat, float) #.transpose() if helper.host == 'dejavu': #need to find the way that will work everywhere mry90 = helper.rotation_matrix(-math.pi / 2.0, [0.0, 1.0, 0.0]) #? mat = numpy.array(numpy.matrix(mat) * numpy.matrix(mry90)) # mat = numpy.array(mat).transpose() scale, shear, euler, translate, perspective = decompose_matrix( mat) p = translate #matrix[3,:3]/100.0#unit problem tr = scene.TranslateTransform(p[0], p[1], p[2]) rx = scene.RotateTransform(1, 0, 0, numpy.degrees(euler[0])) ry = scene.RotateTransform(0, 1, 0, numpy.degrees(euler[1])) rz = scene.RotateTransform(0, 0, 1, numpy.degrees(euler[2])) s = scene.ScaleTransform(scale[0], scale[1], scale[2]) ne = scene.Node(str(nid) + "_" + str(c), children=[ geomnode, ], transforms=[tr, rz, ry, rx, s]) #scene.MatrixTransform(matrix) g.append(ne) c += 1 node = scene.Node(str(nid) + str("instances"), children=g) nr.children.append(node) root_node.children.append(nr) return collada_xml, root_node
def buildRecipe(recipe, name, collada_xml, root_node, mask=None): if recipe is None: return collada_xml, root_node n = scene.Node(str(name)) for ingr in recipe.ingredients: #for each ingredient #build the material matnode = oneMaterial(ingr.name, collada_xml) #build a geomedtry node master_node = buildIngredientGeom(ingr, collada_xml, matnode) collada_xml.nodes.append(master_node) #build the scene instance node c = 0 g = [] for pos, rot in ingr.results: #[pos,rot] if mask != None: if testOnePoints(pos, mask): continue geomnode = scene.NodeNode(master_node) mat = rot.copy() mat[:3, 3] = pos if helper.host == 'dejavu': #and SWAPZ:#need to find the way that will work everywhere mry90 = helper.rotation_matrix(-math.pi / 2.0, [0.0, 1.0, 0.0]) #? mat = numpy.array(numpy.matrix(mat) * numpy.matrix(mry90)) # mat = numpy.array(mat).transpose() scale, shear, euler, translate, perspective = decompose_matrix(mat) p = pos #matrix[3,:3]/100.0#unit problem tr = scene.TranslateTransform(p[0], p[1], p[2]) rx = scene.RotateTransform(1, 0, 0, numpy.degrees(euler[0])) ry = scene.RotateTransform(0, 1, 0, numpy.degrees(euler[1])) rz = scene.RotateTransform(0, 0, 1, numpy.degrees(euler[2])) s = scene.ScaleTransform(scale[0], scale[1], scale[2]) #n = scene.NodeNode(master_node,transforms=[tr,rz,ry,rx,s]) # gnode = scene.Node(self.getName(c)+"_inst", children=[geomnode,]) ne = scene.Node(ingr.o_name + "_" + str(c), children=[ geomnode, ], transforms=[tr, rz, ry, rx, s]) #scene.MatrixTransform(matrix) g.append(ne) c += 1 node = scene.Node(ingr.o_name, children=g) n.children.append(node) root_node.children.append(n) return collada_xml, root_node
def instancesToCollada(self, parent_object, collada_xml=None, instance_node=True, **kw): try: from upy.transformation import decompose_matrix from collada import Collada from collada import material from collada import source from collada import geometry from collada import scene except: return inst_parent = parent_object #self.getCurrentSelection()[0] ch = self.getChilds(inst_parent) transpose = True if "transpose" in kw: transpose = kw["transpose"] #instance master if "mesh" in kw and kw["mesh"] is not None: inst_master = kw["mesh"] f, v, vn = self.DecomposeMesh(kw["mesh"], edit=False, copy=False, tri=True, transform=False) else: inst_master = self.getMasterInstance(ch[0]) print "master is ", inst_master #grabb v,f,n of inst_master f, v, vn = self.DecomposeMesh(inst_master, edit=False, copy=False, tri=True, transform=False) #special case when come from x-z swap # v=[[vv[2],vv[1],vv[0]] for vv in v] # go back to regular #-90degree rotation onY mry90 = self.rotation_matrix(-math.pi / 2.0, [0.0, 1.0, 0.0]) #? v = self.ApplyMatrix(v, mry90) #same for the normal? vn = self.ApplyMatrix(vn, mry90) #same for the normal? iname = self.getName(inst_master) pname = self.getName(inst_parent) if collada_xml is None: collada_xml = Collada() collada_xml.assetInfo.unitname = "centimeter" collada_xml.assetInfo.unitmeter = 0.01 mat = self.getMaterialObject(inst_master) if len(mat): mat = mat[0] props = self.getMaterialProperty(mat, color=1) #,specular_color=1) print "colors is ", props effect = material.Effect( "effect" + iname, [], "phong", diffuse=[props["color"][0], props["color"][1], props["color"][2], 1.0]) # specular = props["specular_color"]) mat = material.Material("material" + iname, iname + "material", effect) collada_xml.effects.append(effect) collada_xml.materials.append(mat) matnode = scene.MaterialNode(iname + "material" + "ref", mat, inputs=[]) #the geom #invert Z ? for C4D? vertzyx = numpy.array(v) #* numpy.array([1,1,-1]) z, y, x = vertzyx.transpose() vertxyz = numpy.vstack([x, y, z]).transpose() #* numpy.array([1,1,-1]) vert_src = source.FloatSource(iname + "_verts-array", vertxyz.flatten(), ('X', 'Y', 'Z')) norzyx = numpy.array(vn) nz, ny, nx = norzyx.transpose() norxyz = numpy.vstack([nx, ny, nz]).transpose() * numpy.array([1, 1, -1]) normal_src = source.FloatSource(iname + "_normals-array", norxyz.flatten(), ('X', 'Y', 'Z')) geom = geometry.Geometry(collada_xml, "geometry" + iname, iname, [vert_src, normal_src]) # normal_src]) input_list = source.InputList() input_list.addInput(0, 'VERTEX', "#" + iname + "_verts-array") input_list.addInput(0, 'NORMAL', "#" + iname + "_normals-array") #invert all the face fi = numpy.array(f, int) #[:,::-1] triset = geom.createTriangleSet(fi.flatten(), input_list, iname + "materialref") geom.primitives.append(triset) collada_xml.geometries.append(geom) #the noe #instance here ? #creae the instance maser node : if instance_node: master_geomnode = scene.GeometryNode(geom, [matnode]) #doesn work ? master_node = scene.Node("node_" + iname, children=[ master_geomnode, ]) #,transforms=[tr,rz,ry,rx,s]) g = [] for c in ch: #collada.scene.NodeNode if instance_node: geomnode = scene.NodeNode(master_node) else: geomnode = scene.GeometryNode(geom, [matnode]) matrix = self.ToMat( self.getTransformation(c)) #.transpose()#.flatten() if transpose: matrix = numpy.array(matrix).transpose() scale, shear, euler, translate, perspective = decompose_matrix(matrix) scale = self.getScale(c) p = translate #matrix[3,:3]/100.0#unit problem tr = scene.TranslateTransform(p[0], p[1], p[2]) rx = scene.RotateTransform(1, 0, 0, numpy.degrees(euler[0])) ry = scene.RotateTransform(0, 1, 0, numpy.degrees(euler[1])) rz = scene.RotateTransform(0, 0, 1, numpy.degrees(euler[2])) # rx=scene.RotateTransform(-1,0,0,numpy.degrees(euler[0])) # ry=scene.RotateTransform(0,-1,0,numpy.degrees(euler[1])) # rz=scene.RotateTransform(0,0,1,numpy.degrees(euler[2])) s = scene.ScaleTransform(scale[0], scale[1], scale[2]) #n = scene.NodeNode(master_node,transforms=[tr,rz,ry,rx,s]) # gnode = scene.Node(self.getName(c)+"_inst", children=[geomnode,]) n = scene.Node( self.getName(c), children=[ geomnode, ], transforms=[tr, rz, ry, rx, s] ) #scene.MatrixTransform(matrix)[scene.MatrixTransform(numpy.array(matrix).reshape(16,))] # n = scene.Node(self.getName(c), children=[geomnode,], # transforms=[scene.MatrixTransform(numpy.array(matrix).reshape(16,))]) #scene.MatrixTransform(matrix)[scene.MatrixTransform(numpy.array(matrix).reshape(16,))] g.append(n) node = scene.Node( pname, children=g) #,transforms=[scene.RotateTransform(0,1,0,90.0)]) if "parent_node" in kw: kw["parent_node"].children.append(node) node = kw["parent_node"] if not len(collada_xml.scenes): myscene = scene.Scene("myscene", [node]) collada_xml.scenes.append(myscene) collada_xml.scene = myscene else: if "parent_node" not in kw: collada_xml.scene.nodes.append(node) if instance_node: collada_xml.nodes.append(master_node) return collada_xml
def buildRecipe(recipe, name, scene, root_node, mask=None): if recipe is None: return scene, root_node nr = scene.createNullObject(str(name)) nr.setParent(root_node) # n=scene.Node(str(name)) for ingr in recipe.ingredients: #for each ingredient #build the material matnode = oneMaterial(ingr.o_name, scene, color=ingr.color) # print "material",matnode #build a geomedtry node # master_node=buildIngredientGeom(ingr,scene,matnode) master_node = buildIngredientParticles(ingr, scene, matnode) if master_node is None: continue #hide it master_node.setHideToCamera(True) master_node.setParent(nr) # master_node,scene=readOne(ingr.meshFile,ingr.meshName,scene) # print "master_node",type(master_node),master_node # master_node = scene.addObject(master_node); # print "master_node",master_node #apply scatter/cloner? n = scene.createNullObject(str(ingr.o_name)) n.setParent(nr) # collada_xml.nodes.append(master_node) #build the scene instance node c = 0 g = [] for pos, rot in ingr.results: #[pos,rot] # print c,ingr.o_name+"_"+str(c),master_node #test if in bb ? if mask != None: if testOnePoints(pos, mask): continue #can we do it using a particle cloud instance = scene.createInstancement( str(ingr.o_name) + "_" + str(c), master_node) mat = rot.copy() mat[:3, 3] = pos # if helper.host == 'dejavu':#need to find the way that will work everywhere # mry90 = helper.rotation_matrix(-math.pi/2.0, [0.0,1.0,0.0])#? # mat = numpy.array(numpy.matrix(mat)*numpy.matrix(mry90)) # mat = numpy.array(mat).transpose() scale, shear, euler, translate, perspective = decompose_matrix(mat) #instance.setParent(n) #instance.setPosition(Cvector(pos[0],pos[1],pos[2])) base, pivot, ok = instance.getBaseAndPivot() base.origin = Cvector(pos[0], pos[1], pos[2]) #pivot = Cvector(euler[0],euler[1],euler[2]) mat = numpy.array(mat).transpose() # print ("tr,eiler",translate,euler,mat) base.xAxis = Cvector(mat[0][0], mat[0][1], mat[0][2]) base.yAxis = Cvector(mat[1][0], mat[1][1], mat[1][2]) base.zAxis = Cvector(mat[2][0], mat[2][1], mat[2][2]) instance.setBaseAndPivot(base, pivot) #instance.setRotation(Cvector(euler[0],euler[1],euler[2])) # Scale instance #base.xAxis.scale(RADIUS[name[i]]*2*SCALE); #base.yAxis.scale(RADIUS[name[i]]*2*SCALE); #base.zAxis.scale(RADIUS[name[i]]*2*SCALE); # Apply translation and scale # instance.setBaseAndPivot(base,pivot); instance.setParent(n) c += 1 # print instance # break return scene, root_node
def buildCompartmentsGeom(comp, scene, parent=None): if comp.representation_file is None: return nr = scene.createNullObject(str(comp.name) + str("rep")) if parent is not None: nr.setParent(parent) filename = autopack.retrieveFile(comp.representation_file, cache="geoms") #geometries gdic = helper.read(filename) for nid in gdic: matnode = oneMaterial(str(nid), scene, color=gdic[nid]["color"]) mxmesh = maxwellMesh(str(nid), gdic[nid]["mesh"][0], gdic[nid]["mesh"][1], gdic[nid]["mesh"][2], scene, matnode=matnode) mxmesh.setParent(nr) if len(gdic[nid]['instances']): nri = scene.createNullObject(str(nid) + str("instances")) nri.setParent(nr) c = 0 for mat in gdic[nid]['instances']: instance = scene.createInstancement( str(nid) + "_" + str(c), mxmesh) mat = numpy.array(mat, float).transpose() scale, shear, euler, translate, perspective = decompose_matrix( mat) base, pivot, ok = instance.getBaseAndPivot() base.origin = Cvector(float(mat[3][0]), float(mat[3][1]), float(mat[3][2])) mat[3, :3] = [0., 0., 0.] # mat = numpy.array(mat,float).transpose() newaxis = helper.ApplyMatrix( [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], mat) base.xAxis = Cvector(float(newaxis[0][0]), float(newaxis[0][1]), float(newaxis[0][2])) base.yAxis = Cvector(float(newaxis[1][0]), float(newaxis[1][1]), float(newaxis[1][2])) base.zAxis = Cvector(float(newaxis[2][0]), float(newaxis[2][1]), float(newaxis[2][2])) # base.xAxis=Cvector(float(mat[0][0]),float(mat[1][0]),float(mat[2][0])) # base.yAxis=Cvector(float(mat[0][1]),float(mat[1][1]),float(mat[2][1])) # base.zAxis=Cvector(float(mat[0][2]),float(mat[1][2]),float(mat[2][2]))\ # base.xAxis=Cvector(float(mat[0][0]),float(mat[0][1]),float(mat[0][2])) # base.yAxis=Cvector(float(mat[1][0]),float(mat[1][1]),float(mat[1][2])) # base.zAxis=Cvector(float(mat[2][0]),float(mat[2][1]),float(mat[2][2])) base.xAxis.normalize() base.yAxis.normalize() base.zAxis.normalize() # base.origin = Cvector(float(mat[0][3]),float(mat[1][3]),float(mat[2][3])) # print ("X",mat[0][0],mat[0][1],mat[0][2],base.xAxis.x(),base.xAxis.y(),base.xAxis.z()) # print ("Y",mat[1][0],mat[1][1],mat[1][2],base.yAxis.x(),base.yAxis.y(),base.yAxis.z()) # print ("Z",mat[2][0],mat[2][1],mat[2][2],base.zAxis.x(),base.zAxis.y(),base.zAxis.z()) # print ("base",mat[3][0],mat[3][1],mat[3][2],base.origin.x(),base.origin.y(),base.origin.z()) instance.setBaseAndPivot(base, base) # pos=Cvector(float(mat[3][0]),float(mat[3][1]),float(mat[3][2])) # eul=Cvector(float(euler[0]),float(euler[1]),float(euler[2])) # instance.setPosition(pos) # instance.setRotation(eul) # instance.setParent(nri) # wt=instance.getWorldTransform()[0] print str(nid) + "_" + str(c), mat # print wt.origin.x(),wt.origin.y(),wt.origin.z(),float(mat[3][0]),float(mat[3][1]),float(mat[3][2]) # print wt.xAxis.x(),wt.xAxis.y(),wt.xAxis.z() # print wt.yAxis.x(),wt.yAxis.y(),wt.yAxis.z() # print wt.zAxis.x(),wt.zAxis.y(),wt.zAxis.z() # print instance.getPosition()[0] # print instance.getRotation()[0] # base,pivot,ok = instance.getBaseAndPivot(); if c == 5: # print ("X",mat,base.xAxis.x(),base.xAxis.y(),base.xAxis.z()) break c += 1
def instancesToCollada(self,parent_object,collada_xml=None,instance_node=True,**kw): try : from upy.transformation import decompose_matrix from collada import Collada from collada import material from collada import source from collada import geometry from collada import scene except : return inst_parent=parent_object#self.getCurrentSelection()[0] ch=self.getChilds(inst_parent) transpose=True if "transpose" in kw : transpose = kw["transpose"] #instance master if "mesh" in kw and kw["mesh"] is not None: inst_master = kw["mesh"] f,v,vn = self.DecomposeMesh(kw["mesh"],edit=False,copy=False,tri=True, transform=False) else : inst_master = self.getMasterInstance(ch[0]) print "master is ",inst_master #grabb v,f,n of inst_master f,v,vn = self.DecomposeMesh(inst_master,edit=False,copy=False,tri=True, transform=False) #special case when come from x-z swap # v=[[vv[2],vv[1],vv[0]] for vv in v] # go back to regular #-90degree rotation onY mry90 = self.rotation_matrix(-math.pi/2.0, [0.0,1.0,0.0])#? v=self.ApplyMatrix(v,mry90)#same for the normal? vn=self.ApplyMatrix(vn,mry90)#same for the normal? iname = self.getName( inst_master ) pname = self.getName( inst_parent ) if collada_xml is None: collada_xml = Collada() collada_xml.assetInfo.unitname="centimeter" collada_xml.assetInfo.unitmeter=0.01 mat = self.getMaterialObject(inst_master) if len(mat) : mat = mat[0] props = self.getMaterialProperty(mat,color=1)#,specular_color=1) print "colors is ",props effect = material.Effect("effect"+iname, [], "phong", diffuse=[props["color"][0],props["color"][1],props["color"][2],1.0]) # specular = props["specular_color"]) mat = material.Material("material"+iname, iname+"material", effect) collada_xml.effects.append(effect) collada_xml.materials.append(mat) matnode = scene.MaterialNode(iname+"material"+"ref", mat, inputs=[]) #the geom #invert Z ? for C4D? vertzyx = numpy.array(v)#* numpy.array([1,1,-1]) z,y,x=vertzyx.transpose() vertxyz = numpy.vstack([x,y,z]).transpose()#* numpy.array([1,1,-1]) vert_src = source.FloatSource(iname+"_verts-array", vertxyz.flatten(), ('X', 'Y', 'Z')) norzyx=numpy.array(vn) nz,ny,nx=norzyx.transpose() norxyz = numpy.vstack([nx,ny,nz]).transpose()* numpy.array([1,1,-1]) normal_src = source.FloatSource(iname+"_normals-array", norxyz.flatten(), ('X', 'Y', 'Z')) geom = geometry.Geometry(collada_xml, "geometry"+iname, iname, [vert_src,normal_src])# normal_src]) input_list = source.InputList() input_list.addInput(0, 'VERTEX', "#"+iname+"_verts-array") input_list.addInput(0, 'NORMAL', "#"+iname+"_normals-array") #invert all the face fi=numpy.array(f,int)#[:,::-1] triset = geom.createTriangleSet(fi.flatten(), input_list, iname+"materialref") geom.primitives.append(triset) collada_xml.geometries.append(geom) #the noe #instance here ? #creae the instance maser node : if instance_node: master_geomnode = scene.GeometryNode(geom, [matnode])#doesn work ? master_node = scene.Node("node_"+iname, children=[master_geomnode,])#,transforms=[tr,rz,ry,rx,s]) g=[] for c in ch : #collada.scene.NodeNode if instance_node: geomnode = scene.NodeNode(master_node) else : geomnode = scene.GeometryNode(geom, [matnode]) matrix = self.ToMat(self.getTransformation(c))#.transpose()#.flatten() if transpose: matrix = numpy.array(matrix).transpose() scale, shear, euler, translate, perspective=decompose_matrix(matrix) scale = self.getScale(c) p=translate#matrix[3,:3]/100.0#unit problem tr=scene.TranslateTransform(p[0],p[1],p[2]) rx=scene.RotateTransform(1,0,0,numpy.degrees(euler[0])) ry=scene.RotateTransform(0,1,0,numpy.degrees(euler[1])) rz=scene.RotateTransform(0,0,1,numpy.degrees(euler[2])) # rx=scene.RotateTransform(-1,0,0,numpy.degrees(euler[0])) # ry=scene.RotateTransform(0,-1,0,numpy.degrees(euler[1])) # rz=scene.RotateTransform(0,0,1,numpy.degrees(euler[2])) s=scene.ScaleTransform(scale[0],scale[1],scale[2]) #n = scene.NodeNode(master_node,transforms=[tr,rz,ry,rx,s]) # gnode = scene.Node(self.getName(c)+"_inst", children=[geomnode,]) n = scene.Node(self.getName(c), children=[geomnode,],transforms=[tr,rz,ry,rx,s]) #scene.MatrixTransform(matrix)[scene.MatrixTransform(numpy.array(matrix).reshape(16,))] # n = scene.Node(self.getName(c), children=[geomnode,], # transforms=[scene.MatrixTransform(numpy.array(matrix).reshape(16,))]) #scene.MatrixTransform(matrix)[scene.MatrixTransform(numpy.array(matrix).reshape(16,))] g.append(n) node = scene.Node(pname, children=g)#,transforms=[scene.RotateTransform(0,1,0,90.0)]) if "parent_node" in kw : kw["parent_node"].children.append(node) node = kw["parent_node"] if not len(collada_xml.scenes) : myscene = scene.Scene("myscene", [node]) collada_xml.scenes.append(myscene) collada_xml.scene = myscene else : if "parent_node" not in kw : collada_xml.scene.nodes.append(node) if instance_node: collada_xml.nodes.append(master_node) return collada_xml