Skip to main content

Space Switch

import maya.cmds as cmds
import pymel.core as pm

def create_space_switch(child, parents, enum_con):
"""
Creates space switching for a given child with specified parents.

Args:
child (str): The name of the child control (e.g., 'hand_IK_con').
parents (list): A list of parent controls (e.g., ['chest_con', 'hips_con', 'root_con']).
"""

# Add enum attribute to the child
enum_attr_name = "space"
if not cmds.attributeQuery(enum_attr_name, node=enum_con, exists=True):
enum_attr = cmds.addAttr(enum_con, longName=enum_attr_name, attributeType="enum", enumName=":".join(parents))
cmds.setAttr(f"{enum_con}.{enum_attr_name}", keyable=True)

# Create a parent constraint without maintaining offset
constraint = cmds.parentConstraint(parents, child, maintainOffset=True)[0]

# Create a condition node for each parent
for i, parent in enumerate(parents):
condition_node = cmds.createNode('condition', name=f"{child}_to_{parent}_condition")

# Set the condition node attributes
cmds.setAttr(f"{condition_node}.colorIfTrueR", 1)
cmds.setAttr(f"{condition_node}.colorIfFalseR", 0)
cmds.setAttr(f"{condition_node}.secondTerm", i)

# Connect the space enum attribute to the condition's first term
cmds.connectAttr(f"{enum_con}.{enum_attr_name}", f"{condition_node}.firstTerm")

# Connect the condition output to the corresponding weight in the parent constraint
cmds.connectAttr(f"{condition_node}.outColorR", f"{constraint}.w{i}")

# Example usage:
child_control = "arm_IK_L_con_grp"
parent_controls = ["root_con", "body_con"]
enum_con = "arm_IK_L_con"
create_space_switch(child_control, parent_controls, enum_con)
import maya.cmds as cmds
import pymel.core as pm

def create_space_switch(child, parents):
"""
Creates space switching for a given child with specified parents.

Args:
child (str): The name of the child control (e.g., 'hand_IK_con').
parents (list): A list of parent controls (e.g., ['chest_con', 'hips_con', 'root_con']).
"""
# Ensure the child exists
if not cmds.objExists(child):
raise ValueError(f"Child control '{child}' does not exist.")

# Ensure all parents exist
for parent in parents:
if not cmds.objExists(parent):
raise ValueError(f"Parent control '{parent}' does not exist.")

# Add enum attribute to the child
enum_attr_name = "space"
if not cmds.attributeQuery(enum_attr_name, node=child, exists=True):
enum_attr = cmds.addAttr(child, longName=enum_attr_name, attributeType="enum", enumName=":".join(parents))
cmds.setAttr(f"{child}.{enum_attr_name}", keyable=True)

# Create a parent constraint without maintaining offset
constraint = cmds.parentConstraint(parents, child, maintainOffset=True)[0]

# Create a condition node for each parent
for i, parent in enumerate(parents):
condition_node = cmds.createNode('condition', name=f"{child}_to_{parent}_condition")

# Set the condition node attributes
cmds.setAttr(f"{condition_node}.colorIfTrueR", 1)
cmds.setAttr(f"{condition_node}.colorIfFalseR", 0)
cmds.setAttr(f"{condition_node}.secondTerm", i)

# Connect the space enum attribute to the condition's first term
cmds.connectAttr(f"{child}.{enum_attr_name}", f"{condition_node}.firstTerm")

# Connect the condition output to the corresponding weight in the parent constraint
cmds.connectAttr(f"{condition_node}.outColorR", f"{constraint}.w{i}")

# Example usage:
child_control = "arm_IK_L_con_grp"
parent_controls = ["root_con", "body_con"]
create_space_switch(child_control, parent_controls)


import maya.cmds as cmds
import pymel.core as pm

def create_space_switch(child, parents, enum_con):
"""
Creates space switching for a given child with specified parents.

Args:
child (str): The name of the child control (e.g., 'hand_IK_con').
parents (list): A list of parent controls (e.g., ['chest_con', 'hips_con', 'root_con']).
"""
# # Ensure the child exists
# if not cmds.objExists(child):
# raise ValueError(f"Child control '{child}' does not exist.")

# # Ensure all parents exist
# for parent in parents:
# if not cmds.objExists(parent):
# raise ValueError(f"Parent control '{parent}' does not exist.")

# Add enum attribute to the child
enum_attr_name = "space"
if not cmds.attributeQuery(enum_attr_name, node=enum_con, exists=True):
enum_attr = cmds.addAttr(enum_con, longName=enum_attr_name, attributeType="enum", enumName=":".join(parents))
cmds.setAttr(f"{enum_con}.{enum_attr_name}", keyable=True)

# Create a parent constraint without maintaining offset
constraint = cmds.parentConstraint(parents, child, maintainOffset=True)[0]

# Create a condition node for each parent
for i, parent in enumerate(parents):
condition_node = cmds.createNode('condition', name=f"{child}_to_{parent}_condition")

# Set the condition node attributes
cmds.setAttr(f"{condition_node}.colorIfTrueR", 1)
cmds.setAttr(f"{condition_node}.colorIfFalseR", 0)
cmds.setAttr(f"{condition_node}.secondTerm", i)

# Connect the space enum attribute to the condition's first term
cmds.connectAttr(f"{enum_con}.{enum_attr_name}", f"{condition_node}.firstTerm")

# Connect the condition output to the corresponding weight in the parent constraint
cmds.connectAttr(f"{condition_node}.outColorR", f"{constraint}.w{i}")

# Example usage:
child_control = "arm_IK_R_con_grp"
parent_controls = ["root_con", "body_con"]
enum_con = "arm_IK_R_con"
create_space_switch(child_control, parent_controls, enum_con)


import maya.cmds as cmds
import pymel.core as pm

def create_space_switch(child, parents, enum_con):
"""
Creates space switching for a given child with specified parents.

Args:
child (str): The name of the child control (e.g., 'hand_IK_con').
parents (list): A list of parent controls (e.g., ['chest_con', 'hips_con', 'root_con']).
"""
# # Ensure the child exists
# if not cmds.objExists(child):
# raise ValueError(f"Child control '{child}' does not exist.")

# # Ensure all parents exist
# for parent in parents:
# if not cmds.objExists(parent):
# raise ValueError(f"Parent control '{parent}' does not exist.")

# Add enum attribute to the child
enum_attr_name = "space"
if not cmds.attributeQuery(enum_attr_name, node=enum_con, exists=True):
enum_attr = cmds.addAttr(enum_con, longName=enum_attr_name, attributeType="enum", enumName=":".join(parents))
cmds.setAttr(f"{enum_con}.{enum_attr_name}", keyable=True)

# Create a parent constraint without maintaining offset
constraint = cmds.parentConstraint(parents, child, maintainOffset=True)[0]

# Create a condition node for each parent
for i, parent in enumerate(parents):
condition_node = cmds.createNode('condition', name=f"{child}_to_{parent}_condition")

# Set the condition node attributes
cmds.setAttr(f"{condition_node}.colorIfTrueR", 1)
cmds.setAttr(f"{condition_node}.colorIfFalseR", 0)
cmds.setAttr(f"{condition_node}.secondTerm", i)

# Connect the space enum attribute to the condition's first term
cmds.connectAttr(f"{enum_con}.{enum_attr_name}", f"{condition_node}.firstTerm")

# Connect the condition output to the corresponding weight in the parent constraint
cmds.connectAttr(f"{condition_node}.outColorR", f"{constraint}.w{i}")

# Example usage:
child_control = "arm_IK_R_con_grp"
parent_controls = ["root_con", "body_con"]
enum_con = "arm_IK_R_con"
create_space_switch(child_control, parent_controls, enum_con)