View Full Version : Python scripting thought process... from concept

12 December 2010, 05:21 PM
I was hoping someone more educated in scripting could give me some pointers. I'm having a difficult time wrapping my head around scripting.

I think it would help if I knew the thought process that goes into creating one... If someone could tell me the thought process they would follow in creating a script I want to make and give me some pointers on how I could create it, it would really help me out in learning scripting. I'd like to learn Python... I have written simple Maya scripts (copied functions from the script editor) and translated them to python but this would still really help.

The script i'm trying to write is a tool for helping test the skin weights on a selected group of joints by bending them at different times in the timeline. This is how i'd like it to work:

-User selects the joints he'd like to run the script on
-User sets a flag denoting which axis he'd like to rotate each selected joint along and the range he'd like to rotate say from "rx=0 - -90"
-User executes the command
-Each selected joint is allotted 10 seconds of time on the time line for testing. Starting with the first which is keyed at rx=0 at frame 0 on the time line then keyed at rx=-90 at frame 10. Then keyed at rx=0 at frame 11. The next selected joint is keyed at rx=0 on frame 10 and then rx-90 at frame 20 etc etc...

And that's what i'd like it to do. Does anyone have a few minutes to spare explaining what the thought process would be going in to writing something like this?

Thanks a lot!

12 December 2010, 11:15 AM
Just my $0.02

Scripting is of course a big subject, but first and foremost it is a matter of breaking down the "big picture" into smaller sub-problems. Then when the sub-problems are simple enough, you start combining the results from them and in the end you have a construct that solves the pig picture.

Anyway, I'm not a python man myself, I so I speak from the MEL perspective, however the methods are the same.

First your homework; read the python manual - familiarize yourself with variables and loop constructs/controls structures. Then do a quick read through of the Maya python command documentation. A big one, but necessary. However you don't need in this step to know how all commands work. It is more important that you know OF them, then when you stump upon a problem you can read up on the particulars of the command you think might solve the problem.

Then scripts are like onions (bad one - I know :) ) - they have layers.

Start at the very core of your problem, test if you can create a script which sets a keyframe on the joint given some input parameters - have all parameters specified in the function prototypes (i.e. no `ls -sl`in this function). Do this in a "clean" scene - i.e. a scene with just a joint or two - nothing that you can break if things go wrong.

If you can do this, you then have the core layer of you script; a function that sets a keyframe to a specific angle.

Then add the next layer; write a function that calls this "core function" with different angles in some kind of "for" or "while" loop, you will then most likely have very much the same parameters to this function as you had to your core function, however instead of a single angle, you supply it with a range.

Then wrap this function in an outer layer that decodes user input (i.e. put your `ls -sl`statements here). Give some thoughts on how to supply values; your parameter"rx=0 - 90" might be hard to decode, perhaps the combo "-attribute='rx' -min=0 -max=90" would be easier to process.

Anyway. Finally; some of these functions might be tiny and then you might as well combine it with the layer "above". For instance the "core function" might only be a single command, and then it is better to integrate it into the loop.

As I said, it is a big subject and there are many approaches, but in the end it is all about breaking down the problem and solving the pieces. Knowledge of the API (i.e. the commands) will guide you in the problem break-down. If you don't know the commands, you won't know how the problems can be solved.

/ Daniel

CGTalk Moderation
12 December 2010, 11:15 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.