Introduction to Python Scripting in Maya – Part 2: Working with Selections and Constraints

In this part, we’ll cover how to access the user’s selection in a Python script, and how to constrain each selected object’s aim towards the first object selected. To start, select “Window>Settings/Preferences>Preferences”. Click on the “Selection” category, and ensure that “Track selection order” is enabled. Enabling this setting will allow our script to be aware of the user’s selection order. In the Script Editor, open the randomCubes script we had created in part 1. Let’s modify this script to make instances of the first object selected. To easily identify it, change its commented name to Delete the clean-up code, and remove the call to polyCube. Use the “ls” command with the “orderedSelection” flag set to True to return the objects in the same order they were selected by the user. The order is important here, since we want to create instances of only the first item selected. To print the whole list, uncomment the following line. As a side note, we can streamline this print statement by replacing the str() conversion function with percent s “%s”. For this to work, we’ll also need to replace the plus (+) concatenation symbol with the percent symbol. This way, the string will be formatted such that the result’s value will replace the instance of percent s. Keep in mind that we’ll be using this string substitution technique several times throughout this series. To test our newly modified script, open the outliner, and clear the scene by deleting the original cube and its instances. Create a cone, a pipe, and a sphere. Select the pipe first, then hold SHIFT to select the sphere and the cone. When we run the script, note that only the pipe is instanced, since it was the first object selected. Note also that the selection order is correctly printed to the Script Editor’s output. We’ll be using this script again in a bit, so you can save it to your shelf as randomInstances. You can also save it to your file system for later reference as Undo the pipe instancing by pressing CTRL+Z. Now that we know how to work with an ordered selection, we can look at how to create aim constraints. To get an idea of how to manually create an aim constraint between two objects, first click on the sphere, then shift-click on the cone. In the Animation Menu Bar, select “Constrain>Aim”. The cone will now aim towards the sphere, regardless of either object’s position. The resulting MEL command is “aimConstraint”, along with several flags to specify the orientation and aim vector of the constrained object. Undo this constraint for now. If we select the sphere, the cone, and the pipe, and apply a new aim constraint, the pipe’s aim is actually constrained towards the average position of the sphere and the cone. However, we would like all the objects selected to point towards the first object in the selection list. Let’s see how we can automate this process. Add a new Python tab to your script editor by pressing CTRL+T, or by selecting “Command>New Tab”. Identify this script as Import the maya.cmds module, and use the “ls” command to obtain the current ordered selection. Start by checking whether or not the selectionList contains at least two items. If this requirement isn’t met, print the following message. Create the targetName variable to keep track of the first object in the list. Now that we have our target’s name, we can remove it from the selection list. Use the following for-loop to iterate over the remaining items in the list. We can add a print statement to check that we are indeed iterating over the correct objects. Add the call to cmds.aimConstraint() with the following flags to create the aim constraint between the current object and its target. You can change the aimVector to make the object point along different axes towards its target. Save this script to your shelf as “aimAtFirst”. To test our scripts so far, delete the cone and the pipe. Create a cylinder, and reduce its subdivision axis to 6 to turn it into a hexagon. Flatten it by changing its height and its radius. Zoom in on the small hexagon by pressing the F key. You can also bevel its edges by selecting the Polygons Menu Bar, and clicking on Edit Mesh>Bevel. While the hexagon is selected, click on the “randomInstances” shelf script. Open the outliner, and select the sphere. Then, holding control, add the first cylinder instance to your selection, and shift-click on the bottom instance to add all of them to your selection. Now click on the aimAtFirst shelf script. All the hexagons should now aim towards the sphere to create an interesting shielding effect. In the next part, we’ll look at how to set keyframes in Python to smoothly rotate the group of instances.

9 thoughts on “Introduction to Python Scripting in Maya – Part 2: Working with Selections and Constraints

  1. I found that the aimToFirst script seems to create an aim constraint to the first hexagon instead of the sphere.  I control clicked after selecting the sphere and shift selected the other 49 hexagons.  Then I applied the script and got the problem I mentioned before.  I must be doing something incorrectly but I'm not sure what it is.

  2. When entering the code for result = orderedSelection=True ) I come up with a result saying # Error: TypeError: Invalid flag 'orderedSelection' #.
    After researching on why it doesn't work I couldn't find anything. The only conclusion that i could come up with is that I am using Maya 2011.

  3. Dear SDK learning Channel folks. Here's a humble question. if we are using Maya 2009 are we just out of luck? I really want to learn this but am extremely frustrated. I realize that Python is powerful. but how can I achieve this task in Python with out the almighty " Track Selection Order and orderedSelection" function which is not present in 2009? Any help would be nice. Peace.

  4. me likey.  Very well conceived and explained tutorials.   It is very helpful and appreciated that nothing is assumed and everything is sufficiently explained for complete understanding.

  5. hi. i'm selecting this controller select -r ANNE_RIG_FINAL:leftArm1_ArmIKControl ;

    what is the corresponding script for selecting this in maya by using python?

Leave a Reply

Your email address will not be published. Required fields are marked *