Skip to content

Instantly share code, notes, and snippets.

@maxenko
Last active February 12, 2021 03:10
Show Gist options
  • Save maxenko/10a8472aafe8adc1b3be96f8090700fb to your computer and use it in GitHub Desktop.
Save maxenko/10a8472aafe8adc1b3be96f8090700fb to your computer and use it in GitHub Desktop.
import c4d
from c4d import utils
# returns list of all children matching the name within given hierarchy
def findChildrenWithName(parent, childName):
op = parent
if parent.GetName() == childName:
return op
found = [] # store all matches
def recurse_hierarchy(op, found):
while op:
if op.GetName() == childName:
found.append(op)
recurse_hierarchy(op.GetDown(), found)
op = op.GetNext() # i++ for next while cycle
return
recurse_hierarchy(op, found)
return found
def firstOrNone( lst ):
if len(lst) > 0:
return lst[0]
print("Empty joint list.")
return None
def getMorphTag():
userData = op.GetUserDataContainer()
tag = None
# iterate userdata, find "MorphTag"
for userDataId, baseContainer in userData:
if baseContainer[c4d.DESC_NAME] == "MorphTag":
tag = op[userDataId]
if tag == None:
print("No morph tag specified. Please set User Data field named 'MorphTag'.")
return None
return tag
def getMorph(name):
tag = getMorphTag()
numOfTags = tag.GetMorphCount()
for i in range(numOfTags):
morph = tag.GetMorph(i)
if (morph.GetName() == name):
return morph
return None
def getJoinAngle(joint, vector):
angle = joint[c4d.ID_BASEOBJECT_REL_ROTATION,vector]
#print(angle)
def setMorphWeight(name, joint, vector, fromMin, fromMax, toMin, toMax):
tag = getMorphTag()
morph = getMorph(name)
#print(fromMin)
fromMin = utils.DegToRad(fromMin)
#print(fromMin)
fromMax = utils.DegToRad(fromMax)
if morph is not None:
angle = joint[c4d.ID_BASEOBJECT_REL_ROTATION,vector]
targetVal = utils.RangeMap(angle, fromMin, fromMax, toMin , toMax, True)
tag[c4d.ID_CA_POSE_ANIMATE_DATA,(tag.GetMorphID(tag.GetMorphIndex(morph)))[1].id] = targetVal
def setMorphWeight2Step(name, joint, vector, fromMinA, fromMaxA, toMinA, toMaxA, fromMinB, fromMaxB, toMinB, toMaxB):
tag = getMorphTag()
morph = getMorph(name)
if morph is not None:
getJoinAngle(joint,vector)
angle = joint[c4d.ID_BASEOBJECT_REL_ROTATION,vector]
if(angle >= fromMinA and angle <= fromMaxA ):
targetVal = utils.RangeMap(angle, fromMinA, fromMaxA, toMinA , toMaxA, True)
tag[c4d.ID_CA_POSE_ANIMATE_DATA,(tag.GetMorphID(tag.GetMorphIndex(morph)))[1].id] = targetVal
if(angle >= fromMinB and angle <= fromMaxB):
targetVal = utils.RangeMap(angle, fromMinB, fromMaxB, toMinB , toMaxB, True)
tag[c4d.ID_CA_POSE_ANIMATE_DATA,(tag.GetMorphID(tag.GetMorphIndex(morph)))[1].id] = targetVal
def getJoint(jointName):
userData = op.GetUserDataContainer()
root = None
# iterate userdata, find "SkeletalRoot", set it as our root
for userDataId, baseContainer in userData:
if baseContainer[c4d.DESC_NAME] == "SkeletalRoot":
root = op[userDataId]
if root == None:
print("No root joint specified. Please set User Data field named 'SkeletalRoot'.")
return None
return firstOrNone(findChildrenWithName(root,jointName))
def getMorphPrefix():
userData = op.GetUserDataContainer()
tag = None
# iterate userdata, find "MorphPrefix"
for userDataId, baseContainer in userData:
if baseContainer[c4d.DESC_NAME] == "MorphPrefix":
prefix = op[userDataId]
if prefix == None:
print("No morph prefix tag specified. Please set User Data field named 'MorphPrefix'.")
return None
return prefix
def main():
# get mesh and its tag
mesh = doc.SearchObject("Genesis3Female.Shape")
pmTag = mesh.GetTag(c4d.Tposemorph)
prefix = getMorphPrefix()
# neck
setMorphWeight(prefix+"pJCMNeckFwd35", getJoint("neckLower"), c4d.VECTOR_Y, 0, 35, 0, 1)
setMorphWeight(prefix+"pJCMNeckBack27", getJoint("neckUpper"), c4d.VECTOR_Y, 0, -27, 0 , 1)
setMorphWeight(prefix+"pJCMNeckLowerSide40R", getJoint("neckLower"), c4d.VECTOR_Z, 0, -40, 0, 1)
setMorphWeight(prefix+"pJCMNeckLowerSide40L", getJoint("neckLower"), c4d.VECTOR_Z, 0, 40, 0, 1)
setMorphWeight(prefix+"pJCMNeckTwist22R", getJoint("neckLower"), c4d.VECTOR_X, 0, -22, 0, 1)
setMorphWeight(prefix+"pJCMNeckTwist22L", getJoint("neckLower"), c4d.VECTOR_X, 0, 22, 0, 1)
# shoulders
setMorphWeight(prefix+"pJCMShldrDown75R", getJoint("rShldrBend"), c4d.VECTOR_Z, 0, -75, 0, 1)
setMorphWeight(prefix+"pJCMShldrDown75L", getJoint("lShldrBend"), c4d.VECTOR_Z, 0, 75, 0, 1)
setMorphWeight(prefix+"pJCMShldrUp35R", getJoint("rShldrBend"), c4d.VECTOR_Z, 0, 35, 0, 1)
setMorphWeight(prefix+"pJCMShldrUp35L", getJoint("lShldrBend"), c4d.VECTOR_Z, 0, -35, 0, 1)
setMorphWeight(prefix+"pJCMShldrFwd95R", getJoint("rShldrBend"), c4d.VECTOR_X, 0, 95, 0, 1)
setMorphWeight(prefix+"pJCMShldrFwd95L", getJoint("lShldrBend"), c4d.VECTOR_X, 0, -95, 0, 1)
#collar
setMorphWeight(prefix+"pJCMCollarUp50R", getJoint("rCollar"), c4d.VECTOR_Z, 0, 50, 0, 1)
setMorphWeight(prefix+"pJCMCollarUp50L", getJoint("lCollar"), c4d.VECTOR_Z, 0, -50, 0, 1)
setMorphWeight(prefix+"pJCMCollarTwistp30R", getJoint("rCollar"), c4d.VECTOR_X, 0, 30, 0, 1)
setMorphWeight(prefix+"pJCMCollarTwistp30L", getJoint("lCollar"), c4d.VECTOR_X, 0, 30, 0, 1)
setMorphWeight(prefix+"pJCMCollarTwistn30R", getJoint("rCollar"), c4d.VECTOR_X, 0, -30, 0, 1)
setMorphWeight(prefix+"pJCMCollarTwistn30L", getJoint("lCollar"), c4d.VECTOR_X, 0, -30, 0, 1)
#forearm
setMorphWeight(prefix+"pJCMForeArmFwd135R", getJoint("rForearmBend"), c4d.VECTOR_Y, 0, 135, 0, 1)
setMorphWeight(prefix+"pJCMForeArmFwd135L", getJoint("lForearmBend"), c4d.VECTOR_Y, 0, -135, 0, 1)
setMorphWeight(prefix+"pJCMForeArmFwd75R", getJoint("rForearmBend"), c4d.VECTOR_Y, 0, 75, 0, 1)
setMorphWeight(prefix+"pJCMForeArmFwd75L", getJoint("lForearmBend"), c4d.VECTOR_Y, 0, -75, 0, 1)
#hand
setMorphWeight(prefix+"pJCMHandUp80R", getJoint("rHand"), c4d.VECTOR_Z, 0, 80, 0, 1)
setMorphWeight(prefix+"pJCMHandUp80L", getJoint("lHand"), c4d.VECTOR_Z, 0, -80, 0, 1)
setMorphWeight(prefix+"pJCMHandDwn80R", getJoint("rHand"), c4d.VECTOR_Z, 0, -80, 0, 1)
setMorphWeight(prefix+"pJCMHandDwn80L", getJoint("lHand"), c4d.VECTOR_Z, 0, 80, 0, 1)
#fingers
#index
setMorphWeight(prefix+"pJCMIndex3Dwn90R", getJoint("rIndex3"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMIndex2Dwn105R", getJoint("rIndex2"), c4d.VECTOR_Z, 0, -105, 0, 1)
setMorphWeight(prefix+"pJCMIndex1Dwn90R", getJoint("rIndex1"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMIndex3Dwn90L", getJoint("lIndex3"), c4d.VECTOR_Z, 0, 90, 0, 1)
setMorphWeight(prefix+"pJCMIndex2Dwn105L", getJoint("lIndex2"), c4d.VECTOR_Z, 0, 105, 0, 1)
setMorphWeight(prefix+"pJCMIndex1Dwn90L", getJoint("lIndex1"), c4d.VECTOR_Z, 0, 90, 0, 1)
#mid
setMorphWeight(prefix+"pJCMMid3Dwn90R", getJoint("rMid3"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMMid2Dwn105R", getJoint("rMid2"), c4d.VECTOR_Z, 0, -105, 0, 1)
setMorphWeight(prefix+"pJCMMid1Dwn95R", getJoint("rMid1"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMMid3Dwn90L", getJoint("lMid3"), c4d.VECTOR_Z, 0, 90, 0, 1)
setMorphWeight(prefix+"pJCMMid2Dwn105L", getJoint("lMid2"), c4d.VECTOR_Z, 0, 105, 0, 1)
setMorphWeight(prefix+"pJCMMid1Dwn95L", getJoint("lMid1"), c4d.VECTOR_Z, 0, 90, 0, 1)
#ring
setMorphWeight(prefix+"pJCMRing3Dwn90R", getJoint("rRing3"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMRing2Dwn105R", getJoint("rRing2"), c4d.VECTOR_Z, 0, -105, 0, 1)
setMorphWeight(prefix+"pJCMRing1Dwn95R", getJoint("rRing1"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMRing3Dwn90L", getJoint("lRing3"), c4d.VECTOR_Z, 0, 90, 0, 1)
setMorphWeight(prefix+"pJCMRing2Dwn105L", getJoint("lRing2"), c4d.VECTOR_Z, 0, 105, 0, 1)
setMorphWeight(prefix+"pJCMRing1Dwn95L", getJoint("lRing1"), c4d.VECTOR_Z, 0, 90, 0, 1)
#pinky
setMorphWeight(prefix+"pJCMPinky3Dwn90R", getJoint("rPinky3"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMPinky2Dwn105R", getJoint("rPinky2"), c4d.VECTOR_Z, 0, -105, 0, 1)
setMorphWeight(prefix+"pJCMPinky1Dwn95R", getJoint("rPinky1"), c4d.VECTOR_Z, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMPinky3Dwn90L", getJoint("lPinky3"), c4d.VECTOR_Z, 0, 90, 0, 1)
setMorphWeight(prefix+"pJCMPinky2Dwn105L", getJoint("lPinky2"), c4d.VECTOR_Z, 0, 105, 0, 1)
setMorphWeight(prefix+"pJCMPinky1Dwn95L", getJoint("lPinky1"), c4d.VECTOR_Z, 0, 90, 0, 1)
#thumb
setMorphWeight(prefix+"pJCMThumb3Bend90R", getJoint("rThumb3"), c4d.VECTOR_Y, 0, -90, 0, 1)
setMorphWeight(prefix+"pJCMThumb2Bend65R", getJoint("rThumb2"), c4d.VECTOR_Y, 0, -65, 0, 1)
setMorphWeight(prefix+"pJCMThumb1Bend50R", getJoint("rThumb1"), c4d.VECTOR_Y, 0, -50, 0, 1)
setMorphWeight(prefix+"pJCMThumb3Bend90L", getJoint("lThumb3"), c4d.VECTOR_Y, 0, 90, 0, 1)
setMorphWeight(prefix+"pJCMThumb2Bend65L", getJoint("lThumb2"), c4d.VECTOR_Y, 0, 65, 0, 1)
setMorphWeight(prefix+"pJCMThumb1Bend50L", getJoint("lThumb1"), c4d.VECTOR_Y, 0, 50, 0, 1)
setMorphWeight(prefix+"pJCMThumb1Up20R", getJoint("rThumb1"), c4d.VECTOR_Y, 0, 20, 0, 1)
setMorphWeight(prefix+"pJCMThumb1Up20L", getJoint("lThumb1"), c4d.VECTOR_Y, 0, -20, 0, 1)
#abdomen
setMorphWeight(prefix+"pJCMAbdomen2Fwd40", getJoint("abdomenUpper"), c4d.VECTOR_X, 0, 40, 0, 1)
setMorphWeight(prefix+"pJCMAbdomen2Fwd35", getJoint("abdomenLower"), c4d.VECTOR_X, 0, 35, 0, 1)
setMorphWeight(prefix+"pJCMAbdomen2Side24R", getJoint("abdomenUpper"), c4d.VECTOR_Z, 0, -24, 0, 1)
setMorphWeight(prefix+"pJCMAbdomen2Side24L", getJoint("abdomenUpper"), c4d.VECTOR_Z, 0, 24, 0, 1)
setMorphWeight(prefix+"pJCMAbdomenLowerFwdNavel", getJoint("abdomenUpper"), c4d.VECTOR_X, 0, 40, 0, 1)
setMorphWeight(prefix+"pJCMAbdomenUpperFwdNavel", getJoint("abdomenLower"), c4d.VECTOR_X, 0, 35, 0, 1)
#pelvis
setMorphWeight(prefix+"pJCMPelvisFwd25", getJoint("pelvis"), c4d.VECTOR_X, 0, -25, 0, 1)
#chest
setMorphWeight(prefix+"pJCMChestFwd35", getJoint("chestLower"), c4d.VECTOR_X, 0, 35, 0, 1)
setMorphWeight(prefix+"pJCMChestSide20R", getJoint("chestLower"), c4d.VECTOR_Z, 0, -20, 0, 1)
setMorphWeight(prefix+"pJCMChestSide20L", getJoint("chestLower"), c4d.VECTOR_Z, 0, 20, 0, 1)
#thigh
setMorphWeight(prefix+"pJCMThighSide85R", getJoint("rThighBend"), c4d.VECTOR_Z, 0, 85, 0, 1)
setMorphWeight(prefix+"pJCMThighSide85L", getJoint("lThighBend"), c4d.VECTOR_Z, 0, -85, 0, 1)
setMorphWeight(prefix+"pJCMThighFwd115R", getJoint("rThighBend"), c4d.VECTOR_X, 0, -115, 0, 1)
setMorphWeight(prefix+"pJCMThighFwd115L", getJoint("lThighBend"), c4d.VECTOR_X, 0, -115, 0, 1)
setMorphWeight(prefix+"pJCMThighBack35R", getJoint("rThighBend"), c4d.VECTOR_X, 0, 35, 0, 1)
setMorphWeight(prefix+"pJCMThighBack35L", getJoint("lThighBend"), c4d.VECTOR_X, 0, 35, 0, 1)
#shin
setMorphWeight2Step(prefix+"pJCMShinBend90R", getJoint("rShin"), c4d.VECTOR_Y, 0, 110, 0, 1, 110, 155.5, 1, 0)
setMorphWeight2Step(prefix+"pJCMShinBend90L", getJoint("lShin"), c4d.VECTOR_Y, 0, 110, 0, 1, 110, 155.5, 1, 0)
setMorphWeight(prefix+"pJCMShinBend155R", getJoint("rShin"), c4d.VECTOR_Y, 0, 155, 0, 1)
setMorphWeight(prefix+"pJCMShinBend155L", getJoint("lShin"), c4d.VECTOR_Y, 0, 155, 0, 1)
c4d.EventAdd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment