So as one of my first posts I thought I would publish something on one of the reasons I like python over MEL. In C++ there something called function overloading. Which means you can create a function with the same name but different argument types and number of arguments. For example:
int foo( int varA )
{
return (varA * 2);
}
double foo( double varA, int varB )
{
return (varA * varB);
}
In the above C++ example you can pass foo an int which is valid or pass foo a double and an int which is valid.
So getting back to MEL and Python, MEL has strongly typed variables like C++ and no general way to overload functions. You could pass your procedure a string array and maybe have the first argument say what to do with that string array, but in the end your still passing and only can pass a string array to that variable.
In Python there is no way to create completely separate overload functions, but you can create a function that receive different types of variables and does completely separate actions based on what types it receives. For example:
import maya.cmds as cmds # notice that varA and varB are not declared so they can receive any type of variable def add( varA, varB ): # use the type function to get the variable types and use the vAType = type( varA ) vBType = type( varB ) # in these cases you would not even need this function adding two string's #, float's, int's, list's already works if vAType == type(str()) and vBType == type(str()): return (varA + varB) elif vAType == type(int()) and vBType == type(int()): return (varA + varB) elif vAType == type(float()) and vBType == type(float()): return (varA + varB) elif vAType == type(list()) and vBType == type(list()): return (varA + varB) ################################################ # here is where overloading a function can help ################################################ # if varA is an int and varB is not elif vAType == type(int()): # normaly if you add a int to a float with python it changes the int to a float # this makes it so if you add an int to a float it stays an int if vBType == type(float()): return (varA + int(varB)) # if varA is a string and varB is not elif vAType == type(str()): if vBType == type(int()): return (varA + str(varB)) elif vBType == type(float()): return (varA + str(varB)) # int the case of the list we need something more elif vBType == type(list()): result = varA # iterate through the list items for v in varB: # if a list item is a certain type add it to the varA string if type(v) == type(str()) or type(v) == type(float()) or type(v) == type(int()): result = ( result + str(v) ) return result # nothing matched so return None return
Try and execute this script in the Maya script editor then enter these commands:
add( 1, 5 ) add( 'test' , ['one', 2, 3.0] ) add( 'test' , 1.567 ) add( 1, 1.567 )
Your results will be:
# 6 # test1.567 # testone23.0 # 2
In the above example I add like types together and also add types that normaly would not be capable of being added together with just the + operator. Now you would not necesarly want to do this to make a giant add anything function, but you might want to create a function that can receive different data types and do similar operations with them based on the type they are.
-RyanT
February 7th, 2010 at 8:54 pm
Thanks a lot for ur tutor.I’m a Python beginner in Maya and learn by myself,i found it’s a bit complicate to study because there’re too many informations on line.Are there any tutors that teach step by step?Appreciate~
And,there’s one more question.(sorry about my blahblah ^_^)
I’m trying to get the abusolute world position of one componet(like vertex) of a poly object.I tried the MEL,it works.But failed to convert it into Python.
in MEL , described like this:
float $vPos = `xform -q -ws -t pCube1.vtx[0]`;
print $vPos;
Question is how should i convert the “-t” into Python?The quick help told me “-t/Translation length length length”
Apologize if disturb…
February 7th, 2010 at 9:57 pm
As for learning Python on your own, I recommend the book called Learning Python:
http://www.amazon.com/Learning-Python-Powerful-Object-Oriented-Programming/dp/0596158068/ref=sr_1_1?ie=UTF8&s=books&qid=1265604905&sr=8-1
As for querying a vertices position. To get the position of a vertex using Maya commands would be:
import maya.cmds as cmds
vPos = cmds.xform(”pCube1.vtx[0]“, q=True, ws=True, t=True)
When using MEL in query mode the -t flag represents translation and when using the -q flag it is being queried. In Python you can not create empty flags like in MEL. So for a flag that you need information from you must pass it a boolean which is either True or False. You only need to pass it the [length, length, length] list when setting the translation. To set the translation it would look like this:
cmds.xform(”pCube1.vtx[0]“, ws=True, t=[1.0, 5.0, 7.0])
Note that you must pass the name of the node you are working with first unlike MEL which is passed last.
Ryan
February 7th, 2010 at 11:32 pm
Thanks Ryan,Thanks for ur kindly help very much,God bless u~! ^_^