Saturday, 29 June 2013

Maya and Python - scripted gaming

One challenge I set myself while teaching Python 101 was to show just how easy it would be to create an interactive game, or other user-driven application, that runs in Maya.  The first question was just how do you run python in real time in Maya?  You can't use a loop in Python - this tends to tie up Maya, and its relatively easy to lock up the software this way.  Trust me, I did that - several times.  Its not fun to keep bringing up the task manager to kill Maya...




The solution was expressions.


Expressions in Maya are evaluated as the timeline is scrubbed (hence pressing play will be like running a program), and while expressions in Maya use MEL, we can actually execute python functions using the MEL command python(python statements); 

So - how did I test this?  Easy - I made a simple pacman ghost script that would draw a maze, and then patrol a ghost through this maze as long as the timeline was playing.




For your Maya pleasure, or just out of your own curiousity, the python file I created to do this can be downloaded below.  I'm providing this as a resource for learning from, so I hope you'll find it interesting...


I won't go through the code here on this blog, but rest assured I (hopefully) heavily commented this enough for you to work out.  To make use of this script, download it somewhere.
  1. Start Maya
  2. Open the script editor - load and execute the python script you just downloaded.
  3. Making sure that the command line at the bottom of the Maya GUI is set to Python, enter the function drawMaze() and press enter.
  4. A simple pacman-style maze should appear in the view port.
  5. Create a simple cylinder object - I'd suggest naming it as theGhost rather then leave it as pCylinder1.  Its cleaner that way.
  6. Open up Maya's expression editor (image below if you've never seen it before) and create a new expression.  Into the expression code area, enter python("moveMuncher(" + frame + ",'theGhost')");

If you're not familiar with where the expression editor is...

Press play on the timeline and watch the magic happen.  If its moving like crazy, make sure to open up the Animation preferences and set the frame rate (ie. Play real time) to control the speed of the script.


So, that's nice - but you can't make a game if you can't play!

True enough.  In this next downloadable script, I create an example script to show you how to redefine the cursor keys and use it to move an object around.  Obviously I haven't put together a complete game with these scripts, but the example should make it really easy to do so when I next get a little spare time.



Some like it hot...  keys that is...

How would this user-input system work?  Well, I scratched my head for a long time wondering if there was any way to get real time reading of user input in a script.  While talking to a handful of students who were curious as to what I was actually doing, I explained the project.  One student piped up with what was a good solution - Can I just redefine the hot keys in Maya?

After a little digging about, I managed to redefine (and also reset) the keys and have them interact with my scripts.  The only downside here is that there doesn't appear to be any key press repeat, so a tap of a key at a time seems to be the only way to operate this.

In the example script, I've done what I'd applied in the pacman ghost function and just set a motion vector so that when a key was pressed, the object would just keep moving.  So, to use this script, just do the following.
  1. Open the script editor - load and execute this python script.
  2. Create a simple object - perhaps just a sphere for now, and I'd rename it to thePlayer just to keep things clean.
  3. Into the command line, type initGameKeys() and press enter to execute it.  You'll be able to see if it worked by holding down the space bar.  If the Marking menu doesn't come up, its all go!
  4. Open up the expression editor as I explained previously, create a new expression and enter the code python("movePlayerObject(" + frame + ",'thePlayer')");
  5. Press play on the timeline, and then press the cursor keys.  The object should now move!
  6. Press space will reset the object to the middle of the scene and stop it moving.


Ok, so now how do I fix my space bar so I can go back and work in Maya normally?

Once you're done playing, in the command line, type the command resetGameKeys() and press enter.  This will restore all of Maya's standard hot keys.  By that, I meanthat if you've got your own custom hot keys defined, this will reset them to whatever Maya had originally.

The code can be tweaked to prevent this, but that means you'll have to add a few simple pieces of code to capture the named commands first, and then re-assign the named commands back to the keys.  Resetting the factory presets was the quick way to reset things while I was testing.

So there you have it - a couple of scripts that I hope you'll find something valuable in, and that I encourage you to expand and improve on (or just use the concepts to develop your own games or tools).  It is a lot easier to create interactive scripts that run like software in Maya using just expressions and a little code then it looks.

I hope these come in useful or provide some kind of educational value to your own skills.  Enjoy!

0 comments:

Post a Comment