Monday, November 3, 2014

Rigging (Art Eval): Scripting - Variable Looping Troubleshooting

This month, along with my two on campus classes I am also taking an online course which is my Art Evaluation for Rigging (Set Up). This evaluation preps me for my portfolio production and the creation of my demo reel. Each week, a milestone is due. There are four milestones total. Each milestone proposes it's own challenges and troubleshooting issues that I must figure out in order for the rig to work properly.

I was in need of creating a script that would create a control for the selected joint(s), orient that control the same way that the joint is oriented, put a pad on the control, put a parent constraint on the joint selected to the control, and parent the consecutive controls to one another.

How to run the script: Select a joint - run the script.
         The script should create 2 pad groups above a generated control.
         The pads and control should contain the name of the joint as part of their names. I don't care how you name your pads/icons/joints, as long as you are consistent.
         The control should have identical orientation as the joint.
         The joint should be constrained to the control
When I first ran the script, I would select each joint in the joint chain and run the script. As a result, I would get the correct grouping, padding, naming conventions, and orientation. However, the only joint that contained a parent constraint on it was the last joint selected.

Here is the script I was using initially:
'''
Shannon Gilmore
Gilmore_Shannon_ArtEvaluation_Campus1411.py

Description:
Rigging Tools - Milestone 2

import Gilmore_Shannon_ArtEvaluation_Campus1411
reload(Gilmore_Shannon_ArtEvaluation_Campus1411)
''' 

import pymel.core as pm

# On selected joints.
# Create a local oriented control.
joints = pm.ls(selection=True)

# Loop through selected joints
for current_joint in joints:
    # Rename control and delete history
    #ct_back_01_bind -> ct_back_01_icon
    icon_name = current_joint.replace('_bind', '_icon')
    pad_name = current_joint.replace('_bind', '_pad')
    group_name = current_joint.replace('_bind', '_group')
    

    # Create a circle
    control_icon = pm.circle(name=icon_name, radius=.6, normal=[1, 0, 0])[0]
    
    # Create a group
    pad = pm.group(name=pad_name)
    
    # Create Orient Group
    group = pm.group(name=group_name)
    
    # Snap group to target joint.
    # Snapping using parent constraint.
    kenny = pm.parentConstraint(current_joint, pad)
    
    # Delete parent constraint.
    pm.delete(kenny)
    
    # Parent Constraint Joint to Control
    pm.parentConstraint(control_icon, joints, mo=True, w=1)


What I didn't realize was that I was not including the pieces of the script that told Maya that it needed to loop through the controls.
I also needed to add in the scripting that tells Maya to parent the control to it's previous pad. I reached out to one of my teachers for an insight of what could possibly be the problem, when he told me it was an issue with looping variables. From this, I elaborated on the script a little bit, double checked all of the names that were used and where they were being placed, and I finally made the script a successful one.

Here is the final script with it's new changes:
'''
Shannon Gilmore
Gilmore_Shannon_ArtEvaluation_Milestone2_Scripting_Campus1411.py

Description:
    Grouping the controls.
    Naming and orienting controls.
    Putting pads on controls.
    Constraining joints to controls.

How to Run:

import Gilmore_Shannon_ArtEvaluation_Milestone2_Scripting_Campus1411
reload(Gilmore_Shannon_ArtEvaluation_Milestone2_Scripting_Campus1411)

'''

import pymel.core as pm

# Select joint chain.
# On selected joints.
# Create a local oriented control.
joint_system = pm.ls(selection=True, dag=True)

last_control = None

# Loop through selected joints
for current_joint in joint_system[0:-1]:
    # Rename control and delete history
    #ct_back_01_bind -> ct_back_01_icon
    icon_name = current_joint.replace('_bind', '_icon')
    pad_name = current_joint.replace('_bind', '_pad')
    group_name = current_joint.replace('_bind', '_group')
    

    # Create a circle
    control_icon = pm.circle(name=icon_name, radius=.6, normal=[1, 0, 0])[0]
    
    # Create a group
    pad = pm.group(name=pad_name)
    
    # Create Orient Group
    group = pm.group(name=group_name)
    
    # Snap group to target joint.
    # Snapping using parent constraint.
    kenny = pm.parentConstraint(current_joint, pad)
    
    # Delete parent constraint.
    pm.delete(kenny)
    
    # Parent Constraint Joint to Control
    pm.parentConstraint(control_icon, current_joint, mo=True, w=1)
    
    if last_control:
        pm.parent(pad, last_control)
        

    last_control = control_icon

No comments:

Post a Comment