Oct 25

I wanted to give another example that is even closer to how C++ overloading functions work. With python you can create functions that use keywords which are basically predefined variables. The good thing about this is a keyword does not have to have a variable passed to it because it already has a valid value. You can use this in a way to make a function work much like a maya command. For example these three lines are valid:

import maya.cmds as cmds

cmds.polySphere ( r=1 )
cmds.polySphere ( sx=10, sy=15, r=1 )
cmds.polySphere ( sy=15, r=1)

You could write your own maya command with Python API, which would be more complicated but completely possible. The good part of using the Python API to write a command is it includes other nice features that Maya does for you, but I wont get into that now. For now I will give an example how a basic function can imitate this. Example:

Continue reading »

Oct 22

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:

Continue reading »