Renaming Blendshape Targets

Came across an issue when I tried to use the renameAttr command to rename targets on a blendshape node in Maya. After a lot of digging, it didn’t seem like anyone had a solid solution in Python written out.

Here’s the one that I wrote up for my purposes, feel free to use…

import maya.cmds

def rename_morph_targets( node_name, attr_dict, quiet = False ):
	# list to catch our failures	
	fail_list = [ ]

	# How many targets there are in the alias list
	number_of_targets = maya.cmds.getAttr( '{0}.weight'.format( node_name ), size = True )

	# Iterate through the weight list
	for index in range( 0, number_of_targets ):
		# Query the name of the current blendshape weight
		old_name = maya.cmds.aliasAttr( '{0}.weight[{1}]'.format( node_name, index ), query = True )

		# If the old name isn't in the attr_dict, we're going to pass on it.
		if old_name in attr_dict.keys( ):
			# We're going to rename
			new_name = attr_dict[ old_name ]
			print 'Found old name: ', index, new_name, old_name
			
			absolute_name = '{0}.weight[{1}]'.format( node_name, index )

			maya.cmds.aliasAttr( new_name, absolute_name ) # Re-aliasing / Renaming occurs here.
			if not quiet:
				print 'Changed {0} -> {1}'.format( old_name, new_name )
				
		# Add the failure to the fail list		
		else:
			fail_list.append( old_name )

	if fail_list:
		maya.cmds.warning( '{0} names were not changed. Check console for details'.format( len( fail_list ) ) )
		for name in fail_list:
			print name

###################
## Example Usage ##
###################
	
attr_dict = { 'old_name': 'new_name',
                 'another_name': 'another_new_name' }

node_name = '' # Name of the blendshape
rename_morph_targets( node_name, attr_dict )

 

Leave a Reply

Your email address will not be published. Required fields are marked *