|
Mastering
3D Studio MAX R3 |
Some objects may not allow you to access position data using .pos,
because they have a special controller. For example, because bone objects
have an IK controller, using .pos will return invalid or
undefined. Only bones with End Effectors or without the IK controller
will allow .pos. To know their position in space, you can
use .transform.translation, which will read the current
translation of the object in 3D space. This command works for all objects
in MAX, no matter which controller has been applied. Use .pos
for PRS controllers and .transform.translation for all
others.
Scale works exactly the same way as position. In some cases, if .scale
fails, you can use .transform.scale.
Rotation
Rotation might be a little bit difficult to understand. This is because
MAX uses TCB rotation by default. TCB rotation uses a quaternium algebra
(or quats) to calculate the rotation. TCB rotations are smooth for animation,
but its a little hard to manipulate TCB values in MAXScript, since
the quaternium doesnt show us directly the rotation of the object.
For more information on quats and TCB rotation, refer to the MAXScript
Online Help.
Well make rotation simple and use a different rotation method,
called eulerangles. Eulerangles are X/Y/Z rotation vectors that work the
same way as position and scale. In order to access the rotation without
using quats, you need to access each axis independently, using object.rotation.axis_rotation,
substituting x, y, or z for axis.
For instance, to access a boxs Z rotation for an object named b,
you can use b.rotation.z_rotation.
Hands-On
MAXScript: Creating an Array and Changing the Rotation
Lets create a script that creates an array of objects and changes
their rotation across the array, as in Figure 14.4.
FIGURE
14.4 Results of the Array Rotate script
The array command can create a 2D array but does not allow you to rotate
the objects in two axes in the array. This script enables you to assign
a different X and Y rotation.
To work with this script, you first will need to select an object. Then,
the script will ask you how many rows and columns you want in the array.
Finally, you define the position of the first object in the bottom left
of the array.
Start a new script and type all the commands shown in Listing 14.1. If
you prefer, open this code, using MAXScript Ø
Open Script, from the CD-ROM that accompanies this book, in the file named
array_exercise.ms.
LISTING 14.1: The Array Rotate script (array_exercise.ms)
if selection.count == 0 then format “Select at least one object.\n” else
(
obj = $
size_x = abs(obj.min.x - obj.max.x)
size_y = abs(obj.min.y - obj.max.y)
nx = getkbvalue prompt:”Enter number of objects in X:” as float
ny = getkbvalue prompt:”Enter number of objects in Y:” as float
pt = pickpoint prompt:”Select Start Position:”
start_x = -90
start_y = -90
end_x = 90
end_y = 90
for j in 0 to ny do
(
for i in 0 to nx do
(
s = copy obj
s.wirecolor = obj.wirecolor
s.pos = [pt.x + size_x * i, pt.y + size_y * j, pt.z]
s.rotation.x_rotation = start_x + (end_x - start_x) * i/nx
s.rotation.y_rotation = start_y + (end_y - start_y) * j/ny
)
)
)
Select all lines and drag them to the Compounds Tab, or to any toolbar
of your choice. Edit the icon and rename it to Array Rotate.
Now lets examine how the script works.
First, it checks to see if theres an object selected. If not, the
script returns a message asking the user to select an object before using
the script, then the script ends.
If there are objects selected, abs (obj.maxobj.min)
tells you how large the object or the selection is in X and Y. Using GETKBVALUE
and PICKPOINT, you ask the user for the information you need: the number
of copies in X, Y and the position of the first object.
start_x and snd_x define the initial and
final rotation of the object around the X axis, and this rotation will
be progressive, which means that any object between the first and the
last in X will be rotated until it reaches the final rotation value. The
same happens in Y.
You used nested FOR commands here, one to copy the object in X and the
other to copy the object in Y. This is possible because youre multiplying
the size and the i or j value, which will make it possible to calculate
the position of the object in the array. The math is simple: initial position
in X added to the distance in X multiplied by the number of the copy (i
or j)
Next comes the rotation part. The rotation is incremental, and you have
the start and end values, so the script makes this calculation:
start + (endstart) * object number / total number of objects
This way you will increment the rotation for each copy thats done.
Since youre doing the same in X and Y, you will be able to apply
rotation to the objects in the two axes.
You could enhance the script by asking the user for the initial and final
rotation, and you could also add a user interface, which you will learn
to do in Chapter 15.
Creating Materials
Materials are essential to any scene you develop in MAX. And of course,
MAXScript can access each and every parameter in any material. To assign
or access a material in one object, use object.material.
This will return the material assigned to an object. You can also assign
a material to a variable and later assign it to an object, like variable
= material and then object.material = variable.
The same rules apply to MAXScript as elsewhere in MAX: if you modify
a material that has been assigned to an object, all other objects that
have this material will be modified, unless you clone it before modifying.
Standard
Materials
Materials work the same way as objects. If you assign a Standard material
to one object, it will be assigned with default values, until you change
them.
You can create a blank standard material by simply typing
variable = standardmaterial()
or
variable = standard()
Then, you can assign this material to an object using
object.material = variable
|
|
| NOTE You can assign the
material directly to the object. Its just easier to manage the
material when you have it assigned to a variable first.
|
© 2000, Frol (selection,
edition, publication)
|