def __init__(self,*args,**keywords): #check to make sure unique names are used in scene uniqueNames(iterable(mc.ls(type='dagNode')),re=True) #default options self.number=1 #if there are no transforms, create this many rivets self.distribute='' self.parents=['',''] self.rebuild=False self.taper='none' # none, normal, distance self.scale='none' # none, uniform, or relative self.scaleDirection=['length','width'] # length, width self.createAimCurve=False self.aimCurve='' self.handles=[] self.handleShapes=[] self.autoFlexGroups=[] self.mo=True # maintain offset self.ctrls=[] #by default control attributes are placed on given transforms self.keepPivot=True self.snapToSurface=False self.realign=False self.rivets=[] self.ClosestPoints=[] self.aimGroups=[] self.softParents=[] self.ArcCtrl=[] self.arcWeight=.5 self.closestPoint='' # geometry, pivot self.organize=True self.constraint=False # use a parent constraint self.parent=False # create a transform and parent objects to it self.spaceTr='' self.surfaceAttr='' self.bindPoses=[] self.parentSpaces=[] self.hasGeometry=True self.skipRotate=[] self.skipTranslate=[] self.skipScale=[] self.skipScaleObjects=[] self.skipRotateObjects=[] self.skipTranslateObjects=[] self.minScaleWidth=0 self.maxScaleWidth=1 self.minScaleLength=0 self.maxScaleLength=1 self.surfaceMatrix='' self.surfaceInverseMatrix='' self.worldMatrix='' self.trs=[] self.uSpans=-1 self.vSpans=-1 self.skins=[] self.shortNames=\ { 'sd':'scaleDirection', 'p':'parents', 'sp':'softParents' } sel=[] if len(args)==0: sel=mc.ls(sl=True) for a in args: sel.extend(iterable(a)) self.bindPoses=iterable(getBindPoses(sel)) if len(self.bindPoses)>0: goToDagPose(self.bindPoses[0]) self.edges=PolyEdgeList(sel,f=True) self.trs=removeDuplicates(mc.ls(self.edges.etcObj,tr=True)) if len(self.edges)==0: #look for a surface or surface attribute reversedObjList=getReversed(self.edges.etcObj) for obj in reversedObjList: if len(obj.split('.'))>1 and mc.getAttr(obj,type=True)=='nurbsSurface': self.surfaceAttr=obj obj=mc.ls(self.surfaceAttr,o=True)[0] if mc.nodeType(obj)=='nurbsSurface': self.uSpans=mc.getAttr(obj+'.spansU') self.vSpans=mc.getAttr(obj+'.spansV') self.spaceTr=mc.listRelatives(obj,p=True)[0] break elif mc.nodeType(obj)=='nurbsSurface': self.surfaceAttr=obj+'.worldSpace[0]' self.spaceTr=mc.listRelatives(obj,p=True)[0] self.uSpans=mc.getAttr(obj+'.spansU') self.vSpans=mc.getAttr(obj+'.spansV') self.trs.remove(obj) break else: children=mc.listRelatives(obj,c=True,s=True,ni=True,type='nurbsSurface') if isIterable(children) and len(children)>0: self.spaceTr=mc.ls(obj)[0] self.surfaceAttr=children[0]+'.worldSpace[0]' self.uSpans=mc.getAttr(children[0]+'.spansU') self.vSpans=mc.getAttr(children[0]+'.spansV') self.trs.remove(obj) break else: self.spaceTr=self.edges.getTr() for k in keywords: if k in self.__dict__: exec('self.'+k+'=keywords[k]') elif k in self.shortNames: exec('self.'+self.shortNames[k]+'=keywords[k]') if len(iterable(self.parents))==1: self.parents=[iterable(self.parents)[0],iterable(self.parents)[0]] if len(iterable(self.softParents))==1: self.softParents=[iterable(self.softParents)[0],iterable(self.softParents)[0]] if len(self.ctrls)==0: self.ctrls=self.trs while len(self.ctrls)<len(self.trs): self.ctrls.append(self.ctrls[-1]) if len(self.skipRotate)>0: for i in self.skipRotate: self.skipRotateObjects.append(self.trs[self.skipRotate[i]]) if len(self.skipTranslate)>0: for i in self.skipTranslate: self.skipTranslateObjects.append(self.trs[self.skipTranslate[i]]) if len(self.skipScale)>0: for i in self.skipScale: self.skipScaleObjects.append(self.trs[self.skipScale[i]]) if len(self.trs)>0: for t in self.trs: shCount=len\ ( removeDuplicates(mc.listRelatives(t,c=True,type='nurbsSurface'))+ removeDuplicates(mc.listRelatives(t,c=True,type='nurbsCurve'))+ removeDuplicates(mc.listRelatives(t,c=True,type='mesh')) ) if shCount==0: self.hasGeometry=False self.closestPoint='pivot' break if self.closestPoint=='' and self.hasGeometry: # default to pivot if using snap to surface, otherwise defualts to geometry if self.snapToSurface: self.closestPoint='pivot' else: self.closestPoint='geometry' if len(self.trs)!=0: self.number=len(self.trs) if len(self.ctrls)<len(self.trs): self.ctrls=self.trs if not self.parent and not self.constraint: self.parent=True if self.createAimCurve: self.parent=True #self.constraint=False #find the root joint if present try: joints=removeDuplicates(mc.listConnections(iterable(mc.ls(iterable(mc.listHistory(self.surfaceAttr)))),type='joint')) if len(joints)>0: self.spaceTr=hierarchyOrder(joints)[0] except: pass self.create()
def __init__(self,*args,**keywords): # default options self.name='limb' self.stretch=20 self.squash=0 self.twist=True # performs auto detect self.sway=True self.switch='ik' # initial ik/fk switch state self.handleOptions=[{'type':'doubleEllipse','spin':-180},{'type':'doubleEllipse','spin':-90},{'type':'doubleEllipse'},{'type':'locator'}] self.tol=1.0 # angle tolerance for preferred angles self.parent='' self.shortNames=\ { 'n':'name', 'p':'parent', 'sp':'softParent', 'co':'controlObjects', 'ho':'handleOptions' } # attributes self.controlObjects=['','','',''] self.bindPoses=[] self.joints=[] self.group='' self.orientAxis='' self.bendAxis='' self.poleAxis='' self.ctrlJoints=[] self.handles=[] self.endEffector='' self.ikHandle='' self.jointParent='' self.jointParent='' self.originalRotations={} self.bendDirection=0 self.poleVector=[] self.poleVectorWorld=[] self.upVector=[] self.aimVector=[] self.parentSpaces=[] for k in keywords: if k in self.__dict__: exec('self.'+k+'=keywords[k]') elif k in self.shortNames: exec('self.'+self.shortNames[k]+'=keywords[k]') uniqueNames(re=True) if len(args)==0: args=mc.ls(sl=True) sel=[] for a in args: sel.extend(iterable(a)) sel=hierarchyOrder(sel) # parse options defualtHandleOptions=[{'type':'doubleEllipse','spin':-180},{'type':'doubleEllipse','spin':-90},{'type':'doubleEllipse'},{'type':'locator'}] i=len(self.handleOptions) while len(self.handleOptions)<4: self.handleOption.append(defualtHandleOptions[i]) i+=1 if isinstance(self.handleOptions,dict): self.handleOptions=[self.handleOptions,self.handleOptions,self.handleOptions] elif isIterable(self.handleOptions): if len(self.handleOptions)==0: self.handleOptions.append({}) while len(self.handleOptions)<3: self.handleOptions.append(self.handleOptions[-1]) else: self.handleOptions=[{},{},{}] self.controlObjects=iterable(self.controlObjects) self.orientAxis=self.orientAxis.lower() self.baseTwist='' self.hierarchy=[] if len(sel)>2: for j in sel[:-1]: if len(hierarchyBetween(j,sel[-1]))>len(self.hierarchy): self.hierarchy=hierarchyBetween(j,sel[-1]) closest=9e999 for s in removeAll([self.hierarchy[0],self.hierarchy[-1]],sel): if\ ( len(iterable(mc.listRelatives(self.hierarchy[0],p=True)))==0 or s in mc.listRelatives(mc.listRelatives(self.hierarchy[0],p=True)[0],c=True,type='joint') ): dist=distanceBetween(s,self.hierarchy[0]) if dist<closest: closest=dist self.baseTwist=s else: self.hierarchy=hierarchyBetween(sel[0],sel[-1]) self.bindPoses=iterable(getBindPoses(self.hierarchy)) self.joints=['','',''] if len(self.hierarchy)<3: raise Exception('There are no joints between your start and end joint. No IK created.') self.joints[0]=self.hierarchy[0] self.joints[-1]=self.hierarchy[-1] # find the orientation axis self.orientAxis='x' axisLen={'x':0,'y':0,'z':0} for j in self.hierarchy[1:]: for a in ['x','y','z']: axisLen[a]+=abs(mc.getAttr(j+'.t'+a)) if axisLen[a]>axisLen[self.orientAxis]: self.orientAxis=a # find bend joint and pole vector self.originalRotations={} for j in self.hierarchy[1:-1]: # check to see if any have a non-zero preferred angle for a in removeAll(self.orientAxis,['x','y','z']): if abs(mc.getAttr(j+'.pa'+a))>=self.tol: self.originalRotations[j+'.r'+a]=mc.getAttr(j+'.r'+a) mc.setAttr(j+'.r'+a,mc.getAttr(j+'.pa'+a)) greatestAngle=0 for j in self.hierarchy[1:-1]: jPos=mc.xform(j,q=True,ws=True,rp=True) prevJPos=mc.xform(self.hierarchy[self.hierarchy.index(j)-1],q=True,ws=True,rp=True) nextJPos=mc.xform(self.hierarchy[self.hierarchy.index(j)+1],q=True,ws=True,rp=True) vAngle=mc.angleBetween(v1=normalize(jPos[0]-prevJPos[0],jPos[1]-prevJPos[1],jPos[2]-prevJPos[2]),v2=normalize(nextJPos[0]-jPos[0],nextJPos[1]-jPos[1],jPos[2]-jPos[2]))[-1] if abs(vAngle)>greatestAngle: greatestAngle=abs(vAngle) self.joints[1]=j mp=midPoint\ ( self.hierarchy[0],self.hierarchy[-1], bias=\ ( distanceBetween(self.hierarchy[0],self.joints[1])/ (distanceBetween(self.hierarchy[0],self.joints[1])+distanceBetween(self.joints[1],self.hierarchy[-1])) ) ) bendPoint=mc.xform(self.joints[1],q=True,ws=True,rp=True) self.poleVectorWorld=normalize\ ( bendPoint[0]-mp[0], bendPoint[1]-mp[1], bendPoint[2]-mp[2] ) pmm=mc.createNode('pointMatrixMult') mc.setAttr(pmm+'.vm',True) mc.connectAttr(self.joints[1]+'.worldInverseMatrix',pmm+'.im') mc.setAttr(pmm+'.ip',*self.poleVectorWorld) self.poleVector=mc.getAttr(pmm+'.o')[0] disconnectNodes(pmm) mc.delete(pmm) greatestLength=0.0 for i in [0,1,2]: if abs(self.poleVector[i])>greatestLength and ['x','y','z'][i]!=self.orientAxis: self.poleAxis=['x','y','z'][i] greatestLength=abs(self.poleVector[i]) self.bendDirection=-abs(self.poleVector[i])/self.poleVector[i] for r in self.originalRotations: mc.setAttr(r,self.originalRotations[r]) preferredAngleWarning=False if not mc.objExists(self.joints[1]): preferredAngleWarning=True mp=midPoint(self.hierarchy[0],self.hierarchy[-1]) cd=9e999 dist=0 for j in self.hierarchy[1:-1]: dist=distanceBetween(j,mp) if dist<cd: cd=dist self.joints[1]=j self.bendAxis=removeAll(self.orientAxis,['z','y','x'])[0] if self.poleAxis=='': self.poleAxis=removeAll([self.orientAxis,self.bendAxis],['x','y','z'])[0] if self.bendAxis=='': self.bendAxis=removeAll([self.orientAxis,self.poleAxis],['x','y','z'])[0] if self.orientAxis=='': self.orientAxis=removeAll([self.bendAxis,self.poleAxis],['x','y','z'])[0] if self.poleAxis=='x': self.poleVector=[-self.bendDirection,0.0,0.0] if self.poleAxis=='y': self.poleVector=[0.0,-self.bendDirection,0.0] if self.poleAxis=='z': self.poleVector=[0.0,0.0,-self.bendDirection] if self.bendAxis=='x': self.upVector=[-self.bendDirection,0.0,0.0] if self.bendAxis=='y': self.upVector=[0.0,-self.bendDirection,0.0] if self.bendAxis=='z': self.upVector=[0.0,0.0,-self.bendDirection] if self.orientAxis=='x': self.aimVector=[self.bendDirection,0.0,0.0] if self.orientAxis=='y': self.aimVector=[0.0,self.bendDirection,0.0] if self.orientAxis=='z': self.aimVector=[0.0,0.0,self.bendDirection] if mc.objExists(self.baseTwist): conn=False for a in ['.r','.rx','.ry','.rz']: if mc.connectionInfo(self.baseTwist+a,id=True): conn=True if not conn: mc.orientConstraint(self.joints[0],self.baseTwist,sk=self.orientAxis) # load ik2Bsolver - ikRPSolver does not work well with this setup mel.eval('ik2Bsolver') self.create() if preferredAngleWarning: raise Warning('Warning: Joints are co-linear and no preferred angles were set. Results may be unpredictable.')
def uniqueNames(*args,**keywords): """ With no keywords, returns a unique version of the given name(s). With re=True or raiseException=True : Ensures unique names are used in the scene and raises an error if there are duplicates present. With rn=True or rename=True : renames any object which have duplicate names. """ rename=False raiseException=False validate=False shortNames=\ { 'rn':'rename', 'v':'validate', 're':'raiseException' } for k in keywords: if k in locals(): exec(k+'=keywords[k]') elif k in shortNames: exec(shortNames[k]+'=keywords[k]') if len(args)==0: args=mc.ls(sl=True) if len(args)==0: args=mc.ls() sel=[] for a in args: sel.extend(iterable(a)) shapes=[] for s in sel: if mc.objExists(s) and shape(s)==s: shapes.append(s) sel=removeAll(shapes,sel)+shapes if validate: rename=False raiseException=False if rename: raiseException=False validate=False if raiseException: rename=False validate=False max=0 if rename or validate or raiseException: max=1 names=[] nonUnique=[] newNames={} baseNameRE=re.compile('(^[^0-9].*[^0-9])') for s in sel: sn=iterable(s.split('|'))[-1] ln=hierarchyOrder(removeDuplicates(mc.ls(sn,l=True)),r=True) if len(ln)>max: try: baseName=baseNameRE.match(sn).group(0) except: baseName=sn i=1 rn=s un=[] while mc.objExists(rn) or len(un)<len(ln): rn=baseName+str(i) if not mc.objExists(rn): un.append(rn) i+=1 names.append(un[0]) for i in range(0,len(ln)): if validate or rename: newNames[ln[i]]=un[i] if rename: mc.rename(ln[i],un[i]) else: nonUnique.append(ln[i]) else: names.append(s) if raiseException: if len(nonUnique)>0: print('##Non-unique names:##\n'+str(nonUnique)) raise Exception('Non-unique names found.') return nonUnique elif validate or rename: return newNames else: if len(sel)==1 and len(names)==1: return names[0] else: return names