def setupRigJoint(words, obj, verts, locations): key = words[0] typ = words[1] if typ == 'joint': loc = mh2proxy.calcJointPos(obj, words[2]) locations[key] = loc elif typ == 'vertex': v = int(words[2]) locations[key] = verts[v].co elif typ == 'position': x = locations[words[2]] y = locations[words[3]] z = locations[words[4]] locations[key] = [x[0], y[1], z[2]] elif typ == 'line': k1 = float(words[2]) k2 = float(words[4]) locations[key] = vadd(vmul(locations[words[3]], k1), vmul(locations[words[5]], k2)) elif typ == 'offset': x = float(words[3]) y = float(words[4]) z = float(words[5]) locations[key] = vadd(locations[words[2]], [x, y, z]) elif typ == 'voffset': v = int(words[2]) x = float(words[3]) y = float(words[4]) z = float(words[5]) try: loc = verts[v].co except: loc = verts[v] locations[key] = vadd(loc, [x, y, z]) elif typ == 'front': raw = locations[words[2]] head = locations[words[3]] tail = locations[words[4]] offs = map(float, words[5].strip().lstrip('[').rstrip(']').split(',')) vec = aljabr.vsub(tail, head) vec2 = aljabr.vdot(vec, vec) vraw = aljabr.vsub(raw, head) x = aljabr.vdot(vec, vraw) / vec2 rvec = aljabr.vmul(vec, x) nloc = aljabr.vadd(head, rvec, offs) locations[key] = nloc else: raise NameError("Unknown %s" % typ)
def projectLighting(self): mesh = gui3d.app.selectedHuman.mesh mesh.setShadeless(1) dstImg = mh.Image(width=1024, height=1024, bitsPerPixel=24) dstW = dstImg.width dstH = dstImg.height for v in mesh.verts: ld = vnorm(vsub((-10.99, 20.0, 20.0,), v.co)) s = vdot(v.no, ld) s = max(0, min(255, int(s*255))) v.setColor([s, s, s, 255]) for g in mesh.faceGroups: if g.name.startswith("joint") or g.name.startswith("helper"): continue for f in g.faces: co = [(mesh.uvValues[i][0]*dstW, dstH-(mesh.uvValues[i][1]*dstH)) for i in f.uv] c = [v.color for v in f.verts] RasterizeTriangle(dstImg, co[0], co[1], co[2], ColorShader(c[:3])) RasterizeTriangle(dstImg, co[2], co[3], co[0], ColorShader((c[2], c[3], c[0]))) #dstImg.resize(128, 128); dstImg.save(os.path.join(mh.getPath(''), 'data', 'skins', 'lighting.png')) gui3d.app.selectedHuman.setTexture(os.path.join(mh.getPath(''), 'data', 'skins', 'lighting.png')) mesh.setColor([255, 255, 255, 255])
def setupRigJoint (words, obj, verts, locations): key = words[0] typ = words[1] if typ == 'joint': loc = mh2proxy.calcJointPos(obj, words[2]) locations[key] = loc elif typ == 'vertex': v = int(words[2]) locations[key] = verts[v].co elif typ == 'position': x = locations[words[2]] y = locations[words[3]] z = locations[words[4]] locations[key] = [x[0],y[1],z[2]] elif typ == 'line': k1 = float(words[2]) k2 = float(words[4]) locations[key] = vadd(vmul(locations[words[3]], k1), vmul(locations[words[5]], k2)) elif typ == 'offset': x = float(words[3]) y = float(words[4]) z = float(words[5]) locations[key] = vadd(locations[words[2]], [x,y,z]) elif typ == 'voffset': v = int(words[2]) x = float(words[3]) y = float(words[4]) z = float(words[5]) try: loc = verts[v].co except: loc = verts[v] locations[key] = vadd(loc, [x,y,z]) elif typ == 'front': raw = locations[words[2]] head = locations[words[3]] tail = locations[words[4]] offs = map(float, words[5].strip().lstrip('[').rstrip(']').split(',')) vec = aljabr.vsub(tail, head) vec2 = aljabr.vdot(vec, vec) vraw = aljabr.vsub(raw, head) x = aljabr.vdot(vec, vraw) / vec2 rvec = aljabr.vmul(vec, x) nloc = aljabr.vadd(head, rvec, offs) locations[key] = nloc else: raise NameError("Unknown %s" % typ)
def generateHairInterpolation2(self,guide1,guide2,humanMesh,isCollision,startIndex=9,gravity=True): if isCollision: octree = simpleoctree.SimpleOctree(humanMesh.getData().verts,0.08) hairName = "strand%s-%s"%(guide1.name,guide2.name) hSet = HairGroup(hairName) if len(guide1.controlPoints)>= len(guide2.controlPoints): longerGuide = guide1 shorterGuide = guide2 else: longerGuide = guide2 shorterGuide = guide1 nVerts = min([len(guide1.controlPoints),len(guide2.controlPoints)]) interpFactor = 0 vertsListToModify1 = [] vertsListToModify2 = [] for n in range (self.numberOfHairsMultiStrand): h = Hair() interpFactor += 1.0/self.numberOfHairsMultiStrand for i in range(len(longerGuide.controlPoints)): if random.random() < self.randomPercentage: xRand = self.sizeMultiStrand*random.random()*self.randomFactMultiStrand yRand = self.sizeMultiStrand*random.random()*self.randomFactMultiStrand zRand = self.sizeMultiStrand*random.random()*self.randomFactMultiStrand randomVect = [xRand,yRand,zRand] else: randomVect = [0,0,0] if i == 0: i2 = 0 if i == len(longerGuide.controlPoints)-1: i2 = len(shorterGuide.controlPoints)-1 else: i2 = int(round(i*len(shorterGuide.controlPoints)/len(longerGuide.controlPoints))) vert1 = longerGuide.controlPoints[i] vert2 = shorterGuide.controlPoints[i2] #Slerp dotProd = aljabr.vdot(aljabr.vnorm(vert1),aljabr.vnorm(vert2)) #Python has a very very bad numerical accuracy.. we need to do this for very small angle between guides #this occurs when we do collision detection if dotProd>1: angleBetweenGuides = 0.0 else: angleBetweenGuides = math.acos(aljabr.vdot(aljabr.vnorm(vert1),aljabr.vnorm(vert2))) denom = math.sin(angleBetweenGuides) if denom == 0.0: #controlpoints of some guides coincide vert1[0] = self.randomPercentage*self.sizeMultiStrand*random.random()*self.randomFactMultiStrand+vert1[0] vert1[1] = self.randomPercentage*self.sizeMultiStrand*random.random()*self.randomFactMultiStrand+vert1[1] vert1[2] = self.randomPercentage*self.sizeMultiStrand*random.random()*self.randomFactMultiStrand+vert1[2] vert1= aljabr.vadd(vert1,randomVect) angleBetweenGuides = math.acos(aljabr.vdot(aljabr.vnorm(vert1),aljabr.vnorm(vert2))) denom = math.sin(angleBetweenGuides) f1 = math.sin((1-interpFactor)*angleBetweenGuides)/denom f2 = math.sin(interpFactor*angleBetweenGuides)/denom newVert = aljabr.vadd(aljabr.vmul(vert1,f1),aljabr.vmul(vert2,f2)) #Uncomment the following line we use lerp instead slerp #newVert = aljabr.vadd(aljabr.vmul(vert1,(1-interpFactor)),aljabr.vmul(vert2,interpFactor)) h.controlPoints.append([newVert[0]+randomVect[0],\ newVert[1]+randomVect[1],\ newVert[2]+randomVect[2]]) if isCollision: print "h is: ", h.controlPoints for j in (0,len(h.controlPoints)): #print "h.controlPts is : ", h.controlPoints[i] #print "h.controlPts[i] length is: ", len(h.controlPoints[i]) h.controlPoints[i][2] = -h.controlPoints[i][2] #Renderman to Blender coordinates! collision(h.controlPoints,humanMesh,octree.minsize,startIndex,gravity) for j in (0,len(h.controlPoints)): h.controlPoints[i][2] = -h.controlPoints[i][2] #Blender to Renderman coordinates! hSet.hairs.append(h) self.hairStyle.append(hSet)
def newSetupJoints(obj, joints): the.Locations = {} for (key, typ, data) in joints: #print(key) if typ == 'j': loc = mh2proxy.calcJointPos(obj, data) the.Locations[key] = loc the.Locations[data] = loc elif typ == 'v': v = int(data) the.Locations[key] = obj.verts[v].co elif typ == 'x': the.Locations[key] = [ float(data[0]), float(data[2]), -float(data[1]) ] elif typ == 'vo': v = int(data[0]) loc = obj.verts[v].co the.Locations[key] = [ loc[0] + float(data[1]), loc[1] + float(data[3]), loc[2] - float(data[2]) ] elif typ == 'vl': ((k1, v1), (k2, v2)) = data loc1 = obj.verts[int(v1)].co loc2 = obj.verts[int(v2)].co the.Locations[key] = vadd(vmul(loc1, k1), vmul(loc2, k2)) elif typ == 'f': (raw, head, tail, offs) = data rloc = the.Locations[raw] hloc = the.Locations[head] tloc = the.Locations[tail] #print(raw, rloc) vec = aljabr.vsub(tloc, hloc) vec2 = aljabr.vdot(vec, vec) vraw = aljabr.vsub(rloc, hloc) x = aljabr.vdot(vec, vraw) / vec2 rvec = aljabr.vmul(vec, x) nloc = aljabr.vadd(hloc, rvec, offs) #print(key, nloc) the.Locations[key] = nloc elif typ == 'b': the.Locations[key] = the.Locations[data] elif typ == 'p': x = the.Locations[data[0]] y = the.Locations[data[1]] z = the.Locations[data[2]] the.Locations[key] = [x[0], y[1], z[2]] elif typ == 'vz': v = int(data[0]) z = obj.verts[v].co[2] loc = the.Locations[data[1]] the.Locations[key] = [loc[0], loc[1], z] elif typ == 'X': r = the.Locations[data[0]] (x, y, z) = data[1] r1 = [float(x), float(y), float(z)] the.Locations[key] = aljabr.vcross(r, r1) elif typ == 'l': ((k1, joint1), (k2, joint2)) = data the.Locations[key] = vadd(vmul(the.Locations[joint1], k1), vmul(the.Locations[joint2], k2)) elif typ == 'o': (joint, offsSym) = data if type(offsSym) == str: offs = the.Locations[offsSym] else: offs = offsSym the.Locations[key] = vadd(the.Locations[joint], offs) else: raise NameError("Unknown %s" % typ) return
def direction(self): direction = vnorm(self.offset) axis = vnorm(vcross([0.0, 0.0, 1.0], direction)) angle = acos(vdot([0.0, 0.0, 1.0], direction)) return axisAngleToQuaternion(axis, angle)
def projectBackground(self): if not hasattr(self, "leftTop"): gui3d.app.prompt("Warning", "You need to load a background before you can project it.", "OK") return mesh = gui3d.app.selectedHuman.getSeedMesh() # for all quads, project vertex to screen # if one vertex falls in bg rect, project screen quad into uv quad # warp image region into texture leftTop = gui3d.app.modelCamera.convertToScreen(*self.leftTop) rightBottom = gui3d.app.modelCamera.convertToScreen(*self.rightBottom) r = [leftTop[0], leftTop[1], rightBottom[0], rightBottom[1]] srcImg = mh.Image(self.backgroundImage.getTexture()) dstImg = mh.Image(gui3d.app.selectedHuman.getTexture()) srcW = srcImg.width srcH = srcImg.height dstW = dstImg.width dstH = dstImg.height eye = gui3d.app.modelCamera.eye focus = gui3d.app.modelCamera.focus transform = mesh.object3d.transform eye = mtransform(transform, eye) focus = mtransform(transform, focus) camera = vnorm(vsub(eye, focus)) for g in mesh.faceGroups: if g.name.startswith("joint") or g.name.startswith("helper"): continue for f in g.faces: # From hdusel in regard of issue 183: As agreed with marc I'll change the # call from packed to discrete because packed structs # are not available on Python 2.6.1 which is mandatory for MakeHuman to run # on OS X 10.5.x # # src = [gui3d.app.modelCamera.convertToScreen(*v.co, obj=mesh.object3d) for v in f.verts] # src = [gui3d.app.modelCamera.convertToScreen(v.co[0], v.co[1], v.co[2], obj=mesh.object3d) for v in f.verts] if any([pointInRect(p, r) for p in src]): for i, v in enumerate(f.verts): src[i][2] = max(0.0, vdot(v.no, camera)) if any([v[2] >= 0.0 for v in src]): for i, v in enumerate(f.verts): src[i][2] = max(0.0, vdot(v.no, camera)) co = [(mesh.uvValues[i][0]*dstW, dstH-(mesh.uvValues[i][1]*dstH)) for i in f.uv] uva = [((v[0]-leftTop[0])/(rightBottom[0] - leftTop[0]), (v[1]-leftTop[1])/(rightBottom[1] - leftTop[1]), v[2]) for v in src] RasterizeTriangle(dstImg, co[0], co[1], co[2], UvAlphaShader(dstImg, srcImg, (uva[:3]))) RasterizeTriangle(dstImg, co[2], co[3], co[0], UvAlphaShader(dstImg, srcImg, ((uva[2], uva[3], uva[0])))) dstImg.save(os.path.join(mh.getPath(''), 'data', 'skins', 'projection.tga')) gui3d.app.selectedHuman.setTexture(os.path.join(mh.getPath(''), 'data', 'skins', 'projection.tga'))
def newSetupJoints (obj, joints): the.Locations = {} for (key, typ, data) in joints: #print(key) if typ == 'j': loc = mh2proxy.calcJointPos(obj, data) the.Locations[key] = loc the.Locations[data] = loc elif typ == 'v': v = int(data) the.Locations[key] = obj.verts[v].co elif typ == 'x': the.Locations[key] = [float(data[0]), float(data[2]), -float(data[1])] elif typ == 'vo': v = int(data[0]) loc = obj.verts[v].co the.Locations[key] = [loc[0]+float(data[1]), loc[1]+float(data[3]), loc[2]-float(data[2])] elif typ == 'vl': ((k1, v1), (k2, v2)) = data loc1 = obj.verts[int(v1)].co loc2 = obj.verts[int(v2)].co the.Locations[key] = vadd(vmul(loc1, k1), vmul(loc2, k2)) elif typ == 'f': (raw, head, tail, offs) = data rloc = the.Locations[raw] hloc = the.Locations[head] tloc = the.Locations[tail] #print(raw, rloc) vec = aljabr.vsub(tloc, hloc) vec2 = aljabr.vdot(vec, vec) vraw = aljabr.vsub(rloc, hloc) x = aljabr.vdot(vec, vraw) / vec2 rvec = aljabr.vmul(vec, x) nloc = aljabr.vadd(hloc, rvec, offs) #print(key, nloc) the.Locations[key] = nloc elif typ == 'b': the.Locations[key] = the.Locations[data] elif typ == 'p': x = the.Locations[data[0]] y = the.Locations[data[1]] z = the.Locations[data[2]] the.Locations[key] = [x[0],y[1],z[2]] elif typ == 'vz': v = int(data[0]) z = obj.verts[v].co[2] loc = the.Locations[data[1]] the.Locations[key] = [loc[0],loc[1],z] elif typ == 'X': r = the.Locations[data[0]] (x,y,z) = data[1] r1 = [float(x), float(y), float(z)] the.Locations[key] = aljabr.vcross(r, r1) elif typ == 'l': ((k1, joint1), (k2, joint2)) = data the.Locations[key] = vadd(vmul(the.Locations[joint1], k1), vmul(the.Locations[joint2], k2)) elif typ == 'o': (joint, offsSym) = data if type(offsSym) == str: offs = the.Locations[offsSym] else: offs = offsSym the.Locations[key] = vadd(the.Locations[joint], offs) else: raise NameError("Unknown %s" % typ) return