27
MAXSCRIPT 1. Understand MAXScript. 2. Create and run scripts. 3. Assign a script to a toolbar button. APP-1 Appendix C5409_Appendix_REV 10/17/05 10:33 AM Page 1

C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

MAXSCRIPT1. Understand MAXScript.

2. Create and run scripts.

3. Assign a script to a toolbar button.

APP-1

Appendix

C5409_Appendix_REV 10/17/05 10:33 AM Page 1

Page 2: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

MAXSCRIPTAppendix

MAXScript is 3ds Max’s internal scriptinglanguage. MAXScript enables you to extendthe functionality of 3ds Max by runningscripts that can perform a wide variety offunctions. As you work with 3ds Max, youmay eventually find yourself saying, “Itwould be nice if I could have a tool that doesthis task...” or “If 3ds Max could do this...”With MAXScript you can program 3ds Maxto function the way you like.

MAXScript resembles a computer program-ming language in that you write scripts usingspecialized language and logic in order to exe-cute a series of commands. One of the thingsthat sets a scripting language apart from a full-blown computer programming languageis that it is designed to function specifically for

one application. MAXScript is only for access-ing features within 3ds Max and related prod-ucts like Autodesk VIZ, gmax, and plasma.Other computer graphics applications alsohave their own scripting languages.

One of the good things about MAXScript isthat it is not an overly complex language. Ithas very simple syntax and formatting rulesthat make it easy for the casual user to use.This appendix discusses some scripting con-cepts that are necessary to understand inorder to complete the objectives at the endof the lessons. This is by no means meant tobe a thorough introduction to computerprogramming. Rather, it is just an introduc-tion to some of the concepts involved inMAXScripting.

APP-2

C5409_Appendix_REV 10/17/05 10:33 AM Page 2

Page 3: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

APP-3

Tools You’ll Use

C5409_Appendix_REV 10/17/05 10:33 AM Page 3

Page 4: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

L E S S O N 1

What You’ll Do What is MAXScript?MAXScript is a scripting language that isused to perform actions within 3ds Max. Youuse the language to write a script, which isa series of commands written line by line ina format 3ds Max can read. Some scripts canbe as simple as a single line of code, whileothers can be complex and consist of manypages of code. When you run the script, 3dsMax executes the actions described in eachline of code contained in the script.

You can use MAXScript to control many ofthe same features and operations that youperform by working within the interfacedirectly: you can create and edit models,animate, create and edit materials, render,and perform many other 3ds Max functions.MAXScript is comparatively simpler to usethan many other scripting languages, so itis not hard for people unfamiliar with pro-gramming to learn to do something with it.At the same time, it is a robust language thatgives you access to many high-level 3ds Maxfunctions. Even if you have no interest inwriting your own scripts, there are many,

many MAXScripts that are freely available foryou to download, install, and use.

QUICKTIP

A great resource for downloading free MAXScripts on theWeb is www.scriptspot.com.

Some of the things that people do withMAXScript are:

■ Install customized tools and utilitiesthat perform a task or make processesin 3ds Max easier to work with.

■ Create buttons or menu items thatperform custom actions.

■ Automate repetitive tasks.■ Enhance the functionality of preexisting

toolsets within 3ds Max.■ Aid production workflow, like cus-

tomized import and export routines.■ Record actions performed in the inter-

face, and then output them as com-mands in a script.

The MAXScript language is free of many ofthe strict formatting rules that make otherprogramming languages hard for beginners

In this lesson you’ll learn about whatMAXScript is and you’ll learn some ofthe basic concepts and tools used whenworking with MAXScript.

APP-4 AUTODESK 3D STUDIO MAX Maxscript

UNDERSTANDMAXSCRIPT

C5409_Appendix_REV 10/17/05 10:33 AM Page 4

Page 5: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

to learn. However, it does require a degree ofattention to syntax. Syntax refers to the wayin which the lines of code are written andthe formatting rules that you must follow inorder for the script to be read and executedsuccessfully. Using incorrect syntax leads toerrors in the script. However, MAXScript isdesigned to tell you when you’ve writtensomething that it doesn’t understand. Forinstance, if you make a syntax error in yourscript, when you execute the script, 3ds Maxtells you there is a syntax error and displaysthe line with the error. As you learn more

about MAXScript, you start to see what youcan and cannot do as you type in commandsand begin to understand its usage of syntax.

QUICKTIP

Each line of MAXScript is considered a complete expression.In MAXScript the term expression is synonymous with theterm command.

One of the first scripting concepts that youneed to understand is that of the variable.A variable is like a container that you can

place some data in to keep track of. To use avariable, you first have to name the variable,and then you assign it some value. A typicalvariable name in MAXScript might be asingle letter like “X”, or a string of text like“myBox”. Figure 1 shows a very simplescript that uses a variable. Once you havedefined the variable (such as by defininga variable as an object in a scene) you canthen refer to it in later lines of code bysimply using the variable name.

FIGURE 1A simple script

Lesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5

The MAXScript Reference is a concise resource within 3ds Max that documents all ofthe commands and features available in the language. If, after exploring the lessonscontained in this appendix, you would like to continue to learn about MAXScript,then the MAXScript Reference should be your next stop. To open the MAXScriptReference, click Help on the menu bar, and then click MAXScript Reference.

Variable

Commands

C5409_Appendix_REV 10/17/05 10:33 AM Page 5

Page 6: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Working with MAXScript inthe Listener WindowThere are three places within 3ds Maxin which you can write MAXScript: theMAXScript Listener, the Mini Listener,or a MAXScript Editor window. The Listenerwindow, as shown in Figure 2, lets you enterMAXScript commands and execute themimmediately. The window is interactive inthe sense that you can type in a command

and 3ds Max evaluates it and performs theaction when you press [Enter] on thenumeric keypad or press [Shift] + [Enter].In the Listener, you can do things like typein a simple object creation command, press[Enter], and the object is then created in theviewport. You can further manipulate theobject by typing in statements that changethe object’s properties or transforms. Thisgives you the ability to enter code line by

line and see the results, as opposed torunning an entire script full of commandsat once. This is good if you want to do some-thing using MAXScript but don’t need towrite an entire script.

The Listener contains two panes. The pinkpane on top is referred to as the MacroRecorder pane. The Macro Recorder is a

APP-6 AUTODESK 3D STUDIO MAX Maxscript

FIGURE 2MAXScript Listener window

C5409_Appendix_REV 10/17/05 10:33 AM Page 6

Page 7: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

special feature that records actions thatyou perform in 3ds Max, such as openingthe Modify panel and clicking a button onit, and then outputs the actions as scriptcommands. When the Macro Recorder isenabled, the recorded actions are displayedin it. However, you can also use this pinkpane to simply enter commands when theMacro Recorder is not enabled. The whitepane is the output pane. When a scriptprocesses a command it outputs various

results and displays a related message inthe output pane. You can enter commandsin either pane to have them executed.

To open the Listener, click MAXScript onthe menu bar, then click MAXScript Listener.Or, you can open the Listener by clicking theUtilities tab, clicking the MAXScript button onthe Utilities panel to open the MaxScript roll-out underneath it, and then clicking the OpenListener button in the MAXScript rollout.

Appearing on the lower-left side of the statusbar, as shown in Figure 3, the Mini Listeneris a miniature version of the Listener win-dow. It displays the current line from bothpanes of the Listener window. You might usethe Mini Listener to enter and execute aquick command without having to open theregular Listener window. The Mini Listeneris resizable. You can click and drag on thethick line that separates the Mini Listenerfrom the rest of the status bar to increase

Lesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-7

FIGURE 3The Mini Listener

Macro Recorderpane

Output pane

C5409_Appendix_REV 10/17/05 10:33 AM Page 7

Page 8: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

or decrease its size. You can also open theregular Listener window by right-clickingthe Mini Listener, then clicking OpenListener Window on the right-click menu.

Working with MAXScript in aMAXScript Editor WindowThe Listener window is good for executingcommands interactively in 3ds Max, but assoon as you need to make more complexscripts that require more lines of MAXScript

code, you’ll want to use a MAXScript Editorwindow. This window is a basic text editorthat lets you type in MAXScript commandsand build a script. To run the script and exe-cute all of the commands in the Editor win-dow, click File on the Editor window’s menubar, and then click Evaluate All.

To open an empty MAXScript Editor window,click MAXScript on the menu bar, then clickNew Script. To open an existing script in aMAXScript Editor window, click MAXScript

on the menu bar, click Open Script to openthe Choose Editor File dialog box, browse tothe script file you would like to open, thenclick Open. The Editor window appears con-taining the script you chose, as shown inFigure 4.

You can also open a MAXScript Editor win-dow by clicking the New Script button orOpen Script button in the MAXScript rollouton the Utilities panel.

APP-8 AUTODESK 3D STUDIO MAX Maxscript

FIGURE 4Script in MAXScript Editor window

Comments

Reservedwords

Text string

Script provided courtesy of Autodesk, Inc.

C5409_Appendix_REV 10/17/05 10:33 AM Page 8

Page 9: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

The text in a script in the Editor windowappears in different colors. These colors areused to highlight the syntax of the script.Each color indicates a different kind of com-mand. Green text is a comment. Commentsare text that the script does not execute; it issimply there to give you some informationabout the script. Script authors usually putcomments in their scripts to explain whateach part of the script does. To make any lineof code in a script a comment, precede that

line with a double hyphen. Words that areblue are part of the MAXScript language andperform some specific task. If MAXScriptcomes across one of these words or phrasesin a script it thinks that you want to performthat task, so these words are off-limits other-wise. A good example of this is the commandanimate on. This command lets you cre-ate keyframes using MAXScript and alwaysappears in blue.

Words that are black are plain words that arenot reserved and are pretty much the bulktext of a script. Any text in red is a textstring. A text string is a series of alphanu-meric characters that are defined by theuser. They are typically used to make thescript display those actual words on-screenat some point during the execution of thescript (for example, if you wanted to displaya message to the user of a script).

Lesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-9

C5409_Appendix_REV 10/17/05 10:33 AM Page 9

Page 10: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

L E S S O N 2

What You’ll Do Defining a Variable andParameters with MAXScriptOne of the quickest ways to see the power ofMAXScript is to use it to create and manipu-late an object in the viewport. This illus-trates how some commands defined in theMAXScript language mirror the functionsof the software. To create an object withMAXScript, the first thing you want to do isassign it to a variable. This can be done bytyping a command such as the following inthe Listener window. For this example, youonly need to write small code fragments, sousing the Listener is ideal in this situation.

b = box()

In this case, b is the name of the variableand box() is the value of the variable.box() is also a command that tellsMAXScript to create a box with the defaultcreation parameters. Pressing [Enter] onthe numeric keypad after entering this com-mand creates a box with the default parame-ters at the center of world space, as shownin Figure 5. You can create an object with-out first defining it as a variable, by simply

typing box() in the Listener, but thatwould limit how you can manipulate theobject with MAXScript. By defining it as avariable first, you can then refer to the boxlater in the script by its variable name andperform all kinds of operations on it.

The empty parentheses after the word boxtell 3ds Max to create the box with the defaultparameters, with a length, width and height of25 units, and length, width and height seg-ments set to 1. You can, however, define theparameters as you wish. To do so, follow thesyntax shown in the following example:

b = box length:50 width:50height:100

This command creates a box that is 50 ×50 × 100 by adding the parameter namesfollowed by a colon and then the value. Listall the parameters that you want to defineon the same line with spaces in between. Toinitially define the amount of segments inthe box’s height, use the following format:

b = box length:50 width:50height:100 heightsegs:5

In this lesson you will learn how to useMAXScript to create, transform, and mod-ify an object, and how to use the MacroRecorder to create an object. You’ll alsolearn how to run scripts.

APP-10 AUTODESK 3D STUDIO MAX Maxscript

CREATE ANDRUN SCRIPTS

C5409_Appendix_REV 10/17/05 10:33 AM Page 10

Page 11: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

This further defines the box to have fiveheight segments. This could also be doneafter the box was created by typing in asecond line as follows:

b.heightsegs = 5

In this case you are telling 3ds Max tochange the value of the height segmentsproperty of the object that is containedin the variable b (the box).

Transforming an Object with MAXScriptYou can define the position of an object inthe initial line of code that creates the objectby using the position property followed by

the x, y, and z coordinates contained inbrackets. To do so, enter a command suchas the following:

b = box length:50 width:50height:100 position:[50,-50,0]

This places the box at 50 on the x-axis, −50 on the y-axis, and 0 on the z-axis. Todefine the position of an object after it hasalready been created, use the command.pos = next to the variable name, followedby the x, y, and z coordinates contained inbrackets, such as in the following line:

b.pos = [60,-60,10]

When you enter this command, the positioncoordinates of the object contained in thevariable b are updated to reside at 60 unitson the x-axis, −60 units on the y-axis, and10 units on the z-axis. These are absolutevalues that place the box at those coordi-nates. Suppose, however, that you would liketo move the box as an offset of where it cur-rently is. You could then use the move com-mand, followed by the name of the variable,followed by the amount in units on each axisthat you would like to move the box. Thestatement might be entered as follows:

move b [10,10,10]

FIGURE 5Creating an object using MAXScript

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-11

C5409_Appendix_REV 10/17/05 10:33 AM Page 11

Page 12: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

APP-12 AUTODESK 3D STUDIO MAX Maxscript

This moves the object contained in the vari-able b 10 units along the each of its x-, y-,and z-axes as an offset of its current posi-tion, as shown in Figure 6.

Applying a Modifier withMAXScriptAdding a modifier to an object is done withthe addModifier command. You just specifythe name of the modifier and any modifierparameters that you wish to adjust. Forexample, the syntax for applying the Bendmodifer to the box and changing the bend

Angle parameter to 90 degrees on thedefault z-axis would be as follows:

addModifier b (bend angle:90)

Once the modifier has been applied tothe object, the syntax to change one of themodifier’s parameters is slightly different.To change the bend angle once the modifieris applied, the command looks like this:

b.bend.angle = 180

This results in a 180-degree bend angle, asshown in Figure 7. The syntax used in this

command is commonly referred to as dotsyntax. The dots are used in a similar fash-ion as backslashes in a directory path, onlyinstead of referencing folders and files on ahard drive, you are referencing objects (orvariables) and their associated properties.

Table App-1 lists several useful MAXScriptcommands, their syntax, and the results thatthey produce. Use these commands to prac-tice writing MAXScripts that can help youas you work.

FIGURE 6Offsetting the position of an object using MAXScript

FIGURE 7Applying a modifier using MAXScript

C5409_Appendix_REV 10/17/05 10:33 AM Page 12

Page 13: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-13

TABLE APP-1 MAXScript commands

MAXScript command Syntax Description

Object properties

name myObject.name = “object Defines the name of name here” an object

wirecolor myObject.wirecolor = Changes the displaygreen color of an object

using predefined colors; the predefinedcolors are blue, red,green, white, black,orange, yellow, and brown

wirecolor myObject.wirecolor = Changes the display(color 0,255,0) color of an object

using RGB values; theRGB values are listedbetween the parentheses

length myObject.length = 10 Changes the parameterwidth myObject.width = 10 to the value after theheight myObject.height = 10 =. Functions only ifradius myObject.radius = 10 the object has the

parameter. Any objectparameter name can bemanipulated in thismanner.

lengthsegs myObject.heightsegs = 10 Changes the segmentswidthsegs parameter to the value heightsegs after the =

C5409_Appendix_REV 10/17/05 10:33 AM Page 13

Page 14: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

APP-14 AUTODESK 3D STUDIO MAX Maxscript

mapCoords myObject.mapCoords = true A value of “true” enables the GenerateMapping Coordinatescheck box

Transforms

pos myObject.pos = [0,0,0] Defines the absoluteposition property ofthe object; the x, y, zcoordinates are listedbetween the brackets

scale myObject.scale = [2,2,2] Defines the absolute scale property of the object. The scale factor for the object’sx, y, z axis is listed between the brackets. In this example the scale will double.

move move myObject [10,10,10] Moves the object as an offset of its current position; in this example the object will move 10 units along all axes

Modifiers

addModifier addModifier myObject Adds a bend modifier to(bend angle:90) myObject and sets the

bend angle to 90 degrees

C5409_Appendix_REV 10/17/05 10:33 AM Page 14

Page 15: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-15

Using the Macro RecorderThe Macro Recorder lets you record actionsyou perform in the 3ds Max interface, such ascreating objects and changing parameters. Toenable the Macro Recorder, click MAXScripton the menu bar, then click Macro Recorder,as shown on the left in Figure 8. When youclick Macro Recorder, a check mark appearsnext to that menu item to indicate it is active.As shown on the right in Figure 8, you canalso enable the Macro Recorder by clicking

MacroRecorder on the MAXScript Listenerwindow menu bar in the MAXScript Listenerwindow, and then clicking Enable. When theMacro Recorder is enabled, the actions thatyou perform in the user interface appear asrecorded commands in the pink pane of theListener window. You can then save the scriptthat the actions generated and execute it anytime you would like to repeat those actions.For instance, you could turn on the MacroRecorder, create an object in the viewport,

change its parameters, and then apply a mod-ifier to it. You could then turn off the MacroRecorder, open the Listener window, clickFile on the menu bar, click Save As, and thensave the script in the Listener as a script.When you run the script, 3ds Max will gothrough the same series of steps and theobject is created in the same position andwith the same parameters defined when youcreated it with Macro Recorder turned on.

FIGURE 8Two methods for enabling the Macro Recorder

Enable MacroRecorder

C5409_Appendix_REV 10/17/05 10:33 AM Page 15

Page 16: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

APP-16 AUTODESK 3D STUDIO MAX Maxscript

Running a ScriptRunning a MAXScript is a straightforwardprocess. However, there are several ways todo it. You can choose Run Script from theMAXScript menu. A Choose Editor File dialogbox appears that lets you choose a script torun. You can also run a script by pressing theRun Script button found in the MAXScriptrollout on the Utility panel. To run a scriptfrom within the Listener, click File on the

Listener’s menu bar, click Run Script to openthe Choose Editor File dialog box, as shownin Figure 9, and then browse to and select afile in the Choose Editor File dialog box. Yetanother way to run a script is to open thescript in a MAXScript Editor window, clickFile on the menu bar of the Editor window,and then click Evaluate All.

Whichever way you choose to run a script,what happens immediately after you run it

varies according to the purpose of the script.Some scripts call up a special dialog box orfloating rollout that contains some specialtool for doing something. Some scripts canbe specially defined as a utility, and theythen appear in the Utilities drop-down listin the MAXScript rollout on the Utilitiespanel. Other scripts may simply createsome object or animation in the viewportswhen they are run.

FIGURE 9Choose Editor File dialog box

C5409_Appendix_REV 10/17/05 10:33 AM Page 16

Page 17: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Run an existing MAXScript(.ms)

1. Click MAXScript on the menu bar, then clickRun Script.

The Choose Editor File dialog box opens.

2. Browse to the folder in which your DataFiles are stored, then double-clickExample_RandObjects.ms.

A floating dialog box called Random Copiesappears.

TIP Script provided courtesy of Autodesk, Inc.

3. Create a teapot in the Perspective viewport.

4. In the Random Copies dialog box, click theSource Object button (it currently says“none”), then click the teapot in the view-port to designate it as the source object.

5. Click the Create copies button in theRandom Copies dialog box.

Ten instances of the teapot are randomlyspaced around the viewport, as shownin Figure 10.

6. Experiment with creating more randomcopies, then close the Random Copiesdialog box and reset 3ds Max.

There are many scripted tools like this freelyavailable on the Web, some more usefulthan others.

You ran a preexisting script, then used the dialogbox produced by the script to make randommultiple copies of an object in a scene.

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-17

FIGURE 10Random Copies dialog box

C5409_Appendix_REV 10/17/05 10:33 AM Page 17

Page 18: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Define a variable and itsparameters with MAXScript

1. Right-click the Mini Listener window,then click Open Listener Window on theright-click menu.

The Listener window appears.

2. Click in the upper pink pane of the Listenerwindow.

A blinking cursor appears in the pane. It isa good idea to enter your commands in thepink pane so the output window doesn’t getcluttered.

TIP You may need to click and drag downthe bar dividing the panes in the Listenerwindow in order to see the pink pane.

3. Type the following command: b = boxlength:20 width:20 height:50 heightsegs:5

4. Press [Enter] on the numeric keypad.

TIP If your keyboard doesn’t have a numerickeypad, press [Shift] + [Enter].

A box appears at the center of world space,with the creation parameters that youentered, as shown in Figure 11. The resultsof the operation appear in the white outputwindow, displaying the properties of the box.

You opened the Listener window from the MiniListener right-click menu, entered a commanddefining a variable as a box with specific parame-ters, then executed the command.

APP-18 AUTODESK 3D STUDIO MAX Maxscript

FIGURE 11Box created with MAXScript

Commandresults

C5409_Appendix_REV 10/17/05 10:33 AM Page 18

Page 19: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Transform an object withMAXScript

1. Type b.pos, then press [Enter] on thenumeric keypad.

The command b.pos simply asks theListener to display the current x, y, z posi-tion of the object contained by the variablenamed b, which is the box. The x-, y-, andz-axes coordinates of the box are displayedin the output pane as [0,0,0].

2. Type b.pos = [50, −50, 0], then press[Enter] on the numeric keypad.

The box moves to the new coordinates inthe viewports.

3. Enter and execute the following command:move b [10, 10, 10].

This moves the box 10 units along eachaxis. The output pane says OK to confirmthe move.

4. Enter and execute the following command:b.pos.

The output pane shows the new position of thebox ([60, −40, 10]), as shown in Figure 12.

5. Right-click in the upper pink pane of theMini Listener window.

A menu appears that shows a listing of thelast bunch of commands entered.

(continued)

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-19

FIGURE 12Box position in Listener window

New position of box

C5409_Appendix_REV 10/17/05 10:33 AM Page 19

Page 20: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

6. Click b.pos = [50, −50, 0] on the right-clickmenu, as shown in Figure 13.

When you select this previously used com-mand, it is entered in the script for a secondtime, moving the box back to where it was.

You determined the current position of a variable,changed its position, and offset it from its newposition. You also selected a previously enteredcommand from a Mini Listener right-click menuto execute the command again.

Apply a modifier usingMAXScript

1. Beneath the b.pos command in the pink paneof the Listener window, enter and execute thefollowing command: b.heightsegs = 5

2. Click in the viewport and press [F4] to turnon edged faces in the viewport.

The box now has five visible heightsegments.

3. Enter and execute the following command:addModifier b (bend angle:90)

The command applies the modifier andchanges its angle parameter to 90. The boxbends 90 degrees on the z-axis.

(continued)

APP-20 AUTODESK 3D STUDIO MAX Maxscript

FIGURE 13Mini Listener right-click menu

C5409_Appendix_REV 10/17/05 10:33 AM Page 20

Page 21: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

4. Enter and execute the following command:b.bend.angle = 180

The box is now bent 180 degrees on the z-axis, as shown in Figure 14.

5. Click File on the menu bar in the Listenerwindow, then click Save As, then save thescript as bentBox.

6. Close the Listener window, then reset3ds Max without saving your changes.

7. In the reset scene, click MAXScript on themenu bar, then click Run Script.

The Choose Editor File dialog box appearsand prompts you to select a file.

8. Double-click the bentBox.ms script.

The script you just wrote runs, and a boxappears in the viewports at the position of[50, −50, 0], bent at 180 degrees.

9. Reset 3ds max.

You entered commands that added more seg-ments to the box, applied a Bend modifier with abend angle of 90 degrees, then modified the bendangle. You then saved the code and ran the scriptto produce the same results in a new scene.

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-21

FIGURE 14Modified box

C5409_Appendix_REV 10/17/05 10:33 AM Page 21

Page 22: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Create and run a script usingthe Macro Recorder

1. Open the Listener window, click Edit on theListener window menu bar, then click ClearAll to clear all code from the Listener.

2. Click MaxScript on the menu bar, then clickMacro Recorder, as shown in Figure 15, toenable the Macro Recorder.

3. On the Create panel, click the Plane button,then click and drag in the Top viewportto create a plane that fills most of the viewport.

As you create the Plane, the script com-mands appear in the Listener window.

4. On the Modify panel, change the LengthSegs and Width Segs both to 40.

5. Click the Modifier List list arrow, then clickNoise to apply the Noise modifier to theplane.

6. On the Modify panel for the Noise modifier,change the Scale parameter to 30 and theStrength for the z-axis to 20.

The Noise modifier creates a terrainlikeeffect on the plane. As you perform theactions of modifying the plane, the scriptedparameters are updated in the Listener, asshown in Figure 16.

7. Click MaxScript on the menu bar, then clickMacro Recorder to turn off the MacroRecorder.

(continued)

APP-22 AUTODESK 3D STUDIO MAX Maxscript

FIGURE 15MaxScript menu

C5409_Appendix_REV 10/17/05 10:33 AM Page 22

Page 23: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

8. Click File on the Listener window menubar, click Save As, save the script asterrain, close the Listener, then reset 3dsMax without saving changes.

9. In the new scene, click MaxScript on themenu bar, click Run Script, then double-clickterrain in the Choose Editor File dialog box.

The script runs and the terrain objectappears in the viewports.

10.Reset 3ds Max.

You cleared all code from the Listener window,enabled the Macro Recorder, and created a planeand applied a modifier to it. You then disabled theMacro Recorder, saved the recorded code as ascript, then ran the script to repeat the recordedaction.

Lesson 2 Create and Run Scripts DISCREET 3D STUDIO MAX APP-23

FIGURE 16Plane with Noise modifier applied

Actionsrecorded inListener

C5409_Appendix_REV 10/17/05 10:33 AM Page 23

Page 24: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

L E S S O N 3

What You’ll Do Understanding MacroScriptsMacroScripts are MAXScripts that aredesigned to be assigned to a user interfaceelement like a toolbar button, a menu item,or a keyboard shortcut. Once assigned to aninterface element, the MacroScript can beexecuted by pressing the button, clicking themenu item, or typing the shortcut. To makea script a MacroScript, it must be defined assuch within the script. To define a script asa MacroScript, add the following line of codeat the top of the script.

macroScript scriptName category:“CategoryName”

In this example, scriptName would bereplaced by the name of the script, andCategoryName would be replaced by thecategory that the author wants the script tobe defined as. The category name that youdefine here is used when you apply the scriptas an interface element. You can invent yourown category name, but it is a good idea tolearn more about the categories that 3ds Maxuses by default.

Even if you don’t want to know how toauthor your own MAXScripts and just wantto be able to use scripts that someone elsewrote, you should know how to identify thecategory of a script. You need this informa-tion when you want to add the script to auser interface element in the CustomizeUser Interface dialog box. To find out thecategory name of a specific MacroScript,open the script and take a look at the cate-gory name identified at the top of the script.

Once a MacroScript has been installed, youcan then assign it to an interface featureusing the Customize User Interface dialogbox. MacroScripts can also be generatedautomatically by dragging and dropping asection of code from either the MAXScriptListener or a MAXScript Editor window toa toolbar.

Assigning a Script to aToolbar ButtonAssigning a script to an interface element isa two-step process. First, you install thescript by choosing Run Script from either

In this lesson you’ll learn how to installa MacroScript, create a new toolbar, thenassign the script as a button on the toolbar.

APP-24 AUTODESK 3D STUDIO MAX Maxscript

ASSIGN A SCRIPT TO ATOOLBAR BUTTON

C5409_Appendix_REV 10/17/05 10:33 AM Page 24

Page 25: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

the MAXScript menu or the MAXScript roll-out on the Utilities panel, and then browsingto select the script that you want to run.When you do this, a copy of the script isautomatically placed in the UI/MacroScriptsfolder located in the 3ds Max root folder onyour hard drive. This folder contains all ofthe user interface elements that are installedas MacroScripts. Every time you start 3dsMax, it loads these scripts from this folder.

QUICKTIP

As you acquire MacroScripts from Web downloads or otherlocations, it is important to keep them in a separate folderaway from the UI/MacroScripts folder so that the scripts arenot run more than once by accident.

Before you can actually run the script, youmust first add an element to the user inter-face that can be used to repeatedly executethe script. To do this, click Customize on themenu bar, and then click Customize UserInterface. The Customize User Interface dia-log box appears. To assign a MacroScript as abutton on a toolbar, click the Toolbars tab,and then click the Category list arrow andselect the category of the script that youwant to assign. As mentioned before, thisinformation can be found by opening aMacroScript and looking at the categoryname at the top of the script. Once youselect the category, the names of all of thescripts that are available from that category

appear in the Action list on the Toolbars tab,as shown in Figure 17. Next, drag and dropthe script that you want from the Action listto any 3ds Max toolbar.

You can create your own toolbar by clickingthe New button on the Toolbars tab. A dialogbox appears asking you to name the new tool-bar. Type in a name and click OK and a newand empty, free-floating toolbar appears. Toassign a MacroScript to this new toolbar, dragthe script to it from the Action list. A newbutton with the script name appears on thetoolbar. When you press the button the scriptexecutes.

FIGURE 17Toolbars tab in Customize User Interface dialog box

Lesson 3 Assign a Script to a Toolbar Button DISCREET 3D STUDIO MAX APP-25

Category

Script name

C5409_Appendix_REV 10/17/05 10:33 AM Page 25

Page 26: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

Install and run a MacroScript

1. Click MAXScript on the menu bar, then clickOpen Script.

The Choose Editor File dialog box opens.

2. Browse to the folder where your Data Filesare stored, then double-click the file calledMacro_FileBrowser to open it.

The script opens in a MAXScript Editor window. This is a MacroScript that createsa file-browsing utility that you can use asa free-floating window in 3ds Max.

TIP Script provided courtesy of Autodesk, Inc.

3. As shown in Figure 18, notice the line nearthe top of the script that reads category:“MAX Script Tools”.

This is the category name that you need toknow later in the Customize User Interfacedialog box.

4. Click File on the MAXScript editor menubar, then click Evaluate All.

Choosing Evaluate All in the MAXScriptEditor window is essentially the same thingas choosing Run Script from the MAXScriptmenu, and the script is installed in theUI/MacroScripts folder in the 3ds Max rootfolder on your hard drive.

5. Close the Editor window, click Customize on themenu bar, then click Customize User Interface.

The Customize User Interface dialog box opens.

6. Click the Toolbars tab, click the Categorylist arrow, then click MAX Script Tools.

(continued)

APP-26 AUTODESK 3D STUDIO MAX Maxscript

FIGURE 18Macro_FileBrowser script

Category

Script provided courtesy of Autodesk, Inc.

C5409_Appendix_REV 10/17/05 10:33 AM Page 26

Page 27: C5409 Appendix REV - DelmarLearning.comLesson 1 Understand Maxscript DISCREET 3D STUDIO MAX APP-5 The MAXScript Reference is a concise resource within 3ds Max that documents all of

The script named File Browser appears inthe Action list. This is the script you justinstalled.

7. Click the New button located on the rightside of the dialog box.

The New Toolbar dialog box appears,prompting you to enter a name.

8. Type in a name for the new toolbar, thenpress OK to close the dialog box.

A new, empty toolbar appears with the nameyou just gave it. You may need to expandthe toolbar to see the full name.

9. Drag and drop the File Browser item fromthe Action list on the Toolbars panel to thenew toolbar.

A new button appears on the toolbar with thename File Browser, as shown in Figure 19.

10.Close the Customize User Interface dialogbox, then click on the new File Browserbutton to open the File Browser utility.

A FileBrowser dialog box appears on-screenthat you can use to create a new script oropen a script in an Editor window, or browseto an existing script and run a selectedscript.

11.Close the FileBrowser dialog box, then reset3ds Max.

You installed a MacroScript by choosing to EvaluateAll in the MAXScript Editor window. You then foundthe MacroScript’s category in the Customize UserInterface dialog box, created a new toolbar, thendragged the MacroScript to the toolbar to createa button to execute the script when clicked.

Lesson 3 Assign a Script to a Toolbar Button DISCREET 3D STUDIO MAX APP-27

FIGURE 19Custom toolbar with new button

C5409_Appendix_REV 10/17/05 10:33 AM Page 27