Get tips, tricks, and techniques to solve the most common issues Maya artists encounter in a live production environment in this new weekly series.
- [Narrator] Hi, I'm George Maestri and today we're going to take a look at scripting and specifically we're going to write a script that automatically flips the opacity toggle in Arnold. Now, you're probably familiar with this issue is that when you have an object that's rendered in Arnold you have to go into the attribute editor, find the Arnold tab, and hunt down this opaque toggle and you need to toggle that off in order to have transparency work.
So let me show you what I mean. I'm going to go into IPR and do an IPR render. Now, these objects they look like they have a solid surface on it, but if we actually select this object, and go over to the material and you scroll down, you'll see that we actually under refraction have opacity on this object. But it's not showing up, and the reason it's not showing up is because this opaque toggle is turned on.
So if I were to select the object and turn off opaque, you could see how it immediately shows up in IPR as a transparent object. Now this is great if you only have one object, but in this case, we have a number of spheres in this bowl, and if you have a larger scene you may have hundreds of objects that you need to flip this on. So, I'm going to show you how to write a script that will automatically do this, and we're going to create a button in the interface that will automatically do it for anything that we have selected.
So I'm going to go ahead and flip this back on so that way we can work with it in the script. Now what we want to do is create a script. So what we have to do is step through all of the objects that we have selected, and then flip that toggle. So the first thing we need to do is open up the script editor. So I'm going to go into general editors, script editor, and here we go, and if toggle this, notice how this shows up here. So I've already toggled it a few times, but let's toggle it once more, and as you can see when we toggle it off, we have this command here, set attribute, the name of the sphere.AIopaque and then the value, which is zero, and when I toggle it back on, it goes to one.
So this is really the core of what we need to use for this script, but we still have to surround that script with a bunch of other stuff, and we need to be able to know which objects are selected, step through those, and then apply this set attribute command. So the first thing we need to do is understand how mel can give us the information we need when we select multiple objects. So for example if I were to shift select all of these spheres one of the things we could do is we can type the command LS which is a list command, and what that does is that lists everything in the scene, and you can see here under result we get a whole bunch of stuff, including all the lights in the scene, the render layer manager, all of the materials, and that's not really what we want.
But what we could do is we could an LS space dash SL. SL stands for select. So what this means is that it will only show me the selected objects. So this result here basically just gives me this same list here. So that's the first thing we need to do. Now I'm going to build this script in NotePad, which is just a simple text editor. So the first thing I want to do is I want to take the result of this LS and put it into an attribute.
So I'm going to create a string, and let's just call that $ SEL for select. Now I want this to be an array, so I'm going to do square brackets open and close, which tells me that it's going to put it in as an array. So each one of these spaces will create a new entry into that array, and then we can set that to equal to the output which is backwards single quote, and then LS space minus SL, and then close quote, semi colon.
So what that string will contain is basically this result. Now the next thing we need to do is we need to understand how many objects are in this. So in this case, we have, well, one to six, we have six, but that may change. So we need to create another variable. So I'm going to create an integer. Let's just call this NUM for the number of objects, and that is going to equal a function called size of, and then SEL, $ SEL, and semi colon, and what that will do is that will actually spit out the number of objects in this array.
So, if I have five objects selected, this will become five. If I have 10 objects, or 100, that will be put into this. If we want to we can just do something very simple, such as print these out. So we could do say print $ SEL and we could also do print $ NUM for number and just to see how this works. So I'm going to go ahead and copy this, paste this into my script editor, and let's go ahead and hit execute.
And what this does is it basically gives me the names of everything in SEL, but notice I have a little bit of a typo here so that's actually supposed to print, not pring, and let's go ahead and do that. So what it does is it gives me one entry per part of the array, and then it tells me the number six. So I'm on the right path. When you create scripts it's always a good idea to print out your variables if you don't really know if it's going to work or not. So go ahead and print things out, just to know what is in each one of these variables, okay? So now we know that these variables work.
So now all we have to do is step through each one of them and create the command. So I'm going to create a for loop. So I'm going to type for open paren and then we're just going to use $ I as our numerator here, and that's going to start at zero, semi colon, and then we're going to do $ I just as long as that's less than NUM semi colon, and then we want to do an increment. So that's going to be plus plus $ I and what that does is it adds one to I every time through this for loop, and then I'm going to go ahead and open up my curly brace, and then what we need to do is create the command.
Or basically, we need to build this set attribute. So if we go up here we need to build this. So we need to take the name of the object, and add .AI opaque to it. So let's go back here. I'm going to create a new string. Okay, so I'm creating a new variable here, and let's just call this OBJ for object, and ATTR for attribute. So the object attribute is equal to this SEL.
So the selection of $ I. So that steps through one at a time, and I will increment. So let's make sure that's $ I plus and then we can put this in quotes .AI opaque, end quotes, semi colon. We're creating a new variable called object attribute, and that's going to be the name of the object, plus we're going to append this AI opaque to it.
Now once we have that we can do our set attribute. So we can do set ATTR and then just this object here. This $ object attribute, and then the value. In this case, we want to turn it off, so we want to set it to zero, and then end brackets. So hopefully this will work. So I'm going to go ahead and copy this, and let's go ahead and paste it into our script editor.
Make sure it's okay, and let's go ahead and make sure we have everything selected, and let's press play. And hopefully that worked. I'm going to minimize this, and let's take a look at this. So right now if I look at this, every single one of these that I selected is now no longer opaque. But my bowl and my ground are, so this actually did work. So now that we have this, and we know it works, we can now save it to shelf.
So I'm going to go and select my Arnold shelf here, 'cause this is probably the logical place to put it, and then we're just going to save this script to a shelf, and we're just going to call it opacity toggle, and hit OK. So now, I can hit this button any time and I can select multiple objects here. So let's go ahead and select the bowl and the ground and notice how the have their opaque on here, and all I have to do now is just click that and they all go off.
So now we have a button that will turn everything off to opaque and typically you kind of want that to be your default, and now once we have that, we can do an IPR, and now you can see that all the things that have opacity are actually showing opacity. So hopefully this little script showed you a little bit about how to step through selection lists in Maya, as well as how to work with opacity in Arnold.