Skip to content

Instantly share code, notes, and snippets.

@chris-lesage
Last active April 5, 2020 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chris-lesage/b11efede3aa3d92a4b15c9d532417532 to your computer and use it in GitHub Desktop.
Save chris-lesage/b11efede3aa3d92a4b15c9d532417532 to your computer and use it in GitHub Desktop.
Simple Python Challenges in Maya

Intro Email:

[COMMENT: This document was originally written to introduce beginners and generalist co-workers who wanted to start using Python. It was meant to go much further than #9, but the project fizzled out.]

Write scripts that do the following, and we can talk about techniques, or ways to improve it. If they are too easy, you can also add options, or a GUI.

Please try to write nice clean code, and make comments. You will be re-using parts of your scripts in future challenges, so it is important for you to be able to read and re-use your code.

PYTHON CHALLENGES

1. Swap Rename

In Maya, take 2 nodes that you have selected, and swap their names. Example. "cube1" and "sphere2", become "sphere2" and "cube1".

2. Rename Nodes With a Pattern

In Maya, rename a list of nodes, so that they have 3 frame padding, and start at 1, but skip every 2. Let the user choose the name. So "node" becomes whatever they want. eg. node_001 node_003 node_005 node_007, etc.

3 Naming Animation Frames

Print a list of animation frame numbers, so that every 5 frames, there is a new shot. name_[SHOT]_[FRAME] name_001_001 name_001_002 name_001_003 name_001_004 name_001_005 name_002_001 name_002_002 name_002_003 name_002_004 name_002_005 name_003_001 name_003_002 ... and so on.

4. Evenly Spaced Numbers

Print out X float numbers between 0.0 and 20.0, evenly spaced. For example 5 segments would be [0.0, 5.0, 10.0, 15.0, 20.0]. But also let the script run any number of segments that the user chooses. For example, what if you did 45 evenly spaced numbers between 0 and 30? Or 3 numbers between 0 and 100?

5. Evenly Spaced Numbers version 2.0

Improve your last script by adding parameters so you can print out X numbers between any range of numbers. For example, 20 numbers between 5.0 and 30.0. Or 5 numbers between 100.0 and 200.0. What if you also support reverse ranges? For example, 20 numbers between 30.0 and 5.0? And also support negative numbers. 10.0 to -22.5.

6. Place locators between 2 locators

Now take #5 into Maya. Create 2 locators and place them somewhere in your scene. Write a script that places X number of locators between those 2 locators.

Later we will learn more about vectors, but for now, you can just take challenge #5 and do segments for X, Y, and Z to get the 3D position. If you already know how to work with vectors, feel free.

7. Find Constraints With Multiple Targets

Write a script that finds and returns all the nodes in the scene that has a constraint that has more than 1 target. So for example, if cube1 is parentConstrained by 2 locators, then it will return cube1. If sphere1 is constrained by 1 locator, it will not return it.

8. Draw a curve between 2 points.

The curve() command takes an argument -point (-p) that is a list of point positions. That defines where the CVs are.

Take challenge #6, where you created locators that were placed between 2 other locators.

Instead of placing locators, create a list of points. Next, feed that list into the curve() command and create a cubic (degree 3) curve with X number of CVs between 2 locators.

This is a simple challenge. But doing this will begin to give you the first steps in the ability to create auto-riggers that build IK ribbons, or spline IK controls.

9. Set a keyframe on all visible controllers

(Commentary: This exercise was pretty contrived and not very useful. But it is meant to make you think about what "visibility" can mean beyond the attribute value.)

Write a script that sets a keyframe - at the current frame - on all visible animation controllers in the scene. If a controller is not visible in the viewport, it should not get a key.

HINT! There are multiple reasons that a node would not be visible.

Some ideas to enhance this: If the animator has a channel or channels selected in the channel box, only set keys on those channels If the animator has some controls selected, only set keys on those controllers. If there is no selection, do all. (Ignore any that are invisible, even if they are selected.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment