Note: Because this is an ongoing series, viewers will not receive a certificate of completion.
Skill Level Intermediate
- [George] Hi, I'm George Maestri. Today we're going to look at how to create scripts to automate the rigging process in Maya. Now, if you've done rigging before, you realize that it can be very repetitive and so scripts can really help speed things along. So the script we're going to create will take an object's translation data and place it into the offset parent matrix. Now, if you're not familiar with that, you might want to watch last week's video where I covered that in depth. Now, we're going to create our scripts in the Script Editor. So I'm going into Windows, General Editors, Script Editor. And you'll see this has two windows. The top window basically is just a list of everything we do. And then the bottom window allows you to program a script. Now, we can do that either in MEL or Python. Now, I'm going to do this in MEL because basically, it's Maya's native language so all of these commands here are just MEL commands. If you want to, you can program in Python but I usually reserve Python for the more formal software development things for the bigger projects. For really quick buttons like this, MEL is perfect. So we're going to do it MEL but you can certainly do it in Python. So I'm actually going to close this and then I'm going to go through my process that I'm going to script once and then use those commands to create the script I'm actually going to use. So I'm going to start with this circle here, which is called nurbsCircle1. And let's go ahead and create our script. So I'm going to move everything from this transform node into the Transform Offset Parent Matrix node and we can do that in the Node Editor. So if I go into Windows, Node Editor, make sure my nurbsCircle is selected. If not, you can always add it in. And then I'm going to expand this. Now, in order to connect these two, all I have to do is select this matrix node, left click and drag, plug it in the top left here. Select Other, offsetParentMatrix. Now, this object will jump but that's because I have a double transform here and I'll get rid of that in just a second. But the second thing I want to do is disconnect that node. So I've connected the node, disconnected the node and that's moved the values into that offset parent matrix. And now all I have to do is select these here and zero them out. And now, I've zeroed out my channel box or my main transform nodes and placed all those values in the offset parent matrix. Now, if you're working with offset parent matrices, you're going to be doing this a lot. So it makes sense to create a button. So let's go ahead and create our script. So let's go back into Windows. Script Editor, and you'll see we've got all those commands that I just did on this object. Well, I can select these, copy them, and paste them into this window. So now I have all of these commands and all I have to do is surround it with a script. But the first thing I want to do is get rid of these ones here, which basically are the connected or basically the results nodes. We don't need those. So these are basically connect attribute, disconnect attribute, so that's the Node Editor and then basically, set attribute. That's where I zeroed everything out. So let's go ahead and take this and add some script to it. So the first thing that I need to do is get my selection set and this is a pretty standard practice. So what you want to do is create a string. So I'm going to type in the word string. Create a variable. In this case, I'll create a variable called selected, so $selected, and then I want to create an array. So I'm going to open and close a square brace and what that does is it creates an array of strings and that's named selected. So what's going to be inside that array? Well, everything that I've selected in the scene. And that I can get by going equals, backwards single quote ls -sl and that means list my selection. So everything in my selection will be listed and placed in this variable. And then I close my single quotes and I have all of my values now and now I just need to step through them. So I'm going to hit Tab. Type for and then I need to create a new value for each iteration here. So I'm just going to call it $item. $item in $selected. So I'm going to go through my array and just place them one at a time in item and then we'll do some operations on those. And then we're going to go ahead and put in open curly brace. Now, one of the things you want to do is that at the end of every command, you want a semicolon, which I did not do here. So I want to make sure I have a semicolon at the end of every command. And then for a for loop, you want to make sure you open and close your curly brace. So I'm going to scroll all the way down to the bottom and just close my curly brace right now before I forget. So now all I have to do is take these commands and just swap out the name of the object we used to create them with this variable, which is called item. So we can do that by doing an open paren. The name of the variable, $item. Plus this value. Now, what we want to do is we want to replace nurbsCircle1 with the name of the actual object that we have selected. So I'm going to go ahead and delete that. I want to put .matrix in quotes, and then close parentheses. So what this has done is it's basically just swapped out this word, nurbsCircle1, in that phrase. So now that we have this, we can copy and paste it to do other tasks. So this one here is we're basically connecting the object's matrix to its offset parent matrix. So all I have to do is copy this name and paste it and then again, close quotes in parentheses. Make sure we have a semicolon at the end. So now I've got my first value here and I'm going to hit Tab just to indent it so it looks nice. And then I'm just going to copy these and paste them here. So again, for disconnect, we're just going to disconnect the same attributes on the same object. Now, once we've disconnected them, we have to zero everything out. So I'm just going to do one and then I'm just going to actually do a little trick here. I'm actually going to copy and paste from another script I have. But basically, what we want to do is we want to set the attribute of that object's translateX to zero. So again, we can take this. Copy it. Paste it over that object in parenthese, so now I've created one for translateX. And instead of boring you with a lot of typing, I'm just going to paste in the rest of these. So basically we have translateX, Y and Z, rotateX, Y and Z. So now that we have all of this, our script should work. So I'm going to go ahead and copy this script just to make sure that we have it. Now, if you had a problem typing in the script, I do have a copy of this saved to the scripts folder of your exercise files. So if you want to, you can certainly refer to this or just us it instead of having to type a lot. So now that we have this, let's go ahead and execute this script. So I'm going to select this object. You can see that my values here in my channel box are still there. Execute once, they go away. And if we look in the Attribute Editor under Offset Parent Matrix, there are there. So that's really awesome. So let's go back into our Script Editor here and if your script has disappeared, you can repaste it. But now I have my script, I want to save it to my button. So I'm going to go ahead and select that script. Hit this icon here, which says Save Script to Shelf. So I'm just going to call this OPM for Offset Parent Matrix. And we're going to put it on the rigging shelf. So I have my rigging shelf highlighted and let's go ahead and just click OK. So now, I should have this command here. And if I highlight it, you see that yes, all of my code is in there. So let's go ahead and use this to work with this one here. So you can see that each one of these controls in this rig has stuff in the channel box but if we Shift + select each one of these, we can run that script and it should zero everything out. Let's go ahead and Shift + select all of these control objects and then hit this button once. And when I do, each one is zeroed out and the transforms are in the offset parent parent matrix. Now, if you like this script and want to use it again, you can certainly do a Save All Shelves and that will go ahead and save it out so it loads up the next time. Otherwise, it will disappear when you close Maya. So when you create buttons on shelves, make sure you save them. So hopefully this gives you some good insight into how to create buttons to speed your rigging tasks in Maya.