|
|
Mastering
3D Studio MAX R3 |
CHAPTER 14
Manipulating Scenes with Maxscript
Featuring
- Scripting Objects
- Accessing Global Object Properties
- Replicating Objects
- Creating Materials
- Scripting Modifiers and Options
- Creating and Managing a Hierarchy
- Using Environment and Render Effects
- Working with Splines
- Working with Meshes
One of the primary uses of MAXScript is to work with
scene objects, creating, modifying, and automating any task that can be
done with the objects.
In this chapter you will learn about some of the MAXScript tools you
can use to manipulate scenes and work with objects in MAXScript. Throughout
the chapter you will have an opportunity to try out what youve learned
by creating some actual scripts.
Scripting Objects
Using MAXScript you can create and modify almost everything in MAX. There
are some restrictions, which are the same restrictions that apply to the
Macro Recorder (see Macro Recorder Limitations in Chapter
13).
Working with objects in MAXScript takes advantage of the object-oriented
behavior of 3D Studio MAX. Lets see how.
Creating an
Object and Accessing Its Properties
You can create objects in MAXScript by just assigning an object to a
variable. Its as simple as typing a = box(). If you
do so, a default-size box (25x25x25) will be created. This way, you can
create every primitive, helper, camera, light, and so forth. Complex objects,
such as editable meshes, splines, patches, NURBS, and compound objects,
require different commands.
As soon as you assign an object to a variable (or create an object by
assigning it), you can access any object property using this variable.
The property will be valid until the object is deleted, the variable is
redefined, or MAX is restarted.
|
|
| TIP If you want a variable
to be saved with a scene, you need to use Persistent Global variables.
See Chapter 17 for more information.
|
When you create a simple object, as in the box example above, its
created with a default size, position, etc. You will most likely want
to access and change this data. But in order to do so, you need to know
which data is available for you. Why? Because each and every object has
different properties. For instance, a sphere has a radius, but a box has
height, length, and width. To list an objects properties, you use
the SHOWPROPERTIES command. Once you have typed a = box(),
then if you type showproperties a MAXScript will list the
properties in the Listener, as shown in Figure 14.1.
FIGURE
14.1 SHOWPROPERTIES result
You can access any of this boxs properties by typing a.property
(for example, a.height). You can also change the values
of each property, typing a.property = value (a.height
= 60).
You can set an object property at the time of creation, specifying the
properties and their
values: a = box height:60 width:30 length:40
Theres no limit in the number of properties you can define, and
if any property is not listed, it will be set to its default value. If
a property is listed but does not relate to that object, its discarded.
|
|
| TIP Besides using SHOWPROPERTIES,
you can also record creating an object and changing its parameters
to the values you need. This makes it easier for you to manipulate
objects, and keeps you from having to memorize thousands of commands.
|
Accessing
an Object by Name
Sometimes you do not create objects in MAXScript, but you need to process
objects already created. To do so, you need to learn how to select and
manipulate selections, and how to refer to objects by name.
You can access any object by its name simply by adding the $ symbol before
the object name. For instance if you have an object named Wall,
you can access it using $wall.
|
|
| NOTE Object names in MAXScript
are not case-sensitive. If you have two objects, one named Ball
and another named ball (and MAX, unfortunately, allows
you to name as many objects as you wish with the same name), MAXScript
will not distinguish between them even if you use $ball or $Ball.
|
You can also assign variables that point to named objects in the scene,
like a = $wall. From then on, you can access the object
as a or as $wall.
|
|
| WARNING If the object is
renamed, you cannot use $name any more. But if you
assign $name to a variable and later rename the object,
this variable can be used. For example, you could create a box named
Box01, type var = $box01, and then rename
Box01. If you type var, MAXScript shows
you the box properties; if you type $Box01 it returns
undefined.
|
Working with
Selections
Sometimes you dont know a specific object name, or you do not want
to use a specific object, but you want to use an object selection. You
can refer to selections in two ways: using selection, which
is an array of all selected objects, or using the $ symbol.
There are some peculiarities in selections that need to be addressed.
It is very important to know how many objects have been selected, using
$.count or selection.count. This is important
because if selection.count returns only one object selected,
this object can be accessed simply using $. On the other
hand, if it returns more than one object, you must use selection[i]
to access each object in the selection array.
Manipulating Selections
You can manipulate selections by adding or removing objects from the
selection. To remove an object from the selection, use the DESELECT command;
to add objects to a selection, use the SELECTMORE command. To simply discard
any selection and select a specific object, you can use the SELECT command.
|
|
| TIP You can use wildcards
when specifying object names. For instance, if you want to select
all objects that start with B, you can use select
$B*. If you want to select all objects that end in 01
you can use $*01. You can also use the ?
wildcard to represent a single character.
|
© 2000, Frol (selection,
edition, publication)
|
|