Skip to content

Instantly share code, notes, and snippets.

@JFlynnXYZ
Last active April 17, 2016 15:37
Show Gist options
  • Save JFlynnXYZ/95a1469104a1ed4c0f24 to your computer and use it in GitHub Desktop.
Save JFlynnXYZ/95a1469104a1ed4c0f24 to your computer and use it in GitHub Desktop.
Frontier Rigging Script Jobs
def resetMcRestPosition(joints, ctrl, ikHand):
'''Reset Multi chain rest position'''
ikHand.ikBlend.set(0)
for j in joints:
j.rotate.set((0,0,0))
ctrl.translate.set((0,0,0))
ikHand.ikBlend.set(1)
def resetMcRotX(joints, ikHand):
'''Reset Multi chain rotation'''
ikHand.ikBlend.set(0)
for j in joints:
j.rotate.rotateX.set(0)
ikHand.ikBlend.set(1)
def fkIkMatch(fkJnts, ikJnts, ikCtrl, fkIkCtrl, ikHand):
'''Fk to IK multi chain match'''
ikHand.ikBlend.set(0)
for fk, ik in zip(fkJnts,ikJnts):
fkParents = [fk.getParent(i) for i in range(1,5)]
finalRot = [0,0,0]
for f in fkParents:
rot = f.getRotation()
finalRot[0] += rot.x
finalRot[1] += rot.y
finalRot[2] += rot.z
pm.rotate(ik, finalRot)
fkEndPosWp = fkJnts[0].getTranslation(space='world')
ikCtrl.setTranslation(fkEndPosWp, space='world')
fkIkCtrl.fkIk.set(0)
ikHand.ikBlend.set(1)
def setupScriptJobs():
tents = ['l','m','r']
jobs = []
for t in tents:
ikCtrl = pm.PyNode('anim_ik_mc_tentacle_{}_1'.format(t))
fkikCtrl = pm.PyNode('anim_tentacle_{}_fkik_1'.format(t))
fkJnts = pm.PyNode('tentacle_rig_{}_fk_jnts_1'.format(t)).getChildren(ad=True, type=pm.nt.Joint)
fkJnts.reverse()
ikJnts = pm.PyNode('ik_tentacle_{}_1_JNT_1'.format(t)).getChildren(ad=True, type=pm.nt.Joint)
ikJnts.append(pm.PyNode('ik_tentacle_{}_1_JNT_1'.format(t)))
bindJoints = pm.PyNode('bind_tentacle_{}_1_JNT_1'.format(t)).getChildren(ad=True, type=pm.nt.Joint)
bindJoints.append(pm.PyNode('bind_tentacle_{}_1_JNT_1'.format(t)))
ikHand = pm.PyNode('ik_mc_tentacle_{}_2'.format(t))
mcRestJob = pm.scriptJob(attributeChange=[ikCtrl.resetRestPosition,
par(resetMcRestPosition, ikJnts, ikCtrl, ikHand)])
mcRotXJob = pm.scriptJob(attributeChange=[ikCtrl.resetXRot,
par(resetMcRotX, ikJnts, ikHand)])
matchJob = pm.scriptJob(attributeChange=[fkikCtrl.matchIkFk,
par(fkIkMatch, fkJnts, ikJnts, ikCtrl, fkikCtrl, ikHand)])
jobs.append([mcRestJob, mcRotXJob, matchJob])
return jobs
codeString = """from functools import partial as par
import pymel.core as pm
def ikHandleLength(ikh):
startJnt = ikh.startJoint.get()
return (ikh.getTranslation(space='world') - startJnt.getTranslation(space='world')).length()
def limitTranslate(ikh, startLength, root):
ikhLength = ikHandleLength(ikh)
print ikhLength, startLength
if ikhLength > startLength:
startJointPos = ikh.startJoint.get().getTranslation(space='world')
ikhPos = ikh.getTranslation(space='world')
ikhVec = startJointPos - ikhPos
ikhVecNorm = ikhVec.normal()
newStartJointVec = ikhVecNorm * startLength
rootStartJointVec = root.getTranslation(space='world') - startJointPos
finalRootPos = ikhPos + (newStartJointVec + rootStartJointVec)
root.setTranslation(finalRootPos, space='world')
def resetMcRestPosition(joints, ctrl, ikHand):
ikHand.ikBlend.set(0)
for j in joints:
j.rotate.set((0,0,0))
ctrl.translate.set((0,0,0))
ikHand.ikBlend.set(1)
def resetMcRotX(joints, ikHand):
ikHand.ikBlend.set(0)
for j in joints:
j.rotate.rotateX.set(0)
ikHand.ikBlend.set(1)
def fkIkMatch(fkJnts, ikJnts, ikCtrl, fkIkCtrl, ikHand):
ikHand.ikBlend.set(0)
for fk, ik in zip(fkJnts,ikJnts):
fkParents = [fk.getParent(i) for i in range(1,5)]
finalRot = [0,0,0]
for f in fkParents:
rot = f.getRotation()
finalRot[0] += rot.x
finalRot[1] += rot.y
finalRot[2] += rot.z
pm.rotate(ik, finalRot)
fkEndPosWp = fkJnts[0].getTranslation(space='world')
ikCtrl.setTranslation(fkEndPosWp, space='world')
fkIkCtrl.fkIk.set(0)
ikHand.ikBlend.set(1)
def setupScriptJobs():
tents = ['l','m','r']
jobs = []
for t in tents:
ikCtrl = pm.PyNode('anim_ik_mc_tentacle_{}_1'.format(t))
fkikCtrl = pm.PyNode('anim_tentacle_{}_fkik_1'.format(t))
fkJnts = pm.PyNode('tentacle_rig_{}_fk_jnts_1'.format(t)).getChildren(ad=True, type=pm.nt.Joint)
fkJnts.reverse()
ikJnts = pm.PyNode('ik_tentacle_{}_1_JNT_1'.format(t)).getChildren(ad=True, type=pm.nt.Joint)
ikJnts.append(pm.PyNode('ik_tentacle_{}_1_JNT_1'.format(t)))
bindJoints = pm.PyNode('bind_tentacle_{}_1_JNT_1'.format(t)).getChildren(ad=True, type=pm.nt.Joint)
bindJoints.append(pm.PyNode('bind_tentacle_{}_1_JNT_1'.format(t)))
ikHand = pm.PyNode('ik_mc_tentacle_{}_2'.format(t))
mcRestJob = pm.scriptJob(attributeChange=[ikCtrl.resetRestPosition,
par(resetMcRestPosition, ikJnts, ikCtrl, ikHand)])
mcRotXJob = pm.scriptJob(attributeChange=[ikCtrl.resetXRot,
par(resetMcRotX, ikJnts, ikHand)])
matchJob = pm.scriptJob(attributeChange=[fkikCtrl.matchIkFk,
par(fkIkMatch, fkJnts, ikJnts, ikCtrl, fkikCtrl, ikHand)])
jobs.append([mcRestJob, mcRotXJob, matchJob])
return jobs
SCENE = pm.Scene()
jobs = setupScriptJobs()
"""
scriptNode = pm.scriptNode(st=2, bs=codeString, n='tentacleCtrlScriptJobs', stp='python')
import pymel.core as pm
def aimJointToObject(jnt=None, obj=None, axis='x', up=(0,0,1)):
if jnt == None or obj == None:
objs = pm.selected()
if len(objs) < 2:
raise ValueError("Not enough objects selected! Select two.")
obj, jnt = objs
order = ('x','y','z')
v = obj.getTranslation() - jnt.getTranslation()
u = pm.dt.Vector(up)
aimAxis = v
uCrossAimAxis = u.cross(aimAxis)
sideAxis = uCrossAimAxis / uCrossAimAxis.length()
aimCrossSide = aimAxis.cross(sideAxis)
upAxis = aimCrossSide / aimCrossSide.length()
vecs = [aimAxis, sideAxis, upAxis]
i,j = 0, order.index(axis)
vecs[j], vecs[i] = vecs[i], vecs[j]
aimTransform = pm.dt.TransformationMatrix()
aimTransform.a00, aimTransform.a01, aimTransform.a02 = vecs[0].get()
aimTransform.a10, aimTransform.a11, aimTransform.a12 = vecs[1].get()
aimTransform.a20, aimTransform.a21, aimTransform.a22 = vecs[2].get()
rot = aimTransform.getRotation()
rot.setDisplayUnit("degrees")
if axis == "z":
rot.x += 180
rot = rot.bound()
rot.setDisplayUnit("degrees")
obj1.jointOrient.set(rot)
aimJointToObject(axis = 'x')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment