Home Lesson 1 Lesson 2 Lesson 4 Lesson 5 Lesson H

Lesson 3 - Functions and Maya Specific Commands

In this lesson we'll go over the syntax to create functions in python and some Maya-specific commands and workflows.

-----------------------------------------------------------------------------------------------------

Functions

Functions change values of variables or perform some action:

Functions in python have the following syntax:

def functionName(arguement1, arguement2):
    varToPrint = 'inside the function'
    print 'printing %s'%varToPrint 

You may have noticed the %s characters in the above example. This is converting a variable that will be named after the closing quote, to a string. You can convert multiple variables to a string in this way by executing an equivalent of: 'this string will contain a variable called %s and a variable called %s'%(variable1, variable2)

This is called the functions declaration. The above syntax goes:  

-keyword def

-name of the function: functionName

-open paranthesis

-Every arguement the function will take (this could be zero or as many as you like)

-close paranthesis

-colon

Everything at the next indentation level will be executed.

We can call this function now with the following code:

functionName(2, 3)

In this example the 2 input arguments don't do anything, but if we declare the function with 2 arguements, we have to call it with 2 arguements or python will throw an error.

If we were to declare another variable varToPrint 'outside the function' (at an indentation equal to the def keyword) python will use the instance of this variable at the approriate scope. For example the following code:

varToPrint = 'outside the function'
print 'printing %s'%varToPrint 
functionName(2, 3)

We would get the output:

printing outside the function

printing inside the function

This is because the print statement within the function will convert the more 'local' version of varToPrint

-----------------------------------------------------------------------------------------------------

So, with the ability to declare and call functions, let's go over a way to excecute commands in maya from a function we'll write in an external .py file.

Start by creating a .py file with a single print statement and save the file to your equivalent of:

C:\Users\your.name\Documents\maya\20##-x64\scripts

This local maya scripts folder is a path that Maya recognises to search for scripts and so anything in this folder can be imported easily to Maya.

import testMaya

You should see whatever you set your print statement to output in the script editor.

To re-import this file (after updating it for example) you cannot re-run import testMaya, you must run the command:

reload(testMaya)

-----------------------------------------------------------------------------------------------------

Let's make a more interesting file to reference in Maya and also introduce some new functions:

All maya python commands can be found at:

http://download.autodesk.com/global/docs/maya2012/en_us/CommandsPython/index.html

The following code makes use of commenting with the '#' character. Those lines have not been highlighted as they are not essential but they can be included:

import maya.cmds as mc
import random
def createSpheres(numSpheres, seedVal=0):
    #seed the random function with a specified number to create consistent results:
    if seedVal:
        random.seed(seedVal)
    print 'creating %s spheres'%numSpheres
    for i in range(numSpheres):
        makeNurbSphere = mc.sphere(name='new_sphere%s'%i)[1]
        #mc.sphere would normally return ['sphereNode', 'makeNurbSphereNode']
        #we want the 1 index of this array
        mc.move(0, i*2, 0, a=True)
        #note the 'a' keyword, this is setting the position to absolute
        randNum = random.random() + 0.5
        #random.random() returns a float between 0 - 1
        mc.setAttr('%s.radius'%makeNurbSphere, randNum)
        print 'created new sphere number %s'%i

Let's go over what this code does:

-import maya commands under the name 'mc'. Henceforth any call to mc.* will reference a function under the maya.cmds namespace

-create a function named 'createSpheres' with 2 arguements: the first is called numSpheres and the second is called 'seedVal and has a default value of 0

-if seedVal (i.e if it is not 0) then seed the random function with whatever value it was set as

-print 'creating x spheres' where x is the numSpheres value you passed in to the function

-for every element in the array [0,...numSpheres-1], with i set as that number (first time 0, last time numSpheres-1)

-create a maya sphere and set one of the resulting nodes to a variable named makeNurbSphere 

The node we want is the 'makeNurbSphere' node which is the 2nd element in the array returned by mc.sphere()

-move the selected object (the newly created sphere) to [0, i*2, 0] where i is the index as defined in the for loop

-create a variable named randNum and set it to a random float between 0 and 1

-use the maya command mc.setAttr to set the radius attribute on the makeNurbSphere node to that random float

-print 'created new sphere number x' where x is the i variable

This would be referenced in maya using testMaya.createSpheres()

-----------------------------------------------------------------------------------------------------

LESSON 3 TASKS:

  • import the 'time' library and create a function that prints time.time() (this is the number of seconds since some arbitrary date in 1970). You should know how to reference this function in maya

  • adapt your function to create a text object in maya that displays this value. You should use the maya command textCurves() and the t arguement

  • You can move and rotate selected objects using the move and rotate commands

  • adapt your function to run the above createSpheres function and display the time taken to run the function as the textCurves object. Something like 'that took %s seconds'

-----------------------------------------------------------------------------------------------------

lesson3_tasks.py