Michael B. Comet poseReader converted to Python Render Selected Meshes Region
May 18

In response to an e-mail I received I wrote back this response. I figured I would post it.

So I was given this example and asked what the u charter was for in front of each string name returned in the list.


aList = cmds.ls(selection=True)
print aList

>>[u'pSphere1', u'pSphere2', u'pSphere3', u'pSphere4']

The long and the short of it is Unicode allows more characters than ASCII. ASCII can hold 256 values and Unicode can hold 65,536 values. This allows computers to support complex languages with many characters. If you want a more complex explanation go here:

http://www.amk.ca/python/howto/unicode

Getting back to the example. So the aList returns a list of string names using Python Unicode. Thats what the u stands for. It shouldnt bother you though nor affect anything.

So if you have two spheres in your scene and you run this in the script editor it will return u’pSphere2′ and u’pSphere1′ like this:


import maya.cmds as cmds

getlist = cmds.ls(sl=True)
print getlist
>>[u'pSphere2', u'pSphere1']

You should not need to worry about this because Maya automatically converts back to regular strings when you get the items out of the list:


for node in getlist:
    print node
>>pSphere2
>>pSphere1

Notice maya converts the string to a normal string when you print each item in the list. Or if you print just one item like this:


print getlist[0]
>>pSphere2

You could do this though, even though you shouldn’t need to do this, unless linux or apple computers work differently. I am not sure.


import maya.cmds as cmds

getlist = cmds.ls(sl=True)
print len(getlist)

for i in range( 0, len(getlist) ):
    getlist[i] = getlist[i].encode("ascii","ignore")

print getlist
>>['pSphere1', 'pSphere2']

As for the second question:

“Is there a way to fit arguments into the command?”


def fooB (var = 1.0):
cmds.button(command=fooB(var = 9.5) )       	# won't work
cmds.button(command=fooB)                 	# but this will work?

The reason the first method wont work is that the button command is getting confused as to what arguments are being passed to it. The second works but the command thinks it is a string not a function. You can make the first work by passing the command a string containing what you are passing it. To further that thought you can pass it other variables as well.


#Example 1

# this works because it is inside of a string so the command
# clearly understands what is passed to it
def fooB (var = 1.0)
cmds.button(command='fooB(var = 9.5)' )

#Example 2

# this works as long as the functions doesnt expect an argument,
# since you are using keyword like var = 1.0 you dont have to pass
# anything to the function
def fooB (var = 1.0)
cmds.button(command='fooB()') 

#Example 3

# this also works but gets much harder to read whats going on
fooA = 9.5
def fooB (var = 1.0)
cmds.button(command=('fooB(var = ' + fooA + ')'), label='something' )

In example 3 I surround the multiple strings with brackets ( and ) so that it is clear to the command what is to be passed to it. Think of it like brackets in MEL you are defining a clump of data to be passed to the command. Last thing to note is only strings can be passed to a command so you need to type str(fooA) to cast the 9.5 into a string like this “9.5″ so it can be added to the ‘fooB(var = ‘ and ‘)’ string. Now this is getting more complex to read.

If you get to the point you want to do this, you probably need to start reading data from your UI.
For example you could create a floatField like this:


import maya.cmds as cmds

window = cmds.window()
cmds.columnLayout()
cmds.floatField('myFloatFieldA', minValue=-10, maxValue=10, value=5 )
cmds.showWindow( window )

Since I defined the float field with a name ‘myFloatFieldA’ you can ask the UI what the value is in your script editor or a function.


cmds.floatField('myFloatFieldA', query=True, value=True )

Then instead of using:


fooA = 9.5
def fooB (var = 1.0)
cmds.button(command=('fooB(var = ' + fooA + ')'), label='something' )

You can do this:


import maya.cmds as cmds

def fooB ():
	myFloatValue = cmds.floatField('myFloatFieldA', query=True, value=True )
	print myFloatValue

window = cmds.window()
cmds.columnLayout()
cmds.floatField('myFloatFieldA', minValue=-10, maxValue=10, value=5 )
cmds.button(command='fooB()', label='something' )
cmds.showWindow( window )

If you press the button it will print the value in the float field.

Cheers,
Ryan

Leave a Reply