Today, I finished reading Chapter 3, which is faster than my plan. Because this chapter focuses on lots of iteration and branching which is familiar with me. I wrote a small project to attach texture on my model. Here is the result:
From this chapter, I have a firm grasp of many of the fundamentals of programming with Python, including ins and outs of functions, loops, branching and error handling. But to be honest, I'm still only scratching the surface of Python in Maya.
So far, I already learned some basic Maya commands and the essentials for understanding data and variables in Python. Moreover, I walk through a simple texture-processing framework for Maya. Now, I'm capable of crating Python programs, the next step is to organize them into modules, which is exactly the content of Chapter 4.
Friday, February 15, 2013
Monday, February 11, 2013
Found a Good Webesite
Today, I found a really useful website, which I could search every command in Python. I believe this website will help me a lot in the future O(∩_∩)O
http://docs.python.org/2/library/

http://docs.python.org/2/library/

Friday, February 8, 2013
Listing and Selecting Nodes
1) maya.cmds.1s()
retrieving a list of nodes in the scene
Example1:
import maya.cmds;
nodes = maya.cmds.ls(type='transform');
print(nodes); //result: [u'front', u'persp', u'side', u'top']
pass a string specifying the type of objects you would like in your list
Example2:
nodes = maya.cmds.ls('persp*');
print(nodes); //result: [u'persp', u'perspShape']
2) maya.cmds.select()
populate the current global selection list
maya.cmds.select('side*', 'top*'); //select the transform and shape nodes for the top and side
cameras.
print(maya.cmds.ls(selection=True)); //result: [u'side', u'sideShape', u'top', u'topShape']
retrieving a list of nodes in the scene
Example1:
import maya.cmds;
nodes = maya.cmds.ls(type='transform');
print(nodes); //result: [u'front', u'persp', u'side', u'top']
pass a string specifying the type of objects you would like in your list
Example2:
nodes = maya.cmds.ls('persp*');
print(nodes); //result: [u'persp', u'perspShape']
2) maya.cmds.select()
populate the current global selection list
maya.cmds.select('side*', 'top*'); //select the transform and shape nodes for the top and side
cameras.
print(maya.cmds.ls(selection=True)); //result: [u'side', u'sideShape', u'top', u'topShape']
3) Conjunction ls & select
selection_list = ['front', 'persp', 'side', 'top']; //create a list of items to select
maya.cmds.select(selection_list);
print(maya.cmds.ls(sl=True));
maya.cmds.select(maya.cmds.ls(type='shape')); //select all of the shape nodes in the scene
print(maya.cmds.ls(sl=True));
Wednesday, February 6, 2013
Function Arguments
1) Template: def function_name(optional, input, parameters):
pass;
2) Default Arguments: By assigning the default argument in the function's declaration, you are ensuring that the prefix input parameter will always have a value, even if one is not supplied when the function is called
Example:
def process_all_textures(texture_node, prefix='my_'):
print('Processed%s%s'%(prefix, texture_node));
process_all_textures(texture); //result: Processed my_file1
3) Positional Argument(位置参数): not real argument, just declare there has a argument and pass data.
4) keyword Arguments (关键字参数): Prefix value for argument so that even you do not pass value to the keyword argument, it still has value.
5) Two issues for arguments:
(1) Positional arguments must come before keyword arguments, both in the function declaration and when calling the function.
(2) An argument can be passed by position or by keyword, but not both.
6) Variable-Length Argument Lists with the * Operator
allow a developer to define a function with an arbitrary number of arguments.
7) Variable-Length Argument Lists with the ** Operator
tells the interpreters to pack all key-value pairs passed to the function into a dictionary.
pass;
2) Default Arguments: By assigning the default argument in the function's declaration, you are ensuring that the prefix input parameter will always have a value, even if one is not supplied when the function is called
Example:
def process_all_textures(texture_node, prefix='my_'):
print('Processed%s%s'%(prefix, texture_node));
process_all_textures(texture); //result: Processed my_file1
3) Positional Argument(位置参数): not real argument, just declare there has a argument and pass data.
4) keyword Arguments (关键字参数): Prefix value for argument so that even you do not pass value to the keyword argument, it still has value.
5) Two issues for arguments:
(1) Positional arguments must come before keyword arguments, both in the function declaration and when calling the function.
(2) An argument can be passed by position or by keyword, but not both.
6) Variable-Length Argument Lists with the * Operator
allow a developer to define a function with an arbitrary number of arguments.
7) Variable-Length Argument Lists with the ** Operator
tells the interpreters to pack all key-value pairs passed to the function into a dictionary.
Sunday, February 3, 2013
Container Types
1) Sequences: nums = [1,2,3,3,5,6,7,7,7,8,9] (could exists same number)
2) Sets: numset = set(nums) = [1,2,3,5,6,7,8,9] (pruned duplicate elements, more efficient)
While you can generate a set from a list, a list cannot be an item in a set!
3) Dictionaries: numberNames = {1:'one', 2:'two', 3:'three', 'one':1, 'two':2, 'three':3};
(Mutable object, hashable. The keys do not all need to be the same type, the up dictionary mapped numbers to names, as well as names to numbers.)
Practice:
2) Sets: numset = set(nums) = [1,2,3,5,6,7,8,9] (pruned duplicate elements, more efficient)
While you can generate a set from a list, a list cannot be an item in a set!
3) Dictionaries: numberNames = {1:'one', 2:'two', 3:'three', 'one':1, 'two':2, 'three':3};
(Mutable object, hashable. The keys do not all need to be the same type, the up dictionary mapped numbers to names, as well as names to numbers.)
Practice:
import maya.cmds;
loc = maya.cmds.spaceLocator()[0];
print(maya.cmds.xform(loc, q=True, rotateOrder=True));
RESULT:xyz
Friday, February 1, 2013
Working with Numbers
1) Number Types: integers, long integers, floating-point numbers, and complex numbers
2) Basic Operators:
x/y: quotient of x and y, if x and y are integers, result is rounded down
x//y: floored quotient of x and y, use with floating-point numbers to return a decimal result identical to x/y if x and y were integers
x%y: remainder of x/y
divmod(x,y): typle that is (x//y, x%y)
pow(x,y) = x**y: x to the y power (x的y次方)
3) s.index(x): index of first x in s
s.count(x): total occurrences of x in s
2) Basic Operators:
x/y: quotient of x and y, if x and y are integers, result is rounded down
x//y: floored quotient of x and y, use with floating-point numbers to return a decimal result identical to x/y if x and y were integers
x%y: remainder of x/y
divmod(x,y): typle that is (x//y, x%y)
pow(x,y) = x**y: x to the y power (x的y次方)
3) s.index(x): index of first x in s
s.count(x): total occurrences of x in s
Attributes
1) getAttr and setAttr
import maya.cmds
loc = maya.cmds.spaceLocator()[0]; //create a new locator and store the name of its transform mode in a variable called loc
sx = maya.cmds.getAttr(loc+ '.scaleX'); //store the locator's x-scale in a variable named sx
print(sx); // print the result, which will be 1 by default
sx *= 2; //double the sx value
maya.cmds.setAttr(loc+'.scaleX', sx); //assign the new value to the node's attribute by passing a string with the node's name, a period, and the attribute name
2) Compound Attributes
print(maya.cmds.xform(loc, q=True, translation=True)); //print the locator's translation using the xform command
[0.0, 0.0, 0.0] //the result
maya.cmds.xform(loc, translation=[0,1,0]); //using the xform command to set a new translation value (using a list)
print(maya.cmds.getAttr(loc+'.translate')); //print the locator's translation using getAttr command
[(0.0, 1.0, 0.0)] // the command returns a list that contains a tuple
maya.cmds.setAttr(loc+'.translate', 1, 2, 3); //using setAttr to set a new translation value for the locator
Result:
3) connectAttr and disconnectAttr
The basic requirement for attributes to be connected is that they be of the same type.
import maya.cmds;
sphere = maya.cmds.polySphere()[0];
cube = maya.cmds.polyCube()[0];
//create a sphere and a cube and store the names of their transform nodes
maya.cmds.connectAttr(cube+'.ry', sphere+'.ty'); //connect the cube's y-rotation to the sphere's y-translation
maya.cmds.select(cube);
maya.cmds.disconnectAttr(cube+'.ry', sphere+'.ty');
# Result: Disconnect pCube2.rotate.rotateY from pSphere2translate.translateY. #
//execute the following lines to create a multiplyDivide node between the two attributes to scale the effect
mult = maya.cmds.createNode('multiplyDivide');
maya.cmds.connectAttr(cube+'.rt', mult+'.input1X');
maya.cmds.setAttr(mult+'.input2X', 1.0/90.0);
maya.cmds.connectAttr(mult+'.outputX', sphere+'.ty');
maya.cmds.select(cube);
Result: if you rotate the cube, the sphere translates 1 unit for every 90 degrees of rotation.
import maya.cmds
loc = maya.cmds.spaceLocator()[0]; //create a new locator and store the name of its transform mode in a variable called loc
sx = maya.cmds.getAttr(loc+ '.scaleX'); //store the locator's x-scale in a variable named sx
print(sx); // print the result, which will be 1 by default
sx *= 2; //double the sx value
maya.cmds.setAttr(loc+'.scaleX', sx); //assign the new value to the node's attribute by passing a string with the node's name, a period, and the attribute name
2) Compound Attributes
print(maya.cmds.xform(loc, q=True, translation=True)); //print the locator's translation using the xform command
[0.0, 0.0, 0.0] //the result
maya.cmds.xform(loc, translation=[0,1,0]); //using the xform command to set a new translation value (using a list)
print(maya.cmds.getAttr(loc+'.translate')); //print the locator's translation using getAttr command
[(0.0, 1.0, 0.0)] // the command returns a list that contains a tuple
maya.cmds.setAttr(loc+'.translate', 1, 2, 3); //using setAttr to set a new translation value for the locator
Result:
3) connectAttr and disconnectAttr
The basic requirement for attributes to be connected is that they be of the same type.
import maya.cmds;
sphere = maya.cmds.polySphere()[0];
cube = maya.cmds.polyCube()[0];
//create a sphere and a cube and store the names of their transform nodes
maya.cmds.connectAttr(cube+'.ry', sphere+'.ty'); //connect the cube's y-rotation to the sphere's y-translation
maya.cmds.select(cube);
maya.cmds.disconnectAttr(cube+'.ry', sphere+'.ty');
# Result: Disconnect pCube2.rotate.rotateY from pSphere2translate.translateY. #
//execute the following lines to create a multiplyDivide node between the two attributes to scale the effect
mult = maya.cmds.createNode('multiplyDivide');
maya.cmds.connectAttr(cube+'.rt', mult+'.input1X');
maya.cmds.setAttr(mult+'.input2X', 1.0/90.0);
maya.cmds.connectAttr(mult+'.outputX', sphere+'.ty');
maya.cmds.select(cube);
Result: if you rotate the cube, the sphere translates 1 unit for every 90 degrees of rotation.
Subscribe to:
Posts (Atom)







