Sunday, October 10, 2010

My first Python script!

I'm really quite proud of myself here... ok it's only 6 lines of code, but I only started learning Python yesterday. After playing around in Maya, Houdini and RealFlow I figured I should start learning Python, since all of these programs have it embedded.
After hours of reading and playing (mainly with profanity written in script form), I thought I could start with something productive. The shatter effect in Maya is really annoying me for ages now, especially the things you have to do before you can run it. Freeze transformation, delete History by type, making sure it's not assigned to a weird shader, that doesn't sound a lot, but when you're in testing mode and Maya crashes all the time, it can become a real drag. So my idea was to just take whatever object and just click a little shelf button, that would prepare the object for the shatter effect. And here it is:


import maya.cmds as cmds


if len(cmds.ls(selection=True))<1:
cmds.confirmDialog(message='Please select an object', button='OK')


cmds.makeIdentity(apply=True)
cmds.DeleteHistory()
cmds.sets(e=True, forceElement='initialShadingGroup')


The first line loads the standard Maya commands into Python.
The second line checks whether there is an object selected, and if there isn't...
the third line brings up a dialog box that says "Please select an object".
The fourth line freezes the transformation, the fifth deletes the history and the sixth assigns the intialShaderGroup to the object.
Once the script is done and tested I copied it to the shelf, next to the Create Shatter button.


I know that the entire script is written with MEL commands, and it would've been just as easy (probably even easier) to just write it MEL, but the reason I want to learn Python is that I can use it in other software packages. I believe that if I learn Python in Maya, I automatically become better in MEL, but that wouldn't be the other way around.


That is quite an entry for a 6 line script, but it's 6:10 am and I'm full of caffeine, with literally nothing else to do.
Although I could write another script... make it 7 lines this time...

No comments:

Post a Comment