Esempio n. 1
0
    def addInfluences(self, *influences, **keywords):

        controlObjects = [""]
        baseShapes = [""]
        wrapSmoothness = [1]
        nurbsSamples = [10]
        smooth = -1
        smoothType = 0
        influences = list(influences)

        shortNames = {"co": "controlObjects"}

        for k in keywords:
            if k in locals():
                exec(k + "=keywords[k]")
            elif k in shortNames:
                exec(shortNames[k] + "=keywords[k]")

        controlObjects = iterable(controlObjects)
        baseShapes = iterable(baseShapes)
        wrapSmoothness = iterable(wrapSmoothness)
        nurbsSamples = iterable(nurbsSamples)

        if self.influences == influences:
            self.influences = []
        else:
            influencesHold = influences
            for inf in influencesHold:
                if inf in self.influences:
                    influences.remove(inf)
                else:
                    self.influences.append(inf)

        while len(controlObjects) < len(influences):
            controlObjects.append(controlObjects[-1])
        while len(baseShapes) < len(influences):
            baseShapes.append(baseShapes[-1])
        while len(wrapSmoothness) < len(influences):
            wrapSmoothness.append(wrapSmoothness[-1])
        while len(nurbsSamples) < len(influences):
            nurbsSamples.append(nurbsSamples[-1])

        for i in range(0, len(influences)):

            bsh = baseShapes[i]
            inf = influences[i]
            ctrlObj = controlObjects[i]
            nurbsSample = self.nurbsSamples[i]
            wrapSmooth = self.wrapSmoothness[i]
            infFaces = []
            infSourceShape = ""
            infShape = ""
            infTr = ""
            shapeType = ""

            if isinstance(inf, basestring):

                if len(inf.split(".")) > 1:

                    shapeType = str(mc.getAttr(inf, type=True))

                    if (
                        len(iterable(mc.ls(inf, o=True, type="dagNode"))) > 0
                        and len(iterable(mc.listRelatives(mc.ls(inf, o=True), p=True))) > 0
                    ):
                        infTr = mc.listRelatives(mc.ls(inf, o=True), p=True)[0]
                        infShape = shape(infTr)
                    else:
                        infShapeHist = iterable(mc.ls(iterable(mc.listHistory(inf, f=True, af=True)), type="shape"))
                        if len(infShapeHist) > 0:
                            infShape = infShapeHist[0]
                            infTr = mc.listRelatives(infShape, p=True)

                else:

                    shapeType = mc.nodeType(inf)
                    infShape = infSourceShape = inf
                    infTr = mc.listRelatives(inf, p=True)

            elif isIterable(inf):

                infShape = mc.ls(inf, o=True)[0]
                infTr = mc.listRelatives(infShape, p=True)[0]
                shapeType = mc.nodeType(infShape)

                if mc.nodeType(infShape) == "mesh":

                    inf = mc.polyListComponentConversion(inf, tf=True)

                    inputComponents = []
                    for fStr in inf:
                        inputComponents.append(fStr.split(".")[-1])

                    pco = mc.createNode("polyChipOff")
                    mc.setAttr(pco + ".dup", True)
                    mc.setAttr(pco + ".inputComponents", len(inputComponents), type="componentList", *inputComponents)
                    mc.connectAttr(infShape + ".outMesh", pco + ".ip")

                    psep = mc.createNode("polySeparate")
                    mc.setAttr(psep + ".ic", 2)
                    mc.connectAttr(pco + ".out", psep + ".ip")

                    inf = psep + ".out[1]"

                    if not (isinstance(bsh, basestring) and mc.objExists(bsh)):

                        bshSource = ""

                        for m in removeAll(infShape, mc.ls(iterable(mc.listHistory(infShape)), type="mesh")):

                            if (
                                len(mc.ls(m + ".vtx[*]", fl=True)) == len(mc.ls(infShape + ".vtx[*]", fl=True))
                                and len(mc.ls(m + ".f[*]", fl=True)) == len(mc.ls(infShape + ".f[*]", fl=True))
                                and len(mc.ls(m + ".e[*]", fl=True)) == len(mc.ls(infShape + ".e[*]", fl=True))
                                and len(removeAll(m, mc.listHistory(m))) == 0
                            ):
                                isBSTarget = False
                                for bsConn in iterable(mc.listConnections(m, type="blendShape", p=True)):
                                    if "inputTarget" in ".".join(bsConn.split(".")[1:]):
                                        isBSTarget = True
                                        break
                                if isBSTarget:
                                    continue
                                else:
                                    bshSource = m
                                    break

                        if bshSource == "":

                            bshSource = mc.createNode("mesh", p=infTr)
                            mc.connectAttr(infShape + ".outMesh", bshSource + ".inMesh")
                            mc.blendShape(infShape, bshSource, w=[1, 1])
                            mc.delete(bshSource, ch=True)
                            mc.setAttr(bshSource + ".io", True)

                        pco = mc.createNode("polyChipOff")
                        mc.setAttr(pco + ".dup", True)
                        mc.setAttr(
                            pco + ".inputComponents", len(inputComponents), type="componentList", *inputComponents
                        )
                        mc.connectAttr(bshSource + ".outMesh", pco + ".ip")

                        psep = mc.createNode("polySeparate")
                        mc.setAttr(psep + ".ic", 2)
                        mc.connectAttr(pco + ".out", psep + ".ip")

                        bsh = psep + ".out[1]"

                else:

                    inf = infShape

            plug = firstOpenPlug(self[0] + ".basePoints")

            if isinstance(inf, basestring) and mc.objExists(inf):

                if len(inf.split(".")) <= 1:

                    if mc.nodeType(shape(inf)) == "mesh":
                        inf = shape(inf) + ".outMesh"
                    else:
                        inf = shape(inf) + ".local"

            if isinstance(bsh, basestring) and mc.objExists(bsh):

                if len(bsh.split(".")) <= 1:

                    if mc.nodeType(shape(bsh)) == "mesh":
                        bsh = shape(bsh) + ".outMesh"
                    else:
                        bsh = shape(bsh) + ".local"

            else:

                bshShape = mc.createNode(shapeType)

                if shapeType == "mesh":
                    mc.connectAttr(inf, bshShape + ".inMesh")
                    mc.blendShape(infShape, bshShape, w=(1, 1))
                    mc.delete(bshShape, ch=True)
                    bsh = bsh + ".outMesh"
                else:
                    mc.connectAttr(inf, bshShape + ".create")
                    mc.blendShape(infShape, bshShape, w=(1, 1))
                    mc.delete(bshShape, ch=True)
                    bsh = bsh + ".local"

                mc.setAttr(bshShape + ".io", True)

                # mc.connectAttr(inf,self[0]+'.driverPoints['+str(plug)+']',f=True)
                # mc.connectAttr(bsh,self[0]+'.basePoints['+str(plug)+']',f=True

                # poly smooth
            if shapeType == "mesh":

                pspInf = mc.createNode("polySmoothProxy")
                mc.setAttr(pspInf + ".kb", False)
                mc.connectAttr(inf, pspInf + ".ip")
                inf = pspInf + ".out"

                pspBase = mc.createNode("polySmoothProxy")
                mc.setAttr(pspBase + ".kb", False)
                mc.connectAttr(bsh, pspBase + ".ip")
                bsh = pspBase + ".out"

            mc.connectAttr(inf, self[0] + ".driverPoints[" + str(plug) + "]", f=True)
            mc.connectAttr(bsh, self[0] + ".basePoints[" + str(plug) + "]", f=True)

            # add wrap control attributes

            if not mc.objExists(ctrlObj):
                ctrlObj = infTr

            if shapeType == "mesh":

                if not "wrapSmoothLevels" in mc.listAttr(ctrlObj):
                    mc.addAttr(ctrlObj, ln="wrapSmoothLevels", at="short", dv=0)

                mc.setAttr(ctrlObj + ".wrapSmoothLevels", k=False, cb=True)
                mc.connectAttr(ctrlObj + ".wrapSmoothLevels", pspInf + ".el")
                mc.connectAttr(ctrlObj + ".wrapSmoothLevels", pspBase + ".el")
                mc.connectAttr(ctrlObj + ".wrapSmoothLevels", pspInf + ".ll")
                mc.connectAttr(ctrlObj + ".wrapSmoothLevels", pspBase + ".ll")

                if not "wrapSmoothType" in mc.listAttr(ctrlObj):
                    mc.addAttr(
                        ctrlObj, ln="wrapSmoothType", at="enum", en="exponential:linear", min=0, max=1, dv=smoothType
                    )

                mc.setAttr(ctrlObj + ".wrapSmoothType", k=False, cb=True)
                mc.connectAttr(ctrlObj + ".wrapSmoothType", pspInf + ".mth")
                mc.connectAttr(ctrlObj + ".wrapSmoothType", pspBase + ".mth")

                if not "inflType" in mc.listAttr(ctrlObj):
                    mc.addAttr(ctrlObj, ln="inflType", at="enum", en="none:point:face", min=1, max=2, dv=2)

                mc.setAttr(ctrlObj + ".inflType", k=False, cb=True)
                mc.connectAttr(ctrlObj + ".inflType", self[0] + ".inflType[" + str(plug) + "]")

            else:
                if not "nurbsSamples" in mc.listAttr(ctrlObj):
                    mc.addAttr(ctrlObj, ln="nurbsSamples", at="short", dv=nurbsSample)

                mc.setAttr(ctrlObj + ".nurbsSamples", k=False, cb=True)
                mc.connectAttr(ctrlObj + ".nurbsSamples", self[0] + ".nurbsSamples[" + str(plug) + "]")

            if self.calculateMaxDistance:

                greatestDistance = 0.0

                if shapeType == "mesh":

                    distCheckMesh = mc.createNode("mesh", p=infTr)
                    mc.connectAttr(inf, distCheckMesh + ".inMesh")

                    deformedCP = ""
                    if mc.nodeType(shape(self.deformed[0])) == "mesh":
                        deformedCP = mc.createNode("closestPointOnMesh")
                        mc.connectAttr(self.deformed[0] + ".worldMesh[0]", deformedCP + ".im")
                    elif mc.nodeType(shape(self.deformed[0])) == "nurbsCurve":
                        deformedCP = mc.createNode("closestPointOnCurve")
                        mc.connectAttr(self.deformed[0] + ".worldSpace", deformedCP + ".ic")
                    elif mc.nodeType(shape(self.deformed[0])) == "nurbsSurface":
                        deformedCP = mc.createNode("closestPointOnSurface")
                        mc.connectAttr(self.deformed[0] + ".worldSpace", deformedCP + ".is")

                    for f in mc.ls(distCheckMesh + ".f[*]", fl=True):

                        center = midPoint(f)
                        mc.setAttr(deformedCP + ".ip", *center)
                        closestPoint = mc.getAttr(deformedCP + ".p")[0]
                        distance = distanceBetween(closestPoint, center)

                        if distance > greatestDistance:
                            greatestDistance = distance

                    mc.disconnectAttr(inf, distCheckMesh + ".inMesh")
                    mc.delete(distCheckMesh)

                if greatestDistance * 2 > mc.getAttr(self[0] + ".maxDistance"):
                    mc.setAttr(self[0] + ".maxDistance", greatestDistance * 2)

            if shapeType == "mesh":

                if smooth >= 0:

                    mc.setAttr(ctrlObj + ".wrapSmoothLevels", smooth)

                elif mc.nodeType(shape(self.deformed[0])) == "mesh":

                    faceCount = 0
                    for d in self.deformed:
                        if mc.nodeType(shape(d)) == "mesh":
                            faceCount += len(mc.ls(shape(d) + ".f[*]", fl=True))

                    smoothSampleMesh = mc.createNode("mesh", p=infTr)
                    mc.connectAttr(inf, smoothSampleMesh + ".inMesh")

                    smoothFaceCount = 0
                    n = 0

                    while len(mc.ls(smoothSampleMesh + ".f[*]", fl=True)) < faceCount:
                        mc.setAttr(ctrlObj + ".wrapSmoothLevels", n)
                        n += 1

                    if len(mc.ls(smoothSampleMesh + ".f[*]", fl=True)) > faceCount * 1.5:
                        mc.setAttr(ctrlObj + ".wrapSmoothLevels", mc.getAttr(ctrlObj + ".wrapSmoothLevels") - 1)

                    mc.disconnectAttr(inf, smoothSampleMesh + ".inMesh")
                    mc.delete(smoothSampleMesh)
Esempio n. 2
0
	def __init__(self,*args,**keywords):

		#defaults
		self.parent=''
		self.transforms=[]
		self.name=''
		self.pointTo=''
		self.aimAt=''
		self.aim=[0,1,0]
		self.up=[0,0,1]
		self.pivot=[]
		self.type='locator'
		self.shape=''
		self.shapes=[]
		self.radius=1
		self.sweep=90
		self.position=[0,0,0]
		self.scale=[1,1,1]
		self.xform=''
		self.spin=0
		self.xformMatrix=[]
		self.freeze=False
		self.softParent=''

		# attributes

		self.parentSpaces=[]


		self.shortNames=\
		{
			'p':'parent',
			'f':'freeze',
			'n':'name',
			'tan':'tangent',
			'pv':'pivot',
			'piv':'pivot',
			'tr':'transform',
			'pct':'parentCtrl',
			'pctrl':'parentCtrl',
			'ctrl':'control',
			'rad':'radius',
			'r':'radius',
			'sw':'sweep',
			'pos':'position',
			't':'type',
			'x':'xform',
			'aa':'aimAt',
			'sp':'softParent'
		}

		self.attributeTypes=\
		{
			'normal':'float3',
			'up':'double3',
			'aim':'double3',
			'sweep':'double',
			'spin':'double'
		}

		self.attributeLimits=\
		{
			'normal':[[-1,-1,-1],[1,1,1]],
			'up':[[-1,-1,-1],[1,1,1]],
			'aim':[[-1,-1,-1],[1,1,1]],
			'sweep':[0,180],
			'spin':[-225,135],
			'radius':[0,10000000]
		}

		self.attributeShortNames=\
		{
			'position':'pos',
			'radius':'rad',
			'spin':'sp',
			'sweep':'sw',
			'normal':'nr'
		}

		for k in keywords:
			if k in self.shortNames:
				exec('self.'+self.shortNames[k]+'=keywords[k]')
			elif k in self.__dict__:
				exec('self.'+k+'=keywords[k]')

		if len(args)==0 and not mc.objExists(self.xform): args=mc.ls(sl=True)

		sel=[]

		for a in args:
			if isIterable(a):
				sel.extend(a)
			else:
				sel.append(a)

		for s in sel:
			if mc.ls(s)==mc.ls(s,type='transform'):
				self.transforms.append(mc.ls(s)[0])
			elif mc.ls(s)==mc.ls(s,type='shape'):
				self.transforms.append(mc.ls(s)[0])

		# parse options

		if self.name=='': self.name='Handle#'

		if not isIterable(self.position):
			if isinstance(self.position,basestring) and mc.objExists(self.position):
				if len(iterable(self.position.split('.')))>1:
					try:
						self.position=mc.pointPosition(self.position)
					except:
						self.position=mc.pointPosition(self.position+'.rp')
				else:
					self.position=mc.pointPosition(self.position+'.rp')
		elif len(self.position)<3:
			self.position=[0.0,0.0,0.0]

		if self.transforms==[]:
			if 'freeze' not in keywords and 'f' not in keywords:
				self.freeze=True
			if mc.objExists(self.xform):
				self.transforms.append(mc.createNode('transform',n=self.name,p=self.xform))
				mc.setAttr(self.transforms[-1]+'.r',0,0,0)
			else:
				self.transforms.append(mc.createNode('transform',n=self.name))

			if mc.objExists(self.parent):
				mc.parent(self.transforms[-1],self.parent)
			else:
				mc.parent(self.transforms[-1],w=True)

			"""if mc.objExists(self.xform):

				xfm=mc.xform(self.xform,q=True,ws=True,a=True,m=True)
				wsro=mc.xform(self.xform,q=True,ws=True,ro=True)
				wst=mc.xform(self.xform,q=True,ws=True,t=True)
				wsrp=mc.xform(self.xform,q=True,ws=True,rp=True)
				wsra=mc.xform(self.xform,q=True,ws=True,ra=True)
				wssp=mc.xform(self.xform,q=True,ws=True,sp=True)
				print self.transforms[-1]
				print xfm
				mc.xform(self.transforms[-1],ws=True,a=True,m=xfm)
				mc.xform(self.transforms[-1],ws=True,ro=wsro)
				mc.xform(self.transforms[-1],ws=True,t=wst)
				mc.xform(self.transforms[-1],ws=True,rp=wsrp)
				mc.xform(self.transforms[-1],ws=True,ra=wsra)
				mc.xform(self.transforms[-1],ws=True,sp=wssp)

				print mc.xform(self.transforms[-1],q=True,a=True,ws=True,m=True)"""

		elif mc.objExists(self.parent):
			for t in self.transforms:
				mc.parent(t,self.parent)

		if mc.objExists(self.softParent):
			for t in self.transforms:
				self.parentSpaces.append(ParentSpace(t,self.softParent))

		if self.freeze:
			freeze(self.transforms[-1],t=True)

		if mc.objExists(self.pointTo):
			self.aimCurve=mc.createNode('nurbsCurve',p=self.transforms[0])

		if len(self.shapes)==0:
			exec('self.mk'+self.type[0].upper()+self.type[1:]+'()')

		if mc.objExists(self.pointTo):
			self.mkAim()

		for t in self.transforms:
			if not 'zenHandleShape' in mc.listAttr(t):
				mc.addAttr(t,ln='zenHandleShape',m=True,at='message')

		for s in self.shapes:
			if not 'zenHandle' in mc.listAttr(s):
				mc.addAttr(s,ln='zenHandle',at='message')

		for t in self.transforms:
			for s in self.shapes:
				mc.connectAttr\
				(
					s+'.zenHandle',
					t+'.zenHandleShape['+str(firstOpenPlug(t+'.zenHandleShape'))+']'
				)

		if len(self.transforms)>1:
			for t in self.transforms[1:]:
				pass

		self[:]=self.shapes
Esempio n. 3
0
	def create(self):

		mc.cycleCheck(e=False)

		if mc.objExists(self.parent):
			self.group=mc.createNode('transform',n=uniqueNames(self.name),p=self.parent)
		else:
			self.group=mc.createNode('transform',n=uniqueNames(self.name))

		self.jointParent=''
		if len(iterable(mc.listRelatives(self.joints[0],p=True)))>0:
			self.jointParent=mc.listRelatives(self.joints[0],p=True)[0]
		else:
			self.jointParent=mc.createNode('transform',n=uniqueNames(self.name+'CtrlJointGroup'))
			mc.parent(self.joints[0],self.jointParent)

		cMuscleObjects=[]

		# create control joints

		self.ctrlJoints=[]
		for j in self.joints:

			cj=mc.createNode('joint',p=j,n=uniqueNames(self.name+'CtrlJoint'))

			if len(self.ctrlJoints)==0:
				mc.parent(cj,self.group)
				if mc.objExists(self.jointParent):
					self.jointParent=mc.rename(ParentSpace(cj,self.jointParent)[0],self.name+'CtrlJoints')
				else:
					self.jointParent=mc.rename(ParentSpace(cj)[0],self.name+'CtrlJoints')
			else:
				mc.parent(cj,self.ctrlJoints[-1])

			mc.setAttr(cj+'.r',*mc.getAttr(j+'.r')[0])
			mc.setAttr(cj+'.jo',*mc.getAttr(j+'.jo')[0])
			self.originalRotations[cj+'.r']=list(mc.getAttr(cj+'.r')[0])

			mc.setAttr(j+'.r',0,0,0)
			mc.setAttr(cj+'.r',0,0,0)

			mc.setAttr(cj+'.s',1,1,1)
			mc.setAttr(cj+'.radius',mc.getAttr(j+'.radius')*1.5)#0)
			mc.setAttr(cj+'.ovc',10)
			mc.connectAttr(j+'.pa',cj+'.pa')

			if self.joints.index(j)<len(self.joints)-1:
				childList=removeAll\
				(
					iterable(mc.listRelatives(self.joints[self.joints.index(j)+1],c=True,ad=True))+[self.joints[self.joints.index(j)+1]],
					iterable(mc.listRelatives(j,c=True,ad=True))+[j]
				)

				chList=childList
				for c in chList:
					if mc.nodeType(c) not in ['transform','joint','cMuscleObject']:
						childList.remove(c)
					if mc.nodeType(c) in ['transform','joint']:
						for a in ['.t','.tx','.ty','.tz','.r','.rx','.ry','.rz']:
							if mc.connectionInfo(c+a,id=True) or mc.getAttr(c+a,l=True) or mc.getAttr(c+'.io'):
								childList.remove(c)
								break

				if j==self.joints[-2]:
					childList.append(self.joints[-1])

				for jc in childList:
					if mc.nodeType(jc)=='transform' or mc.nodeType(jc)=='joint':
						if jc in self.joints[:-1]:
							mc.parentConstraint(cj,jc,mo=True)
						else:
							mc.parentConstraint(cj,jc,sr=('x','y','z'),mo=True)
					elif 'cMuscle' in mc.nodeType(jc):
						cMuscleObjects.append(jc)

			else:
				mc.parentConstraint(cj,j,st=('x','y','z'),mo=True)

			self.ctrlJoints.append(cj)

		mc.hide(self.jointParent)
		mc.setAttr(self.ctrlJoints[1]+'.ssc',False)

		# create ik

		self.ikHandle,self.endEffector=mc.ikHandle(sol='ik2Bsolver',sj=self.ctrlJoints[0],ee=self.ctrlJoints[-1],n=uniqueNames(self.name+'Handle'))
		self.endEffector=mc.rename(self.endEffector,self.name+'Effector')
		mc.setAttr(self.ikHandle+'.snapEnable',False)
		mc.hide(self.ikHandle)
		mc.setAttr(self.ikHandle+'.ikBlend',0)

		for j in self.originalRotations:
			if isIterable(self.originalRotations[j]):
				mc.setAttr(j,*self.originalRotations[j])
			else:
				mc.setAttr(j,self.originalRotations[j])

		# look for twist joints

		if self.twist:
			skipAxis=removeAll(self.orientAxis,['x','y','z'])
			twistJoints=removeAll([self.joints[-2],self.joints[-1]],hierarchyBetween(self.joints[-2],self.joints[-1],type='joint'))
			for i in range(0,len(twistJoints)):
				tj=twistJoints[i]
				oc=mc.orientConstraint(self.ctrlJoints[-1],tj,sk=skipAxis,mo=True)
				if i>0:
					oc=mc.orientConstraint(self.ctrlJoints[-2],tj,sk=skipAxis,mo=True)
					wal=mc.orientConstraint(oc,q=True,wal=True)
					distToBend=distanceBetween(self.ctrlJoints[-2],tj)
					distToEnd=distanceBetween(self.ctrlJoints[-1],tj)
					mc.setAttr(oc+'.'+wal[-1],distToEnd/(distToBend+distToEnd))
					mc.setAttr(oc+'.'+wal[-2],distToBend/(distToBend+distToEnd))

		# make stretchy

		db=mc.createNode('distanceBetween')
		mc.connectAttr(self.ctrlJoints[0]+'.t',db+'.p1')

		pmm1=mc.createNode('pointMatrixMult')
		pmm2=mc.createNode('pointMatrixMult')

		mc.connectAttr(self.ikHandle+'.t',pmm1+'.ip')
		mc.connectAttr(self.ikHandle+'.pm[0]',pmm1+'.im')

		mc.connectAttr(pmm1+'.o',pmm2+'.ip')
		mc.connectAttr(self.ctrlJoints[0]+'.pim[0]',pmm2+'.im')

		mc.connectAttr(pmm2+'.o',db+'.p2')

		mdl=mc.createNode('multDoubleLinear')
		mc.connectAttr(db+'.d',mdl+'.i1')
		mc.setAttr(mdl+'.i2',1.0/mc.getAttr(db+'.d'))
		cn=mc.createNode('clamp')
		for i in range(0,3):
			c=['r','g','b'][i]
			a=['x','y','z'][i]
			mc.connectAttr(mdl+'.o',cn+'.ip'+c)
			mc.setAttr(cn+'.mn'+c,1)
			mc.connectAttr(mdl+'.o',cn+'.mx'+c)
			mc.connectAttr(cn+'.op'+c,self.ctrlJoints[0]+'.s'+a)

		for cmo in cMuscleObjects:
			mdlcm=mc.createNode('multDoubleLinear')
			mc.setAttr(mdlcm+'.i1',mc.getAttr(cmo+'.length'))
			mc.connectAttr(cn+'.op'+['r','g','b'][['x','y','z'].index(self.orientAxis)],mdlcm+'.i2')
			mc.connectAttr(mdlcm+'.o',cmo+'.length')


		# create control objects or set control object pivots

		poleOffset=distanceBetween(self.ctrlJoints[1],self.ctrlJoints[0])*2

		for i in range(0,len(self.controlObjects)-1):
			if mc.objExists(self.controlObjects[i]):
				mc.xform(self.controlObjects[i],ws=True,piv=mc.xform(self.ctrlJoints[-1],q=True,ws=True,rp=True))
			else:
				if 'r' not in self.handleOptions[i] and 'radius' not in self.handleOptions[i]:
					self.handleOptions[i]['r']=distanceBetween(self.ctrlJoints[-1],self.ctrlJoints[0])/4
				if 'name' not in self.handleOptions[i] and 'n' not in self.handleOptions[i]:
					self.handleOptions[i]['n']=self.joints[i]+'_ctrl'
				if 'x' not in self.handleOptions[i] and 'xform' not in self.handleOptions[i]:
					self.handleOptions[i]['xform']=self.joints[i]
				if 'aim' not in self.handleOptions[i] and 'a' not in self.handleOptions[i]:
					self.handleOptions[i]['aim']=self.aimVector
				self.handleOptions[i]['parent']=self.group
				self.handleOptions[-i]['pointTo']=self.joints[i]
				self.handleOptions[i]['aimAt']=self.joints[i]
				self.handles.append(Handle(**self.handleOptions[i]))
				self.controlObjects[i]=(self.handles[-1].transforms[-1])

		if not mc.objExists(self.controlObjects[-1]):
			if 'name' not in self.handleOptions[-1] and 'n' not in self.handleOptions[-1]:
				self.handleOptions[-1]['n']=self.joints[1]+'_aimCtrl'
			if 'x' not in self.handleOptions[-1] and 'xform' not in self.handleOptions[-1]:
				self.handleOptions[-1]['x']=self.ctrlJoints[1]
			if 'aim' not in self.handleOptions[i] and 'a' not in self.handleOptions[i]:
				self.handleOptions[i]['aim']=self.poleVector
			self.handleOptions[-1]['parent']=self.group
			self.handleOptions[-1]['pointTo']=self.joints[1]
			self.handleOptions[-1]['aimAt']=self.joints[1]
			self.handles.append(Handle(**self.handleOptions[-1]))
			self.controlObjects[-1]=(self.handles[-1].transforms[-1])
			mc.move\
			(
				poleOffset*(self.poleVector[0]),
				poleOffset*(self.poleVector[1]),
				poleOffset*(self.poleVector[2]),
				self.controlObjects[-1],
				r=True,os=True,wd=True
			)

		# add and set control attributes

		mc.setAttr(self.controlObjects[-1]+'.v',k=False)
		for attr in ['.sx','.sy','.sz']:
			mc.setAttr(self.controlObjects[-1]+attr,l=True,k=False,cb=False)
			mc.setAttr(self.ikHandle+attr,l=True,k=False,cb=False)
		for attr in ['.rx','.ry','.rz']:
			mc.setAttr(self.controlObjects[-1]+attr,k=False,cb=False)
		for attr in ['.tx','.ty','.tz']:
			mc.setAttr(self.group+attr,l=True,k=False,cb=False)
			mc.setAttr(self.controlObjects[0]+attr,l=True,k=False,cb=False)

		mc.setAttr(self.ikHandle+'.v',k=False,cb=False)

		for attr in ['.tx','.ty','.tz']:
			mc.setAttr(self.controlObjects[1]+attr,l=True,k=False,cb=False)

		if not mc.objExists(self.controlObjects[-2]+'.twist'):
			mc.addAttr(self.controlObjects[-2],at='doubleAngle',ln='twist',k=True)
		if not mc.objExists(self.controlObjects[-2]+'.sway') and self.sway:
			mc.addAttr(self.controlObjects[-2],at='doubleAngle',ln='sway',k=1)
		if not mc.objExists(self.controlObjects[-2]+'.stretch'):
			mc.addAttr(self.controlObjects[-2],at='double',ln='stretch',k=1,dv=self.stretch,min=0)
		if not mc.objExists(self.controlObjects[-2]+'.squash'):
			mc.addAttr(self.controlObjects[-2],at='double',ln='squash',k=1,dv=self.squash,min=0,max=99)
		if not mc.objExists(self.controlObjects[-2]+'.ikSwitch'):
			mc.addAttr(self.controlObjects[-2],at='enum',ln='ikSwitch',en='ik:fk',k=True,dv=1)# if self.switch=='fk' else 0

		#sway control

		if self.sway:

			adl=mc.createNode('addDoubleLinear')
			mc.connectAttr(self.ctrlJoints[1]+'.r'+self.poleAxis,adl+'.i1')
			mc.connectAttr(self.controlObjects[-2]+'.sway',adl+'.i2')
			childList=removeAll\
			(
				iterable(mc.listRelatives(self.joints[2],c=True,ad=True)),
				iterable(mc.listRelatives(self.joints[1],c=True,ad=True))
			)+[self.joints[2],self.joints[1]]
			for c in childList:
				if mc.nodeType(c)=='transform' or mc.nodeType(c)=='joint':
					pc=mc.parentConstraint(c,q=True)
					nc=listNodeConnections(self.ctrlJoints[1],pc,s=True,d=True)
					for conn in nc:
						if conn[0]==self.ctrlJoints[1]+'.rotate':
							mc.disconnectAttr(conn[0],conn[1])
							for a in removeAll(self.poleAxis,['x','y','z']):
								mc.connectAttr(conn[0]+a.upper(),conn[1]+a.upper(),f=True)
							mc.connectAttr(adl+'.o',conn[1]+self.poleAxis.upper(),f=True)

		# ik/fk switch

		for i in range(0,3):

			c=['r','g','b'][i]
			a=['x','y','z'][i]
			adl=mc.createNode('addDoubleLinear')
			mdl1=mc.createNode('multDoubleLinear')
			mc.setAttr(mdl1+'.i2',.01)
			mdl2=mc.createNode('multDoubleLinear')
			mc.setAttr(mdl2+'.i2',.01)
			revNode=mc.createNode('reverse')
			mc.setAttr(adl+'.i1',1)

			mc.connectAttr(	mdl1+'.o',adl+'.i2')
			mc.connectAttr(self.controlObjects[-2]+'.stretch',mdl1+'.i1')

			mc.connectAttr(	mdl2+'.o',revNode+'.ix')
			mc.connectAttr(self.controlObjects[-2]+'.squash',mdl2+'.i1')

			mc.connectAttr(adl+'.o',cn+'.mx'+c,f=True)
			mc.connectAttr(revNode+'.ox',cn+'.mn'+c,f=True)

		if not mc.objExists(self.controlObjects[-2]+'.zenPreviousIKState'):
			if self.switch=='fk':
				mc.addAttr(self.controlObjects[-2],at='long',ln='zenPreviousIKState',k=0,dv=1)
			else:
				mc.addAttr(self.controlObjects[-2],at='long',ln='zenPreviousIKState',k=0,dv=0)
		if not mc.objExists(self.controlObjects[-2]+'.zenPreviousIKParent'):
			if self.switch=='fk':
				mc.addAttr(self.controlObjects[-2],at='long',ln='zenPreviousIKParent',k=0,dv=1)
			else:
				mc.addAttr(self.controlObjects[-2],at='long',ln='zenPreviousIKParent',k=0,dv=0)

		for i in range(0,2):
			for c in mc.listRelatives(self.controlObjects[i],s=True):
				mc.connectAttr(self.controlObjects[-2]+'.ikSwitch',c+'.v')

		mc.connectAttr(self.controlObjects[-2]+'.twist',self.ikHandle+'.twist')

		rev=mc.createNode('reverse')
		mc.connectAttr(self.controlObjects[-2]+'.ikSwitch',rev+'.ix')
		mc.connectAttr(rev+'.ox',self.ikHandle+'.ikBlend')

		for c in mc.listRelatives(self.controlObjects[-1],s=True):
			mc.connectAttr(rev+'.ox',c+'.v')

		# parent spaces

		for i in [0,1,2]:
			if(mc.objExists(self.jointParent)and i in [0,2]):
				ParentSpace(self.controlObjects[i],self.jointParent)
			else:
				ParentSpace(self.controlObjects[i],self.controlObjects[i-1])

		ParentSpace(self.controlObjects[-1],self.controlObjects[-2])
		if mc.objExists(self.jointParent):
			ParentSpace(self.controlObjects[-1],self.jointParent).setParent(self.jointParent)
			ParentSpace(self.controlObjects[-2],self.jointParent).setParent(self.jointParent)
		else:
			ParentSpace(self.controlObjects[-1],self.controlObjects[0])

		if self.switch=='fk':
			ParentSpace(self.controlObjects[2],self.controlObjects[1])

		for co in self.controlObjects[2:]:
			freeze(co,t=True)

		mc.aimConstraint\
		(
			self.ctrlJoints[1],self.controlObjects[-1],
			aim=(self.aimVector[0],self.aimVector[1],self.aimVector[2]),
			wuo=self.ctrlJoints[1],
			wut='objectrotation',
			mo=True
		)

		if mc.objExists(self.jointParent):
			mc.setAttr(self.controlObjects[0]+'.parentTo',l=True,k=False,cb=False)
		mc.setAttr(self.controlObjects[1]+'.parentTo',l=True,k=False,cb=False)

		#constraints

		orientConstraints=['','','']
		for i in [2,1,0]:
			orientConstraints[i]=mc.orientConstraint(self.controlObjects[i],self.ctrlJoints[i],mo=True)[0]
			mc.setAttr(self.controlObjects[i]+'.v',k=False)
			for attr in ['.sx','.sy','.sz']:
				mc.setAttr(self.controlObjects[i]+attr,l=True,k=False,cb=False)
			if i==1:
				if not self.sway:
					mc.setAttr(self.controlObjects[i]+'.r'+self.poleAxis,l=True,k=False,cb=False)
				mc.setAttr(self.controlObjects[i]+'.r'+self.orientAxis,l=True,k=False,cb=False)

		self.poleVectorConstraint=mc.poleVectorConstraint(self.controlObjects[-1],self.ikHandle)[0]

		for oc in orientConstraints[:-1]:
			octl=mc.orientConstraint(oc,q=True,tl=True)
			ocwal=mc.orientConstraint(oc,q=True,wal=True)
			weightAlias=ocwal[octl.index(self.controlObjects[orientConstraints.index(oc)])]
			mc.connectAttr(self.controlObjects[-2]+'.ikSwitch',oc+'.'+weightAlias)

		mc.parent(self.ikHandle,self.controlObjects[2],r=False)

		if self.switch=='ik':
			mc.setAttr(self.controlObjects[-2]+'.ikSwitch',0)

		# link for asset detection

		if 'zenIkFkLimbCtrls' not in mc.listAttr(self.group):
			mc.addAttr(self.group,ln='zenIkFkLimbCtrls',at='message',m=True)
		if 'zenIkFkLimbCtrlJoints' not in mc.listAttr(self.group):
			mc.addAttr(self.group,ln='zenIkFkLimbCtrlJoints',at='message',m=True)
		for co in self.controlObjects:
			if 'zenCtrl' not in mc.listAttr(co):
				mc.addAttr(co,ln='zenCtrl',at='message')
			mc.connectAttr(co+'.zenCtrl',self.group+'.zenIkFkLimbCtrls['+str(firstOpenPlug(self.group+'.zenIkFkLimbCtrls'))+']')
		for cj in self.ctrlJoints:
			if 'zenCtrl' not in mc.listAttr(cj):
				mc.addAttr(cj,ln='zenCtrl',at='message')
			mc.connectAttr(cj+'.zenCtrl',self.group+'.zenIkFkLimbCtrlJoints['+str(firstOpenPlug(self.group+'.zenIkFkLimbCtrlJoints'))+']')

		for bp in self.bindPoses:
			for co in self.controlObjects:
				mc.dagPose(co,a=True,n=bp)

		self[:]=[self.group]+self.controlObjects

		mc.cycleCheck(e=True)

		mc.select(self[-2])
Esempio n. 4
0
	def addTargets(self,*args,**keywords):

		targets=[]
		goals=[]
		goalRange=[[0,10]]
		deleteTargets=False
		base=self.base
		baseIndex=0
		controlObjects=[]
		controlAttributes=[]
		prune=self.prune
		baseIndex=0
		weights=[0]
		control=True

		shortNames=\
		{
			't':'targets',
			'g':'goals',
			'r':'goalRange',
			'range':'goalRange',
			'dt':'deleteTargets',
			'b':'base',
			'co':'controlObjects',
			'ca':'controlAttributes',
			'goal':'goals',
			'p':'prune',
			'w':'weights',
			'c':'control'
		}

		for k in keywords:
			if k in shortNames:
				exec(shortNames[k]+'=keywords[k]')
			elif k in locals() and k!='shortNames':
				exec(k+'=keywords[k]')

		targets.extend(args)
		controlObjects=iterable(controlObjects)
		controlAttributes=iterable(controlAttributes)
		weights=iterable(weights)

		targetTrs=[]
		for i in range(0,len(targets)):
			targets[i]=iterable(targets[i])
			targetTrs.append([])
			for n in range(0,len(targets[i])):
				targets[i][n]=shape(targets[i][n])
				targetTrs[-1].append(mc.listRelatives(targets[i][n],p=True)[0])

		if len(targets)==0:
			raise Exception('BlendShape.addTargets() requires target(s).')
			return

		#double-check base index in case of changes to indexing
		geometry=iterable(mc.blendShape(self[0],q=True,g=True))
		if shape(base[baseIndex]) in geometry:
			baseIndex=geometry.index(shape(base[baseIndex]))
		elif base[baseIndex] in geometry:
			baseIndex=geometry.index(base[baseIndex])
		else:
			raise Exception('Shape '+shape(base[baseIndex])+' is not used by '+self[0]+'.')
			return

		while len(goals)<len(targets):
			goals.append([])

		while len(weights)<len(targets):
			weights.append(weights[-1])

		while len(goalRange)<len(targets):
			goalRange.append(goalRange[-1])

		for i in range(0,len(targets)):

			t=iterable(targets[i])
			g=iterable(goals[i])
			r=iterable(goalRange[i])

			if len(base)>1:
				matchingBases=[]
				tvs=len(mc.ls(t[-1]+'.vtx[*]',fl=True))
				tfs=len(mc.ls(t[-1]+'.f[*]',fl=True))
				tes=len(mc.ls(t[-1]+'.e[*]',fl=True))
				for b in base:
					if\
					(
						tvs==len(mc.ls(b+'.vtx[*]',fl=True)) and
						tfs==len(mc.ls(b+'.f[*]',fl=True)) and
						tes==len(mc.ls(b+'.e[*]',fl=True))
					):
						matchingBases.append(b)

				if base[baseIndex] not in matchingBases and len(matchingBases)>0:
					baseIndex=base.index(matchingBases[-1])

			index=firstOpenPlug(self[0]+'.it['+str(baseIndex)+'].itg')

			if len(g)>0 and g[-1]<g[0]:
				g.reverse()
				t.reverse()

			if len(g)>=len(t): r=[g[0],g[-1]]

			if len(r)==1 and isinstance(r[0],(float,long,int)): r=[r[0],r[0]+10]
			elif len(r)>2 and isinstance(r[0],(float,long,int)) and isinstance(r[-1],(float,long,int)): r=[r[0],r[-1]]
			elif len(r)==0 or not isinstance(r[0],(float,long,int)) or not isinstance(r[-1],(float,long,int)): r=[0,10]

			if r[-1]<r[0]:
				r.reverse()
				t.reverse()

			if t[-1] not in mc.blendShape(self[0],q=True,t=True):

				n=0
				if len(g)==0: g.append(r[0]+(r[-1]-r[0])/len(t))
				while len(g)<len(t):
					g.append(g[-1]+(r[-1]-g[-1])/(len(t)-len(g)))
					n+=1

				for n in range(0,len(t)):

					mc.blendShape\
					(
						self[0],
						e=True,
						t=(base[baseIndex],index,t[n],g[n]),
						w=(index,weights[i])
					)

			goals[i]=g
			targets[i]=t
			goalRange[i]=r

		# if we don't have all the control attributes or we don't have a control object, make some up

		if len(controlObjects)==0 and len(targets)>0:
			controlObjects.append(mc.listRelatives(base[baseIndex],p=True)[0])

		if len(controlObjects)>0:
			while len(controlObjects)<len(targets):
				controlObjects.append(controlObjects[-1])
			n=0
			while len(controlAttributes)<len(targetTrs):
				controlAttributes.append(targetTrs[n][-1].replace(base[baseIndex],''))
				if controlAttributes[-1][0] in ['_','|']: controlAttributes[-1]=controlAttributes[-1][1:]
				controlAttributes[-1]=controlAttributes[-1][0].lower()+controlAttributes[-1][1:]
				n=+1
			if control:
				for i in range(0,len(targets)):
					if mc.objExists(controlObjects[i]) and controlAttributes[i] not in removeDuplicates(mc.listAttr(controlObjects[i])):
						mc.addAttr\
						(
							controlObjects[i],
							ln=controlAttributes[i],
							at='double',
							dv=weights[i],
							k=True,
							smx=goalRange[i][-1],
							smn=goalRange[i][0]
						)
					if not mc.isConnected(controlObjects[i]+'.'+controlAttributes[i],self[0]+'.'+targets[i][-1]):
						mc.connectAttr(controlObjects[i]+'.'+controlAttributes[i],self[0]+'.'+targets[i][-1],f=True)

		self.controlObjects.extend(controlObjects)
		self.targets.extend(targets)
		self.range.extend(goalRange)
		self.goals.extend(goals)
		self.base.extend(base)
		self.controlObjects.extend(controlObjects)
		self.controlAttributes.extend(controlAttributes)