Let's learn how to leverage Python to write and load JSON files. In this mini course, we'll use the awesome technique to load control sizes, in case our rig is ever rebuilt.
And, to learn more about auto-rigging in Maya, have a look at this course:
from maya import cmds
import json
'''
Save Control Sizes - Python/JSON
email example:me@me.com
'''
#Get Data Path
data_path = cmds.workspace(q=True, rd=True) + 'data/'
print(data_path)
#Get all controls to eventually store their radius values
cmds.select('*_anim', r=True)
ctrls = cmds.ls(sl=True, type='transform')
#Create an empty list to append the data we'll store in our JSON file
ctrl_rad_json = []
for ctrl in ctrls:
#Create a variable to store each control's radius channel + the keys we'll use to call upon the radius values in our JSON file
ctrl_rad = ctrl + '.radius'
#Get the radius of each control
ctrl_rad_val = cmds.getAttr(ctrl_rad)
#Store both the radius channels and their values in variable "ctrl_rad_data"
ctrl_rad_data = [ctrl_rad, ctrl_rad_val]
#Append the data in our empty list "ctrl_rad_json" to extract it from the for loop
ctrl_rad_json.append(ctrl_rad_data)
#Convert List to Dictionary
ctrl_rad_json = dict(ctrl_rad_json)
#Write our JSON file
with open(data_path + 'set_ctrl_radius.json', 'w', encoding='utf-8') as ctrl_rad_file:
json.dump(ctrl_rad_json, ctrl_rad_file)
from maya import cmds
import json
'''
Load Control Sizes - Python/JSON
'''
#Get Data Path
data_path = cmds.workspace(q=True, rd=True) + 'data/'
#Store JSON Data in variable "jsctrlrad"
with open(data_path + 'set_ctrl_radius.json', 'r') as ctrl_rad:
jsctrlrad = json.load(ctrl_rad)
'''
Store all control objects + the path to their radius channels
'''
cmds.select('*_anim', r=True)
ctrls = cmds.ls(sl=True, type='transform')
#Find amount of controls stored in variable "ctrls"
size = len(ctrls)
for i in range(size):
#Create a variable to store each control's radius channel + the keys in our JSON file
ctrl_rad = ctrls[i] + '.radius'
#Iteratively set the value of each radius channel using the keys stored in our JSON file
cmds.setAttr(ctrl_rad, jsctrlrad[ctrl_rad])