Dec 05
We were talking at my work the other day how one could create a MEL command with many arguments without using tokens. So a previous example I gave of MEL in how it compares to Python needs to be updated. While creating this version I have to say it is much more error prone than creating a simple Python object like I show here:
Maya Python vs MEL data storage
It is simply much easier to manage and write more complex data in Python. Regardless here is a MEL test I made to show a more complex and robust version. This is for those who would rather use MEL or are simply curious to see what I am talking about.
Continue reading »
Nov 19
These are some examples of how storing data is much cleaner in Python. If you have not used an object oriented language before some of this might sound overly complex and scare you back to MEL. Just remember if you use Python in Maya you dont have to use all of its complexities but its nice to have them if you end up wanting to try other methods of coding.
So on to the good stuff. First I have a simplistic approach to using a python class to help store data from a user selected hierarchy. Second I have a more complex approach to doing the same thing.
To start you should open Maya and create some joints or nodes to create a hierarchy. These use the code examples to see what kind of data you can store and retrieve.
# Python Example V1
import maya.cmds as cmds
# Node list that will store the selected hiearchy
nodeList = []
class Node:
# Initialize Node
def __init__(self, node = None, parent=None, childs=None):
# Create three public attributes inside Node
# Note that it is easy to add new properties later
self.node = node
self.parent = parent
self.childs = childs
# Function to recurse the selected nodes hiearchy
def recurseNodes( node ):
print ('recursing node: ' + node )
# Get the parent node and child nodes
parent = cmds.listRelatives( node, parent = True )
childs = cmds.listRelatives( node, children = True )
# Maya returns an array - if listRelatives returns the parent remove it from the array
if parent != None:
parent = parent[0]
# Create an instance of the "Node" object
newNode = Node( node, parent, childs )
# Add new node to nodeList
nodeList.append( newNode )
# Recurse children
if( childs != None ):
for child in childs:
recurseNodes( child )
# Get selected node
sel = cmds.ls( sl=True )
# Iterate through selected nodes
for s in sel:
recurseNodes( s )
# Print saved data in nodeList
# Note it is very simple to get the data you want
# and you are free to make the data much more complex
# for instance you could create a "Node" object for the parent
# and each child and store those in the node instance
# this way you could walk the hiearchy and get data quickly and easily
for node in nodeList:
print '\n'
print 'node name:'
print node.node
print 'parents name'
print node.parent
print 'childrens name(s)'
print node.childs
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 »
Recent Comments