Copying Channel Box information between objects

I’ve spent a lot of time in Unity and as anyone who’s spent a lot of time in Unity, you’re using the Transform component quite a lot. One thing that I’ve always like about Unity’s Inspector is the ability to copy a component and paste it either as a new component on another object, or just copy the component values. I’m in the middle of doing some hard surface modeling and things need to line up pretty accurately. The manual way of getting Channel Box information from one object to another is first selecting the object that needs to be adjust, shift select the other you want to adjust to and then tab-enter your way through the Channel Box. It works and it’s actually quite fast. But it’s not the one click solution I’m usually after.

Steps to use this script:

  • Select the object you want to adjust all other objects to.
  • Shift select any other object(s).
  • Either select the channels you want to copy in the Channel Box, or select nothing to copy everything.
  • Run the script.

You can save the following script to your shelf to use it.

import maya.cmds as cmds

selection = cmds.ls(selection = True)
attributes_to_copy = []


if len(selection) < 1:
	cmds.warning("Select at least 2 objects")
	sys.exit()

parent_object = selection[0]
other_objects = selection[1:len(selection)]

selected_main_attributes = cmds.channelBox("mainChannelBox", query = True, selectedMainAttributes = True)

print "selected main attributes are "
print selected_main_attributes

if selected_main_attributes == None:
	attributes_to_copy = cmds.listAttr(parent_object, keyable = True)
else:
	for selected_attribute in selected_main_attributes:
		attributes_to_copy.append(selected_attribute)

print attributes_to_copy

for otherObject in other_objects:
	try:
		for attribute in attributes_to_copy:
				value = cmds.getAttr(parent_object + "." + attribute)	
				cmds.setAttr(otherObject + "." + attribute, value)

	except:
		print "Can't set attribute " + str(attribute) + " with value: " + str(value)
		print "Copying from : " + parent_object
		print "Copying to : " + otherObject