Python For Maya additional Tips

Here is a small list of additional tips for python script for maya, maybe expand in the future.

1. You can call the type() function to get the type of your current variables or literals.

e.g. type(1)  type(x)

2. You can call the dir() function to get all the related attributes of specific objects.

e.g. dir(x)

3. You can call the help() function to get the instruction about specific function.

e.g. help(dir)       help(x.bit_length)

4. If you want to get access to multiple list members, you can use colon to set the range:

myList = [0, 2.5, “you”]

myList[0:2] means the first element and the third element, in this case, 0 and “you”

You can also assign a negative index to represent that the count is from the end to the start.

myList[-1] is the last element of the list, it is “you”

5. A string is a special list. You can also get access with string element by using the method listed in 4. But you cannot assign new values to a string member.

s = “Hello world”

s[0] should be “H”, while s[2:5] should be “llo”

s[1] = “K” is not valid

6. tuple is similar to list, except it cannot be modified once created. When you create a tuple, you need to use parenthesis instead of square brackets.

mtTuple = (0, 2, “K”)

myTuple[0] is 0

7. Python has dictionary, which is very useful for organizing different data sets. You can define a dictionary with braces. In a dictionary, you can save both keys and values. Each key will corresponding to a specific values. The value can be a list or a list of list, which means you can easily save multiple dimension data in a dictionary.

d = {“red” : 1, “blue” : 2}

d[“red”] should be 1

You can even modify the value manually:

d[“blue”] = 16

You can simply add a new key in a dictionary by:

d[“purple”] = 12

Delete a key is simple as well:

del d[“red”] or d.pop[“red”]

8. Python has its own module, which is like a library in C/C++. You can import new module using import command. Or you can only import a specific function from a module by :

from XX import YY

from os import path

9. while key word is used for loops as well.

while i<10:

print i

i++

10. You can open show quick help option in maya script editor (under command tag). You can get quick reference for all mel command.  I personally more prefer official manual.

0030QiC3ty6McfNDcV9ff

 

By Jun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s