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:
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: 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
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. 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: 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:
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:
lesson3_tasks.py |