Hey Everyone!
This week I’ve got a great episode that goes into how you can create floating dialog windows for your MaxScripts. I’ll even go into how you can create a “Random Selection” utility in MaxScript. Hope you enjoy it!
Transcription
Monday Movie #66: MaxScript Rollouts
Hello, everyone. Welcome to another Monday Movie. I'm Mr. Bluesummers.
So last week, I gave you a quick introduction to 3d Studio Max's MaxScript. We took a look at how you can execute single lines of code within the interface in order to invoke functionality on your objects. Now that movie was very well recieved, so this week we'll be taking an even deeper dive into MaxScript where I'll show you how to write a random selection utility. We'll write in two different flavors, which makes it more complicated so I'll also show you how to create a rollout to house your functionality and to help you write longer MaxScripts. Let's get started. I'm going to go up to "MaxScript" and click on "New Script". That's going to create a new script for me. We're going to execute the functionality in a basic way for what we're looking for.for object in objects do if random(0 1) == 1 do selectmore objSo similar to before, what this is doing is "for each object in the scene", flip a coin. So that it's random between 0 and 1, and if it's 1- if it's "heads", I want to add to the selection the object that came up during that coin flip. And when I evaluate this code (Ctrl+E), it'll actually execute the MaxScript and I get this object selection- this randomization I'm looking for. But what if I wanted to make it more complicated? What if I said something like "I only want it for a certain selection- for only these objects, or for only these objects?" Well that makes it slightly more complicated and if I could put this into a rollout, into a floating window, then I would be able to very quickly make that change on the fly. So let's do that. I'm going to create a rollout.rollout MyRandomSelectionTool "Random Selection"I'm going to put the functionality between the parentheses. Now what these do is, this line here says "I'm creating a new rollout", and I'm going to call it "MyRandomSelectionTool"- that's what I'm going to call it in the code. The name I want to appear at the top of the 3dsMax rollout is "Random Selection", and we can test this by cutting this out (Ctrl+X), I'm going to keep it on my clipboard, and below the rollout I'm going to write:createdialog MyRandomSelectionToolNow, when I execute this code (Ctrl+E) I get my random selection dialog. And this will house the functionality for this MaxScript. So let's create these user interface elements that are going to control this script and actually allow it to execute. Then we'll paste our function back in. Now I want two things. I want a checkbox to control whether it's being based on all of the objects or whether it's based on a selection, and then I want a button in order to actually make that happen:checkbox UseCurrentSelection "Use Current Selection" button GetRandom "Get Random"Now, if I hit Ctrl+E again, I'll see that my user interface objects are here. Now I can't use them because they don't do anything. We have to wire them to functionality but that's also very easy to do. Now the user interface elements are event-driven, so we need to say "when X happens, Y needs to happen." So we'll say:on GetRandom pressed doNow, when we press the GetRandom button something will happen. So what do we want to happen? We'll say:if UseCurrentSelection.checked then ( ) else ( )So if we're using the current selection, then do [the upper] code. Else do [the lower] code. We'll paste our old functionality back here, and we'll want to clear the selection so that we're not stacking up random selections all the time. Otherwise we'll end up with everything selected. Now if we're using the current selection, what we need to do is not work from zero objects and select objects as we go, but we need to work with the current selection, hold on to the objects that we want to select until the very end, and then swap that out with the current selection. So we'll create a new array:ObjArray = #()That's what an array looks like. Then we'll say:for obj in selection do if (random 0 1) == 1 doAnd now, instead of "selectmore" object, we need to append this object to the array. So we'll say:append ObjArray objAnd at the very end, we'll clear the selection because, remember, we have something currently selected. Then we'll select the object array that we created at the beginning. Now when I run this (Ctrl+E) I can click "Get Random" and I can click it as many times as I want in order to get random objects from the scene. Or I can select a certain number of objects, click "Use Current Selection" and then click "Get Random. Now it will only select random objects from that set. So this is how you create and deploy a dialog window in 3d Studio Max through MaxScript. It's a very powerful system that helps you add some sense to larger scripts, and extend your workflow even further than executing simple lines of code.rollout MyRandomSelectionTool "Random Selection" ( checkbox UseCurrentSelection "Use Current Selection" button GetRandom "Get Random" on GetRandom pressed do ( if UseCurrentSelection.checked then ( ObjArray = #() for obj in selection do if (random 0 1) == 1 do append ObjArray obj clearselection() select ObjArray ) else ( clearselection() for obj in objects do if (random 0 1) == 1 do selectmore obj ) ) ) createdialog MyRandomSelectionTool
Mr. Bluesummers: www.MrBluesummers.com.
Thanks for tuning in to another Monday Movie. You can find all of my Monday Movies as well as tutorials, resources, and downloads, on my web site: www.MrBluesummers.com.

Comments
Leave a comment Trackback