Титульная страница
ISO 9000 ISO 14000
GMP Consulting
 
Mastering 3D Studio MAX R3

Previous Table of Contents Next


As always, you can use the Macro Recorder to make it easier to design the script. All Particle Array properties could have been recorded and adjusted to the variable names in the script.

We introduced some animation techniques in this script. Animationrange controls the number of frames in the scene. With animate on is the same as turning on the Animate button. Then, at time is the same as going to that frame to adjust an object’s value.

Like all other exercises, you can drag this script to the toolbar and create a Macro Script. After that, select the Asteroid object and run the script.

You can load the file asteroid_explosion_final.max from the CD included with this book to see the result. In this example, the explosion starts on frame 50 and ends on frame 100.

Working with Splines

Splines are essential for modeling. Most of what you do starts with spline modeling. You can create shapes in MAXScript the same way you create primitives or other objects, and you also can create splines by creating each vertex and segment.

Creating Shapes

You can create any Shape object in MAXScript, except the Line object. For instance, you can create a circle using c = circle radius:30.

2D objects can only have certain modifiers applied, like Extrude, Bevel, or Lathe. MAXScript allows you to check whether the object is a spline before adding these modifiers. This can be done using validmodifier c (extrude()), for instance. If it returns true, this modifier can be applied to the object.

Converting Shapes to Splines

Like 3D objects, shapes can have their modifiers collapsed, converting them to editable splines. You can use CONVERTTOSPLINESHAPE to convert any shape to an editable spline. The advantage of working with editable splines is the ability to animate each vertex and the advantage of manipulating its vertex, segments, and splines. Many MAXScript commands require an editable spline to work.

The Editable Spline object, which can be created through the Line command or by converting a Shape object, has no options except the Global Spline options (steps, adaptive, renderable, etc).

Understanding Splines

To better understand shapes and splines, let’s review a few terms. The shape is the entire 2D object. The spline is each element, closed or not, in the shape. A vertex or a knot is each control point of the spline. A segment is an interval between two vertices. You can see this in Figure 14.13.


FIGURE 14.13  Shape elements

You can find out how many vertices are in one shape or spline using the NUMKNOTS command. This works in any shape; for instance, using the previous example, numknots c will return 4, which is how many vertices are present in a Circle shape.

However, to use NUMKNOTS in a specified spline, the object must be an editable spline, and you must specify the number of the spline in the shape. To know how many splines are there in a shape, use NUMSPLINES, which is also valid only in editable splines. Follow this example to understand how NUMKNOTS and NUMSPLINES work:

 d = donut()
 d = converttosplineshape d
 numsplines d
 numknots d 1

A Donut shape has two splines, so numsplines d will return 2. Then, each of these splines have four vertices, which means numknots d 1 will return 4. In a Donut shape, the first spline is the one related to Radius 1. On other shapes it will vary, but as a rule, splines are numbered according to their creation order. Another way to know the number of splines is using the object’s .numsplines property.

CURVELENGTH returns the length of a spline and is valid in all shapes. For example, let’s calculate the length of the circle. First, create it using c = circle radius:30. Now, curvelength c will return 188.494, which is the length (in this case, the circumference) of a circle with radius 30.

You can read any coordinate in the spline by specifying its position relative to the start. To do so use LENGTHINTERP or PATHINTERP. Both work the same way, but differ in their calculation method. PATHINTERP calculates the points based on the segment’s length; LENGTHINTERP calculates them based on the overall length of the spline. This is the same difference as when you animate an object through a Path and turn Constant Velocity off or on.

For instance, if you want to know the position of the point that is 25% from the start of the circle in your example, you could use lengthinterp c 0.25. If you use it in an object that has more than 1 spline, you need to indicate which spline you’re looking at, using lengthinterp c 1 0.25.

Editing Splines

MAXScript has commands that are useful for editing spline sub-objects in scripts. For instance, using MAXScript, you can change all vertices of a shape to “corner.” You can also reverse the direction of a spline, etc.

Editing Vertex Data

You can edit almost all vertex parameters and properties—the vertex position, its tangent, etc.—with MAXScript. All the commands you will see here will only work in editable spline, so if you need to work on any other shape, it first must be converted to a spline.

To find out the type of a vertex (corner, Bezier, smooth, or Bezier-corner), you can use GETKNOTTYPE. To change it, use SETKNOTTYPE. To use both commands, you need to specify the spline number and the vertex number. For instance, let’s set the second vertex of a circle to corner. Since this object has only one spline, the spline index will be 1 in all commands.

 c = circle radius:30
 c = converttosplineshape c
 getknottype c 1 2
 setknottype c 1 2 #corner
 updateshape c

NOTE Every time you make changes to a shape or spline and want to see it on screen, you need to use the UPDATESHAPE command.

You can use GETKNOTPOINT and SETKNOTPOINT to change the position of a vertex. Let’s edit the position of vertex #2 and move it, creating a droplet shape, like in Figure 14.14. First, use getknotpoint c 1 2 to see the actual position of the vertex. It will be something close to [0,30,0]. Move it 30 units up, using setknotpoint c 1 2 [0,60,0].


FIGURE 14.14  Droplet shape

You can use SETKNOTSELECTION and GETKNOTSELECTION to select vertices or to read the vertex selection. Both commands will use an array containing the indexes of the vertices that are selected. SETKNOTSELECTION also has the option to retain the previous selection, adding the new selection to it. For instance, let’s select vertices 1, 2, and 3 in your example, using setknotselection c 1 #(1,2,3). To enable the selection, you must enter sub-object mode. This is done using SUBOBJECTLEVEL. In your example, just type subobjectlevel = 1, and you will see the vertices selected.

Editing Segment Data

The options available for working with segments are almost the same as those for working with vertices. Of course, you cannot set the segment position, but you can select a segment and set its type (curve or line).


Previous Table of Contents Next

© 2000, Frol (selection, edition, publication)

 
Rambler's Top100