Maya Python – Mirror CVs

This script takes two identical controls and mirrors them across the X axis. This is useful if you have a NURBs control on the left side that you’ve tweaked and want to update the right side control.

def CVList(obj):
	for CV in obj:
		cmds.select (obj +'.cv[*]')
		lCVs=cmds.ls(sl=True, fl=True)
	return lCVs
 
def MirrorCVs(): #select obj then target
	sel = cmds.ls(sl=True)
	#get list of cvs from obj1
	obj1 = sel[0]
	obj2 = sel[1]
	lCVs = CVList(obj1)
 
	dCVs = {} #build dictionary from cv# and translation from obj1
	for cv in lCVs:
		CVTrans = cmds.xform(cv, query=True, ws= True, translation = True)   
		dCVs[cv] = (-CVTrans[0], CVTrans[1], CVTrans[2])
 
	lObj2 = CVList(obj2) #iterate thru cvs of obj2 & apply translations
 
	for i, val in enumerate(lObj2): #apply translation to 2nd obj
		cmds.xform(val, ws=True, translation = dCVs[lCVs[i]])
 
	cmds.select(d=True)
 
def Main():
	MirrorCVs()
 
Main()

Maya Python – Querying a Radio Button

Hi & happy new year. To start off I’m going to add some Maya – Python snippets here that I find useful, especially to a beginner. This is for a basic radio button and accessing the state of it.

import maya.cmds as cmds
 
def QueryResults(grp):
	grpResults = cmds.radioButtonGrp(grp,query=True, select=True) #select will return integer based on radio selection
	if grpResults is 1:
		side = 'LEFT'
	if grpResults is 2:
		side = 'RIGHT'
	print ('Congrats! You have picked ' + side + '.')
 
def Main():
	#GUI -----------------------------
	if cmds.window('testQueryRadioButtonWindow', exists=True):
		cmds.deleteUI('testQueryRadioButtonWindow', window=True) 
 
	cmds.window('testQueryRadioButtonWindow', title='testQueryRadioButton', h=200, w=400)
	cmds.columnLayout()
 
	cmds.rowLayout(numberOfColumns = 1)
	myRadioGrp = cmds.radioButtonGrp( label='Choose side:', labelArray2=['LEFT', 'RIGHT'], select=1, numberOfRadioButtons=2 )
	cmds.setParent('..')
 
	cmds.rowLayout(numberOfColumns = 2)
	cmds.button(bgc=(0,1,.25), label='Query', command= lambda *args: QueryResults(myRadioGrp))
	cmds.setParent('..')
 
	cmds.setParent('..')
	cmds.showWindow('testQueryRadioButtonWindow')
 
Main()