Pymel Guidance

I’ve been experimenting with pymel in an attempt to build better and more flexible tools in Maya. The problem I’m running into is my constant inability to grasp object oriented scripting. I’m still building my tools procedurally and I don’t feel I’m taking full advantage of pymel.

Rigging Tool

This is the current tool I am working on to help me with pymel. All it does is create a control on an FK joint. Not really impressive, but it’s very useful in certain situations. I have stopped to see if anyone could give some more information on how to make the script more efficient and how to use the full strength of pymel.

I also left some comments in areas that I specifically want to get advice on.

Any help would be great. thanks!

Brandon L. Harris

This is the code I use in our rig tools for creating color swatches.


cmds.gridLayout(nc=16, cwh=(35, 35))
cmds.canvas(rgb=[0.62745100259780884, 0.62745100259780884, 0.62745100259780884], pc='print 0')
for i in range(1, 32):
	color = cmds.colorIndex(i, q=True)
	cmds.canvas(rgb=color, pc=('print '+str(i)))

On a side note, look into the Callback Class that pymel provides (I don’t actually use pymel, but that class is a life saver) It let’s you use functions instead of string as arguments, but still pass in arguments to the function.

Here’s my modified version of the Callback class:

class Callback():
	_callData = None
	@staticmethod
	def _doCall():
		(func, args, kwargs) = Callback._callData
		Callback._callData = func(*args, **kwargs)
	
	def __init__(self,func,*args,**kwargs):
		self.func = func
		self.args = args
		self.kwargs = kwargs
	def __call__(self,*args):
		Callback._callData = (self.func, self.args, self.kwargs)
		Callback._doCall()
		return Callback._callData 

Example of how to use it:

def EH_button_pressed(whichButton):
	print 'Button pressed', whichButton

cmds.window()
cmds.columnLayout()
for i in range(15):
	cmds.button(l='button '+str(i), c=Callback(EH_button_pressed, i))
cmds.showWindow()

You can use that Callback class to make the swatches all call the same function when they are clicked, but with a different color as an arg.