maya duplicating and parenting joints by python not working?

maya duplicating and parenting joints by python not working?

Anonymous
Not applicable
738 Views
1 Reply
Message 1 of 2

maya duplicating and parenting joints by python not working?

Anonymous
Not applicable

 

I'm creating multiple joints by code in maya, this is what I want to do. Create, and parent them like this...

L_Arm_00IK parent of L_Arm01IK parent of L_Arm02IK

L_Arm_00FK parent of L_Arm01FK parent of L_Arm02FK

L_Arm_00IKDriver parent of L_Arm01IKDriver parent of L_Arm02IKDriver

L_Arm_00Blend parent of L_Arm01Blend parent of L_Arm02Blend

 

but when I run my code, the First joints are created L_Arm_00IK, L_Arm_00FK, L_Arm_00IKDriver, L_Arm_00Blend

but their children aren't created. What am I doing wrong?

 

Below is the code.

 

 

def ikfkchain(name, side, parent, joints, fktemplate, iktemplate, pvtemplate, fkcolor, ikcolor):


fk_joints = duplicate_chain(joints, None, "_JNT", "Fk_JNT")
ik_joints = duplicate_chain(joints, None, "_JNT", "Ik_JNT")
ik_driver_joints = duplicate_chain(joints, None, "_JNT", "IkDriver_JNT")
blend_joints = duplicate_chain(joints, None, "_JNT", "Blend_JNT")

 



def duplicate_chain(joints, parent, replace_what, replace_with):
new_joints = []
new_joints_parent = parent

for jnt in joints:
new_jnt = cmds.duplicate(jnt, n=jnt.replace(replace_what, replace_with), po=True)[0]
if new_joints_parent:
cmds.parent(new_jnt, new_joints_parent)


new_joints_parent = new_jnt
new_joints.append(new_jnt)

return new_joints

0 Likes
739 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

Your script works. My guess is that you are not feeding the right input when you call it. Which you sadly didn't post.
You are very reliant on args and don't use any kwargs, which makes it hard to keep track on what input your arguments are catching.

By the way, you can use the </> button above the comment field to create a code block, that way your indentation doesn't get messed up.

import maya.cmds as cmds

def ikfkchain(name, side, parentObj, joints):


    fk_joints = duplicate_chain(joints, None, "_JNT", "Fk_JNT")
    ik_joints = duplicate_chain(joints, None, "_JNT", "Ik_JNT")
    ik_driver_joints = duplicate_chain(joints, None, "_JNT", "IkDriver_JNT")
    blend_joints = duplicate_chain(joints, None, "_JNT", "Blend_JNT")


def duplicate_chain(joints, parentObj, replace_what, replace_with):
    new_joints = []
    new_joints_parent = parentObj

    for jnt in joints:
        new_jnt = cmds.duplicate(jnt, n=jnt.replace(replace_what, replace_with), po=True)[0]
        if new_joints_parent:
            cmds.parent(new_jnt, new_joints_parent)


        new_joints_parent = new_jnt
        new_joints.append(new_jnt)

    return new_joints

ikfkchain(None,None,None,["Shoulder_JNT","Elbow_JNT","Wrist_JNT"])

I took out all the args behind "joints" for testing, because they weren't relevant for this portion of the script. I also changed "parent" to "parentObj" because "parent" is a defined MEL function, this shouldn't cause any problems but it is just good practice to not name your variables after already defined objects for clarity.

 

At the end of the code you can see how I call the function. I fill up the arguments before "joints" with empty objects. Then I feed the joints argument a list with the string names of the joints. If you don't pack them into a list like this, which I'm guessing is what you did.

ikfkchain(None,None,None,"Shoulder_JNT","Elbow_JNT","Wrist_JNT")

Your child joints just get caught by the following args (fktemplate, iktemplate, pvtemplate, fkcolor, ikcolor) and it will only give you an error if you have 6 or more joint names, because then your function will run out of expected arguments.

 

I hope this helps!

0 Likes