On 22/09/2014 at 11:27, xxxxxxxx wrote:
Yeah, I came up with a slightly different technique that brute forces it after finding the the Transfer Maps just would not work. My code worked initially, but when I created the JointTransfer function to simplify code it fails at the GetName() for the target joint. Since I'm targeting this at a very specific rig I made some very specific assumptions about the script: The point count between the object will and should match, and the only thing that different is where the joint are placed and their names. Since the point counts are the same my thinking was to get the source point count and then based on the joint specified iterate through all the mesh points only applying weight where applicable to a specific joint. If there is a more optimized less error prone way I'd really appreciate it. That's for the help.
import c4d
from c4d import gui, documents
# this script copies weights from a figure to the
# custom c4d rig, point counts of both rigs need to
# match
global sourceWeights
global targetWeights
global meshPntCnt
class transferW(gui.GeDialog) :
def CreateLayout(self) :
#create the layout of the dialog
self.SetTitle("Weight Transfer")
self.GroupBegin(1001, c4d.BFH_SCALEFIT, 2, 3)
self.AddStaticText(1002, flags=c4d.BFH_LEFT, initw=100, name="Source")
self.sourceLink = self.AddCustomGui(1003, c4d.CUSTOMGUI_LINKBOX, "",
c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT, 400, 0)
self.AddStaticText(1003, flags=c4d.BFH_LEFT, initw=100, name="Target")
self.targetLink = self.AddCustomGui(1004, c4d.CUSTOMGUI_LINKBOX, "",
c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT, 400, 0)
self.AddButton(1005, c4d.BFH_SCALE, name="Execute")
self.GroupEnd()
return True
def InitValues(self) :
#initiate the gadgets with values
return True
def Command(self, id, msg) :
#handle user input
if id==1005:
self.TranferWeight()
return True
# transfer weights between source and target joint, takes the names
# as args
def JointTransfer(self, sJntName, tJntName) :
global sourceWeights
global targetWeights
sJntIndex = 0; #Setup the source joint index
dJntIndex = 0; #Setup the destination joint index
# we need the joint count so we can do a name match search
# through all the joints in the weight tags
jntCount = sourceWeights.GetJointCount()
#quick loop to find source joint in the source genesis rig
for i in range(0, jntCount) :
jnt = sourceWeights.GetJoint(i, doc)
if jnt.GetName() == sJntName:
sJntIndex = i
break
#quick loop to find target joint in the destination genesis rig
jntCount = targetWeights.GetJointCount()
print jntCount
for i in range(0, jntCount) :
jnt = targetWeights.GetJoint(i, doc)
print jnt.GetName()
if jnt.GetName() == tJntName:
dJntIndex = i
break
# we are going to brute force this. we'll go through every vertex
# on the mesh and check if this joint is weighted. if it is we'll
# set the weight on our new mesh
for i in range(0, meshPntCnt) :
#Get the weight of the current vertex
weight = sourceWeights.GetWeight(sJntIndex, i)
# if the weight is greater than 0 lets set it on the new joint
if weight > 0:
targetWeights.SetWeight(dJntIndex, i, weight)
#print "Index ", i,": ", weight #debug
return
#Do the weight transfer
def TranferWeight(self) :
global sourceWeights
global targetWeights
#Gets the link to the source weight tag
sourceWeights = self.sourceLink.GetLink().GetTag(c4d.Tweights)
#Get the link to the target weight tag
targetWeights = self.targetLink.GetLink().GetTag(c4d.Tweights)
#The mesh point count
meshPntCnt = self.sourceLink.GetLink().GetPointCount()
# use a custom function to transfer joints
self.JointTransfer("abdomen", "abdomen")
self.JointTransfer("abdomen2", "abdomen2")
self.JointTransfer("chest", "chest")
self.JointTransfer("lPectoral", "l_Pectoral")
self.JointTransfer("rPectoral", "r_Pectoral")
self.JointTransfer("neck", "neck")
self.JointTransfer("head", "head")
self.JointTransfer("lEye", "l_Eye")
#self.JointTransfer("rEye", "r_Eye")
self.JointTransfer("pelvis", "pelvis")
self.JointTransfer("lThigh", "l_Thigh")
self.JointTransfer("lShin", "l_Shin")
self.JointTransfer("lFoot", "l_Foot")
self.JointTransfer("lToe", "l_Toe")
self.JointTransfer("lBigToe", "l_BigToe")
self.JointTransfer("rThigh", "r_Thigh_1")
self.JointTransfer("rShin", "r_Shin_1")
self.JointTransfer("rFoot", "r_Foot_1")
self.JointTransfer("rToe", "r_Toe_1")
self.JointTransfer("rBigToe", "r_BigToe_1")
self.JointTransfer("lCollar", "l_Collar")
self.JointTransfer("lShldr", "l_Shldr")
self.JointTransfer("lForeArm", "l_ForeArm")
self.JointTransfer("lHand", "l_Hand")
self.JointTransfer("lThumb1", "l_Thumb1")
self.JointTransfer("lThumb1", "l_Thumb2")
self.JointTransfer("lThumb1", "l_Thumb3")
self.JointTransfer("lCarpal1", "l_Carpal1")
self.JointTransfer("lCarpal2", "l_Carpal2")
self.JointTransfer("lIndex1", "l_Index1")
self.JointTransfer("lIndex2", "l_Index2")
self.JointTransfer("lIndex3", "l_Index3")
self.JointTransfer("lMid1", "l_Mid1")
self.JointTransfer("lMid2", "l_Mid2")
self.JointTransfer("lMid3", "l_Mid3")
self.JointTransfer("lRing1", "l_Ring1")
self.JointTransfer("lRing2", "l_Ring2")
self.JointTransfer("lRing3", "l_Ring3")
self.JointTransfer("rCollar", "r_Collar")
self.JointTransfer("rShldr", "r_Shldr_1")
self.JointTransfer("rForeArm", "r_ForeArm_1")
self.JointTransfer("rHand", "r_Hand_1")
self.JointTransfer("rThumb1", "r_Thumb1_1")
self.JointTransfer("rThumb1", "r_Thumb2_1")
self.JointTransfer("rThumb1", "r_Thumb3_1")
self.JointTransfer("rCarpal1", "r_Carpal1_1")
self.JointTransfer("rCarpal2", "r_Carpal2_1")
self.JointTransfer("rIndex1", "r_Index1_1")
self.JointTransfer("rIndex2", "r_Index2_1")
self.JointTransfer("rIndex3", "r_Index3_1")
self.JointTransfer("rMid1", "r_Mid1_1")
self.JointTransfer("rMid2", "r_Mid2_1")
self.JointTransfer("rMid3", "r_Mid3_1")
self.JointTransfer("rRing1", "r_Ring1_1")
self.JointTransfer("rRing2", "r_Ring2_1")
self.JointTransfer("rRing3", "r_Ring3_1")
c4d.EventAdd()
return True
dlg = transferW()
dlg.Open(c4d.DLG_TYPE_ASYNC, defaultw=300, defaulth=50)