How to write a python script that allows uploading for a CSV file to rename blendshapes

How to write a python script that allows uploading for a CSV file to rename blendshapes

gtn223
Explorer Explorer
2,326 Views
7 Replies
Message 1 of 8

How to write a python script that allows uploading for a CSV file to rename blendshapes

gtn223
Explorer
Explorer

Hello,

 

This is a question for face tracking apple ARkit. I am trying to write python script in the Maya script editor that can have the ability to upload a csv and rename all the target blend shape groups. In the csv file, I basically have two columns one is the blendshape names you see in the picture and the other column is Apple's ARkit blendshape names. I basically want to do a find and replace function within Maya, but I am having trouble on where to begin or how to best approach this. 

 

reallusion8.png

 

I have this working as a batch program to run on the terminal, I am trying to see if this could be done within Maya to save extra steps for renaming blendshapes. 

 

Thank you for your time! 

0 Likes
2,327 Views
7 Replies
Replies (7)
Message 2 of 8

jmreinhart
Advisor
Advisor

I haven't done this with CSV files before but this approach should work.

You can load the CSV as a python list.

https://www.journaldev.com/30140/parse-csv-files-in-python

And then you can use the cmds.aliasAttr() command to rename the blendshape targets.

0 Likes
Message 3 of 8

gtn223
Explorer
Explorer

Thanks for sending this! I am still getting an error in maya,  I managed to hack this semi functional script that is ready to rename, I am just missing the last part that takes replaces the names in columns "Apple" and "Non-apple" basically renaming the blendshape names in the non-apple column to the names in the apple column in my csv file.

import csv
import sys
import maya.cmds


path = "E:\Reallusion\Pythonscript\Reallusion.csv"
file = open(path)
reader = csv.reader(file)

header = next(reader) #The first line
data = [row for row in reader]


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 test
# renaming function goes here to rename the one column to the other column



print(header)
print(data[0])

If you know someone who can elaborate that would be great! 

0 Likes
Message 4 of 8

jmreinhart
Advisor
Advisor

Which part is not working for you? Turning the CSV into a python dict? The link I originally sent gives an explanation of the syntax required to get CSV converted into a python list, are you having trouble converting that into a dictionary?

0 Likes
Message 5 of 8

gtn223
Explorer
Explorer

Yes, exactly. I am a noob with python. I am trying to run it in maya, there's no errors the renaming wont work. 

import csv
import sys
import maya.cmds


path = "E:\Reallusion\Pythonscript\Reallusion.csv"

header = next(reader) #The first line
data = [row for row in reader]

with open(path, 'r') as file:
    Filedata = file.read()
    csv_reader = csv.reader(csv_file, delimiter=',')
    apple_names = []
    fuse_names = []
    for lines in csv_reader:
      apple_names.append(lines[0])
      fuse_names.append(lines[1])
	
	


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

# running rename hard coding version


for idx,name in enumerate(fuse_names):
    filedata = filedata.replace(name, apple_names[idx])
    
with open(path, 'w') as file:
    file.write(filedata)
        
        
attr_dict = {apple_names.append(lines[0])}         

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

 

0 Likes
Message 6 of 8

jmreinhart
Advisor
Advisor
attr_dict = dict(zip(apple_names,fuse_names)

this is how you turn the seperate lists into a dictionary

0 Likes
Message 7 of 8

gtn223
Explorer
Explorer

**** getting a syntax error, thank you for your time on this! We figured out another solution tho!

import csv
import sys
import maya.cmds
path = "wherever the csv file is on your computer"
node_name = 'CC_Base_Body_ncl1_1' # Name of the blendshape for Reallusion
node_name_tongue = 'CC_Base_Tongue_ncl1_1' # Name of the blendshape for Reallusion (tongue)
with open(path, "r") as csv_file:
    next(csv_file)                  #skip headers
    csv_reader = csv.reader(csv_file, delimiter=',')
    for lines in csv_reader:
        if 'Tongue' in lines[0]:
            maya.cmds.aliasAttr(lines[1],node_name_tongue+'.'+lines[0])
        else:
            maya.cmds.aliasAttr(lines[1],node_name+'.'+lines[0])
csv_file.close()

 This is working, but I just need to make an except: for an error that pops up, it works and renames the blendshapes, now I just need to figure out how to make the error silent or exception

0 Likes
Message 8 of 8

jmreinhart
Advisor
Advisor

Cmds.error() may suit your needs