def analyzeTarget(obj, targetPath): """ This function is used (more during the development cycle than in the runtime application) to analyze the difference between the vertex positions of a mesh object and those recorded in a file on disk. The result is a representation of each vertex displacement as a color. This provides a graphical representation of the deformations with respect to the original positions as defined by the mesh object. Plugin developers may find this function useful for visualizing and checking integration plugins. Parameters ---------- obj: *3D object*. The object possessing the 'original' vertex positions. targetPath: *string*. The file system path to the file containing the target vertex positions. """ try: fileDescriptor = open(targetPath) except: print 'Unable to open %s'%(targetPath) return 0 targetData = fileDescriptor.readlines() distMax = 0.00001 # Step 1: calculate max length for vData in targetData: vectorData = vData.split() targetsVector = [float(vectorData[1]), float(vectorData[2]), float(vectorData[3])] dist = aljabr.vlen(targetsVector) if dist > distMax: distMax = dist # Step 2: calculate color for vData in targetData: vectorData = vData.split() mainPointIndex = int(vectorData[0]) targetsVector = [float(vectorData[1]), float(vectorData[2]), float(vectorData[3])] dist = aljabr.vlen(targetsVector) v = obj.verts[mainPointIndex] if dist == 0: v.color = [255, 255, 255, 255] else: R = int((dist / distMax) * 255) G = 255 - int(R / 10) B = 255 - int(R / 10) v.color = [R, G, B, 255] v.update(0, 0, 1) fileDescriptor.close()
def __updatePrism(self, mesh, o, e, index, p): dir = aljabr.vsub(e, o) # direction vector from o to e len = aljabr.vlen(dir) # distance from o to e scale = 0.5 # the thickness is 10% of the length i = aljabr.vadd(o, aljabr.vmul(dir, 0.25)) # the thickest part is 25% from o n = aljabr.vmul(dir, 1.0 / len) # the normalized direction q = aljabr.axisAngleToQuaternion(n, pi / 2.0) # a quaternion to rotate the point p1 to obtain the other points p1 = p # a random point in the plane defined by 0,0,0 and n p1 = aljabr.vmul(aljabr.vnorm(p1), scale) # the point scaled to the thickness p2 = aljabr.quaternionVectorTransform(q, p1) # the other points p3 = aljabr.quaternionVectorTransform(q, p2) p4 = aljabr.quaternionVectorTransform(q, p3) p1 = aljabr.vadd(i, p1) # translate by i since we were working in the origin p2 = aljabr.vadd(i, p2) p3 = aljabr.vadd(i, p3) p4 = aljabr.vadd(i, p4) # The 6 vertices mesh.verts[index].co = o mesh.verts[index+1].co = p1 mesh.verts[index+2].co = p2 mesh.verts[index+3].co = p3 mesh.verts[index+4].co = p4 mesh.verts[index+5].co = e
def printProxyShape(fp, shapes): shapes.sort() pv = -1 while shapes: (pv0, dx0, dy0, dz0) = shapes.pop() if pv0 == pv: dx += dx0 dy += dy0 dz += dz0 else: if (pv >= 0 and aljabr.vlen([dx, dy, dz]) > 0): fp.write(" sv %d %.4f %.4f %.4f ;\n" % (pv, dx, dy, dz)) (pv, dx, dy, dz) = (pv0, dx0, dy0, dz0) if (pv >= 0 and aljabr.vlen([dx, dy, dz]) > 0): fp.write(" sv %d %.4f %.4f %.4f ;\n" % (pv, dx, dy, dz)) return
def printProxyShape(fp, shapes): shapes.sort() pv = -1 while shapes: (pv0, dx0, dy0, dz0) = shapes.pop() if pv0 == pv: dx += dx0 dy += dy0 dz += dz0 else: if (pv >= 0 and aljabr.vlen([dx,dy,dz]) > 0): fp.write(" sv %d %.4f %.4f %.4f ;\n" % (pv, dx, dy, dz)) (pv, dx, dy, dz) = (pv0, dx0, dy0, dz0) if (pv >= 0 and aljabr.vlen([dx,dy,dz]) > 0): fp.write(" sv %d %.4f %.4f %.4f ;\n" % (pv, dx, dy, dz)) return
def updatePrism(mesh, o, e, index, p): dir = aljabr.vsub(e, o) # direction vector from o to e if dir == [0.0, 0.0, 0.0]: dir = [0.0, 1.0, 0.0] len = aljabr.vlen(dir) # distance from o to e if len == 0: len = 1 scale = 0.5 # the thickness is 10% of the length i = aljabr.vadd(o, aljabr.vmul(dir, 0.25)) # the thickest part is 25% from o n = aljabr.vmul(dir, 1.0 / len) # the normalized direction q = aljabr.axisAngleToQuaternion( n, pi / 2.0) # a quaternion to rotate the point p1 to obtain the other points p1 = p # a random point in the plane defined by 0,0,0 and n p1 = aljabr.vmul(aljabr.vnorm(p1), scale) # the point scaled to the thickness p2 = aljabr.quaternionVectorTransform(q, p1) # the other points p3 = aljabr.quaternionVectorTransform(q, p2) p4 = aljabr.quaternionVectorTransform(q, p3) p1 = aljabr.vadd(i, p1) # translate by i since we were working in the origin p2 = aljabr.vadd(i, p2) p3 = aljabr.vadd(i, p3) p4 = aljabr.vadd(i, p4) # The 6 vertices mesh.verts[index].co = o mesh.verts[index + 1].co = p1 mesh.verts[index + 2].co = p2 mesh.verts[index + 3].co = p3 mesh.verts[index + 4].co = p4 mesh.verts[index + 5].co = e
def addPrism(mesh, o=[0.0, 0.0, 0.0], e=[0.0, 1.0, 0.0], name='prism'): fg = mesh.createFaceGroup(name) dir = aljabr.vsub(e, o) # direction vector from o to e if dir == [0.0, 0.0, 0.0]: dir = [0.0, 1.0, 0.0] len = aljabr.vlen(dir) # distance from o to e if len == 0: len = 1 scale = 0.5 # the thickness is 10% of the length i = aljabr.vadd(o, aljabr.vmul(dir, 0.25)) # the thickest part is 25% from o n = aljabr.vmul(dir, 1.0 / len) # the normalized direction q = aljabr.axisAngleToQuaternion( n, pi / 2.0) # a quaternion to rotate the point p1 to obtain the other points p = p1 = aljabr.randomPointFromNormal( n) # a random point in the plane defined by 0,0,0 and n p1 = aljabr.vmul(aljabr.vnorm(p1), scale) # the point scaled to the thickness p2 = aljabr.quaternionVectorTransform(q, p1) # the other points p3 = aljabr.quaternionVectorTransform(q, p2) p4 = aljabr.quaternionVectorTransform(q, p3) p1 = aljabr.vadd(i, p1) # translate by i since we were working in the origin p2 = aljabr.vadd(i, p2) p3 = aljabr.vadd(i, p3) p4 = aljabr.vadd(i, p4) # The 6 vertices v = [] v.append(mesh.createVertex(o)) # 0 0 v.append(mesh.createVertex(p1)) # 1 /|\ v.append(mesh.createVertex(p2)) # 2 /.2.\ v.append(mesh.createVertex(p3)) # 3 1` | `3 v.append(mesh.createVertex(p4)) # 4 \`.4.`/ v.append(mesh.createVertex(e)) # 5 \ | / # \|/ # 5 # The 8 faces fg.createFace((v[0], v[1], v[4], v[0])) fg.createFace((v[0], v[4], v[3], v[0])) fg.createFace((v[0], v[3], v[2], v[0])) fg.createFace((v[0], v[2], v[1], v[0])) fg.createFace((v[5], v[4], v[1], v[5])) fg.createFace((v[5], v[1], v[2], v[5])) fg.createFace((v[5], v[2], v[3], v[5])) fg.createFace((v[5], v[3], v[4], v[5])) return p
def translLenght(path): try: fileDescriptor = open(path) except: print 'Error opening %s file' % path failedToOpen.append(path) return fullTransl = 0 data = fileDescriptor.readlines() for targetData in data: vectorData = targetData.split() if len(vectorData) > 0: vector = [float(vectorData[1]),float(vectorData[2]),float(vectorData[3])] fullTransl += aljabr.vlen(vector) fileDescriptor.close() return fullTransl
def addPrism(mesh, o=[0.0, 0.0, 0.0], e=[0.0, 1.0, 0.0], name='prism'): fg = mesh.createFaceGroup(name) dir = aljabr.vsub(e, o) # direction vector from o to e if dir == [0.0, 0.0, 0.0]: dir = [0.0, 1.0, 0.0] len = aljabr.vlen(dir) # distance from o to e if len == 0: len = 1 scale = 0.5 # the thickness is 10% of the length i = aljabr.vadd(o, aljabr.vmul(dir, 0.25)) # the thickest part is 25% from o n = aljabr.vmul(dir, 1.0 / len) # the normalized direction q = aljabr.axisAngleToQuaternion(n, pi / 2.0) # a quaternion to rotate the point p1 to obtain the other points p = p1 = aljabr.randomPointFromNormal(n) # a random point in the plane defined by 0,0,0 and n p1 = aljabr.vmul(aljabr.vnorm(p1), scale) # the point scaled to the thickness p2 = aljabr.quaternionVectorTransform(q, p1) # the other points p3 = aljabr.quaternionVectorTransform(q, p2) p4 = aljabr.quaternionVectorTransform(q, p3) p1 = aljabr.vadd(i, p1) # translate by i since we were working in the origin p2 = aljabr.vadd(i, p2) p3 = aljabr.vadd(i, p3) p4 = aljabr.vadd(i, p4) # The 6 vertices v = [] v.append(mesh.createVertex(o)) # 0 0 v.append(mesh.createVertex(p1)) # 1 /|\ v.append(mesh.createVertex(p2)) # 2 /.2.\ v.append(mesh.createVertex(p3)) # 3 1` | `3 v.append(mesh.createVertex(p4)) # 4 \`.4.`/ v.append(mesh.createVertex(e)) # 5 \ | / # \|/ # 5 # The 8 faces fg.createFace((v[0], v[1], v[4], v[0])) fg.createFace((v[0], v[4], v[3], v[0])) fg.createFace((v[0], v[3], v[2], v[0])) fg.createFace((v[0], v[2], v[1], v[0])) fg.createFace((v[5], v[4], v[1], v[5])) fg.createFace((v[5], v[1], v[2], v[5])) fg.createFace((v[5], v[2], v[3], v[5])) fg.createFace((v[5], v[3], v[4], v[5])) return p
def chooseTraslSamples(self): direction = aljabr.vnorm(self.angle) similarity = {} if self.angle != [0.0,0.0,0.0]: for sample in self.examplesTrasl: direction2 = aljabr.vnorm(sample) sampleDistance1 = aljabr.vdist(direction,direction2) sampleDistance2 = math.fabs(aljabr.vlen(aljabr.vsub(self.angle,sample))) similarity[sampleDistance1+sampleDistance2] = sample d = similarity.keys() d.sort() nearestSample1 = similarity[d[0]] nearestSample2 = similarity[d[1]] nearestSample3 = similarity[d[2]] factor1,factor2,factor3 = self.equalize(d[0],d[1],d[2]) return (nearestSample1,nearestSample2,nearestSample3,factor1,factor2,factor3) else: return ([0,0,0],[0,0,0],[0,0,0],0,0,0)
def _fromBvhJoint(genericSkeletonJoint, joint): if not joint.parent: # Replace the generic skeleton root bone newJoint = genericSkeletonJoint newJoint.roll = 0 newJoint.name = joint.name else: # Create a new child bone newJoint = genericSkeletonJoint.addJoint(joint.parent.name, 0) #newJoint.position = joint.getPosition() # absolute position newJoint.length = vlen(joint.offset) newJoint.offset = joint.offset # position relative to parent newJoint.roll = 0 # Copy animation data for frame in joint.frames: Rxyz = [0.0, 0.0, 0.0] Txyz = [0.0, 0.0, 0.0] rOrder = "" for index, channel in enumerate(joint.channels): if channel == 'Xposition': Txyz[0] = frame[index] elif channel == 'Yposition': Txyz[1] = frame[index] elif channel == 'Zposition': Txyz[2] = frame[index] # Rotation channels are swapped to proper XYZ order if channel == 'Xrotation': Rxyz[0] = frame[index]# * degree2rad rOrder = rOrder + "x" elif channel == 'Yrotation': Rxyz[1] = frame[index]# * degree2rad rOrder = rOrder + "y" elif channel == 'Zrotation': Rxyz[2] = frame[index]# * degree2rad rOrder = rOrder + "z" newJoint.addFrame(Rxyz, Txyz, rOrder) # Recurse over children for child in joint.children: _fromBvhJoint(newJoint, child)
def _fromBvhJoint(genericSkeletonJoint, joint): if not joint.parent: # Replace the generic skeleton root bone newJoint = genericSkeletonJoint newJoint.roll = 0 newJoint.name = joint.name else: # Create a new child bone newJoint = genericSkeletonJoint.addJoint(joint.parent.name, 0) #newJoint.position = joint.getPosition() # absolute position newJoint.length = vlen(joint.offset) newJoint.offset = joint.offset # position relative to parent newJoint.roll = 0 # Copy animation data for frame in joint.frames: Rxyz = [0.0, 0.0, 0.0] Txyz = [0.0, 0.0, 0.0] rOrder = "" for index, channel in enumerate(joint.channels): if channel == 'Xposition': Txyz[0] = frame[index] elif channel == 'Yposition': Txyz[1] = frame[index] elif channel == 'Zposition': Txyz[2] = frame[index] # Rotation channels are swapped to proper XYZ order if channel == 'Xrotation': Rxyz[0] = frame[index] # * degree2rad rOrder = rOrder + "x" elif channel == 'Yrotation': Rxyz[1] = frame[index] # * degree2rad rOrder = rOrder + "y" elif channel == 'Zrotation': Rxyz[2] = frame[index] # * degree2rad rOrder = rOrder + "z" newJoint.addFrame(Rxyz, Txyz, rOrder) # Recurse over children for child in joint.children: _fromBvhJoint(newJoint, child)
def translLenght(path): try: fileDescriptor = open(path) except: print 'Error opening %s file' % path failedToOpen.append(path) return fullTransl = 0 data = fileDescriptor.readlines() for targetData in data: vectorData = targetData.split() if len(vectorData) > 0: vector = [ float(vectorData[1]), float(vectorData[2]), float(vectorData[3]) ] fullTransl += aljabr.vlen(vector) fileDescriptor.close() return fullTransl
def setShapeScale(words): key = words[1] scales = None try: scales = rig_panel_25.FaceShapeKeyScale[key] except: pass try: scales = rig_body_25.BodyShapeKeyScale[key] except: pass if not scales: raise NameError("No scale for %s" % key) (p1, p2, length0) = scales x1 = the.Locations[p1] x2 = the.Locations[p2] dist = aljabr.vsub(x1, x2) length = aljabr.vlen(dist) scale = length / length0 #print("Scale %s %f %f" % (key, length, scale)) return scale
def setShapeScale(words): key = words[1] scales = None try: scales = rig_panel_25.FaceShapeKeyScale[key] except: pass try: scales = rig_body_25.BodyShapeKeyScale[key] except: pass if not scales: raise NameError("No scale for %s" % key) (p1, p2, length0) = scales x1 = the.Locations[p1] x2 = the.Locations[p2] dist = aljabr.vsub(x1, x2) length = aljabr.vlen(dist) scale = length/length0 #print("Scale %s %f %f" % (key, length, scale)) return scale
def onShow(self, event): gui3d.TaskView.onShow(self, event) human = gui3d.app.selectedHuman human.hide() if not self.mesh: self.mesh = module3d.Object3D('texture_tool') self.mesh.uvValues = [] self.mesh.indexBuffer = [] # create group fg = self.mesh.createFaceGroup('texture_tool') self.mesh.area = 0.0 for f in human.mesh.faces: verts = [self.mesh.createVertex(v.co) for v in f.verts] uv = [human.mesh.uvValues[i] for i in f.uv] face = fg.createFace(verts, uv) face.area = vlen(vcross(vsub(verts[2].co, verts[0].co), vsub(verts[3].co, verts[1].co))) / 2.0 face.uvArea = vlen(vcross(vsub(uv[2] + [0.0], uv[0] + [0.0]), vsub(uv[3] + [0.0], uv[1] + [0.0]))) / 2.0 self.mesh.area += face.area for f in self.mesh.faces: index = min(510, max(0, int(f.uvArea*510.0/(f.area/self.mesh.area)))) if index > 255: f.setColor([255 - (index - 255), 255 - (index - 255), 255, 255]) else: f.setColor([255, index, index, 255]) self.mesh.texture = human.mesh.texture self.mesh.setCameraProjection(0) self.mesh.setShadeless(1) self.mesh.updateIndexBuffer() self.object = self.addObject(gui3d.Object([0, 0, 0], self.mesh, True)) else: self.mesh.area = 0.0 for f in self.mesh.faces: for i, v in enumerate(f.verts): v.co = human.mesh.faces[f.idx].verts[i].co[:] verts = f.verts f.area = vlen(vcross(vsub(verts[2].co, verts[0].co), vsub(verts[3].co, verts[1].co))) / 2.0 self.mesh.area += f.area for f in self.mesh.faces: index = min(510, max(0, int(f.uvArea*510.0/(f.area/self.mesh.area)))) if index > 255: f.setColor([255 - (index - 255), 255 - (index - 255), 255, 255]) else: f.setColor([255, index, index, 255]) self.mesh.update() if self.textured.selected: self.object.setTexture(human.mesh.texture)