Reader small image

You're reading from  Maya Programming with Python Cookbook

Product typeBook
Published inJul 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781785283987
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Adrian Herbez
Adrian Herbez
author image
Adrian Herbez

Adrian Herbez is an accomplished software engineer and educator with a background in virtual worlds and gaming. He has worked as a web developer at Linden Lab (the creators of Second Life) and a senior software engineer at Sony Computer Entertainment, America, where he developed games and interactive content for PlayStation Home. He also served as the lead gameplay engineer for KIXEYE's War Commander. Adrian has also taught at the Academy of Art University in San Francisco in a number of different departments over the years, covering Maya, web development, and game programming. He is currently the cofounder and president of Jamwix, a game and media start-up. So far, Jamwix has released CineMagic: Hollywood Madness, a mobile game for iOS and Android as well as the first feature-length movie for virtual reality (The Banshee Chapter: Oculus Rift Edition). Adrian holds a master's degree in Fine Arts from the University of California, Irvine, from the Arts, Computation, and Engineering department, and has served as a juror for IndieCade, the international festival of independent gaming, for a number of years.
Read more about Adrian Herbez

Right arrow

Chapter 10. Advanced Topics

In this chapter, we'll look at the following few advanced topics that can be used to take your scripts farther:

  • Wrapping Python functionality in MEL

  • Creating custom tools using contexts

  • Using script jobs to trigger custom functionality

  • Using script nodes to embed code in scenes

  • Combining script jobs and script nodes

Introduction


In this chapter, we'll look at a few advanced topics that can be used to give your scripts extra polish and make them easier to use for your teammates. We'll see how to make your scripts work like Maya's built-in tools using contexts, trigger custom functionality in response to events using script jobs, and embed code into a scene using script nodes.

Finally, we'll look at a tool that can be used to embed custom functionality in a scene and trigger it when a specific object is selected (very useful for invoking complex UIs for character rigs, for example).

Wrapping Python functionality in MEL


Although Python is definitely the preferred way to go about scripting for Maya, there are some features that still require you to use MEL. We'll be seeing several of those features in this chapter, but first we'll need to look at how to call Python code from MEL.

Getting ready

First off, we'll need a Python script to call. You can either use something you've already written or make something new. For the sake of this example, I'll use a new script that simply creates a NURBS sphere at the origin, as follows:

# listing of pythonFromMel.py
import maya.cmds as cmds

def makeSphere():
    cmds.sphere()

How to do it...

In this example, we'll create an MEL script that will in turn call our Python script. Create a new file and add the following code, being sure to save it with a .mel extension. In this case, we'll create a file named melToPython.mel:

global proc melToPython()
{
    python "import pythonFromMel";
    python "pythonFromMel.makeSphere()";
}

Note that...

Creating custom tools using contexts


Many of Maya's tools are used in an interactive manner, with the user specifying inputs as needed, and actions taking place either when the necessary number of inputs have been provided, or the user hits the Enter key.

So far, none of our scripts have worked this way—it has been necessary to have the user explicitly run the script, or press a button. That works fine for many things, but providing interactive input can add a lot of polish to a script. In this example, we'll be doing exactly that.

We'll create a script that, once invoked, prompts the user to select two or more objects. When they press the Enter key, we'll create a locator at the average position of all of the objects. To do that, we'll need to create a custom context to implement our very own tool.

Our custom tool in action. Left image is the tool while it's being used (notice the custom "AVG" icon on the left), and right image shows the result—a new locator at the average position of the...

Using script jobs to trigger custom functionality


Script jobs offer another alternative to explicitly calling scripts, or pressing buttons, to invoke your functionality. By using script jobs, it is possible to trigger custom functionality based on either a specific condition or a specific event.

In this example, we'll create a script job that will respond to the selection changed event by printing the name and type of the selected object to the console.

Getting ready

One of the things that makes script jobs so useful is the fact that they persist (as opposed to just running once). However, that can make developing scripts that use them a bit difficult, since if you change your code and re-run your script, you'll end up with multiple script jobs in your scene. For that reason, it's good to give yourself a way to easily clear out all existing script jobs. The following script will do just that:

import maya.cmds as cmds

def killAll():
    cmds.scriptJob(killAll=True, force=True)
    print('KILLED...

Using script nodes to embed code in scenes


All of the examples we've seen so far exist as scripts, separate from the actual scene that they are run in. That's fine for tools, but means that if you create a script that is tightly tied to a particular scene (such as a custom control UI for a character rig), you have to be careful to make sure that the script file is always distributed along with the Maya file.

For such situations, Maya offers a better way. Script nodes can be used to bake scripts directly into a scene, allowing them to be run without any external dependencies. Furthermore, script nodes can be created with code.

In this example, we'll create a script that will prompt the user for a Python file, and will then create a script node with the contents of the file, and set it up so that the script will be executed each time the file is opened.

Getting ready

To use the script we'll be creating, we'll need to have a script ready to embed. For the sake of the example, I'll be using a simple...

Combining script jobs and script nodes


One of the great things about script jobs, and script nodes, is that you can use script nodes to ensure that a given script job travels along with your scene. For example, you might want to use a script job to trigger a custom character rig UI any time the user selects a certain object in the scene.

In this example, we'll create a script that will make it really easy to set such things up. Our script will perform the following:

  • It will ask the user to point it at a Python file with one or more functions to create UI

  • It will present the user with a list of all functions defined in the file in a scroll list

  • It will allow the user to select an object in the scene and a named function from the file

  • It will embed the contents of the function into the scene as a script node, along with a script job that will run the function every time the specified object is selected

Getting ready

To use the script we'll be writing, you'll need to have a script with at least one...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Maya Programming with Python Cookbook
Published in: Jul 2016Publisher: PacktISBN-13: 9781785283987
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Adrian Herbez

Adrian Herbez is an accomplished software engineer and educator with a background in virtual worlds and gaming. He has worked as a web developer at Linden Lab (the creators of Second Life) and a senior software engineer at Sony Computer Entertainment, America, where he developed games and interactive content for PlayStation Home. He also served as the lead gameplay engineer for KIXEYE's War Commander. Adrian has also taught at the Academy of Art University in San Francisco in a number of different departments over the years, covering Maya, web development, and game programming. He is currently the cofounder and president of Jamwix, a game and media start-up. So far, Jamwix has released CineMagic: Hollywood Madness, a mobile game for iOS and Android as well as the first feature-length movie for virtual reality (The Banshee Chapter: Oculus Rift Edition). Adrian holds a master's degree in Fine Arts from the University of California, Irvine, from the Arts, Computation, and Engineering department, and has served as a juror for IndieCade, the international festival of independent gaming, for a number of years.
Read more about Adrian Herbez