Splines

A spline is a geometric object that represents a 1 dimensional object (a line) where the path of the line is dependant on a set of ordered vertices.

In 3DS Max, splines are contained in primitive objects called shapes. Like meshes, shapes have editable variants called Editable Splines; confusingly enough, an editable spline is in fact a shape and not a spline. This leaves us with the following hierarchy:

  • Shape: a primitive object that contains 0 or more splines
  • Spline: a line object that conatins a set of vertices
  • Vertex (Known as a Knot in 3DS Max): a control point for a spline definining a position that the spline must pass through. Also defines the style of curvature the line will exhibit as it passes through the vertex

Creating Splines in Code

While you can easily create splines through the UI by creating lines or other shapes from the command panel, creating splines in code is slightly more involved, though still easy. In the following we will be using the SplineShape object. Please refer to Autodesk's Maxscript reference of SplineShape for a thorough rundown of all functions.

splineShape

To define a spline in Maxscript, we first need to create a shape. This is done by calling the splineShape constructor.

myShape = splineShape()

This will create a new SplineShape object and return it. A shape initially starts out containing no splines.

addNewSpline

We can add splines to it by calling addNewSpline.

splineIdx = addNewSpline myShape

We need to pass a shape to addNewSpline for the function to create a new spline in. The function returns the index of the newly created spline. This will be repeatedly used. Note that the index of the first spline in a shape is always 1 so if your shape contains only 1 shape, it is safe to pass the number 1 anywhere a spline index is required.

Note that you could add as many splines as you want to a shape. Like a SplineShape, a spline initially starts out containing no vertices (knots).

addKnot

We can add knots (vertices) to a spline by calling the addKnot function.

knot1Idx = addKnot myShape splineIdx #smooth #curve [0, 10, 0]
knot2Idx = addKnot myShape splineIdx #smooth #curve [0, 20, 0]

This will create 2 knots, the first positioned at [0, 10, 0] and the second positioned at [0, 20, 0]. Like addNewSpline, addKnot will return the index of the knot created.

This function requires the following parameters:

  1. SplineShape object
  2. Index of a spline to create the knot in
  3. Type of knot
  4. Type of curve
  5. Position

As a reference, here is addKnot's documentation:

addKnot <shape> <spline_index_integer> \
(#smooth | #corner | #bezier | #bezierCorner) \
(#curve | #line) <position_point3> \
[invec_point3 outvec_point3] \
[where_integer]

getKnotPoint

For many algorithms involving splines, you will need to retrieve the position of a knot. This can be done by calling the getKnotPoint function.

knot1Pos = getKnotPoint myShape splineIdx knot1Idx
knot2Pos = getKnotPoint myShape splineIdx knot2Idx

Modifying Shapes

Shape objects accept modifiers like any other objects in the system. You may use this to have a spline match the shape of modified geometry by taking advantage of modifiers. For example, if we have a box with a bend modifier on it and we want to put a spline through the box matching the center line of the box, we could apply a similar bend modifier to the object.

Once you have shapes the way that you want you could collapse them. Eg:

-- Assume that sp1 is a shape...

-- Add a bend modifier to sp1
addModifier sp1 (Bend angle:90)

-- Collapse sp1 into an editable shape
covertto sp1 Editable_Spline

addAndWeld

To attach two shapes together, use the addAndWeld function. Here's the signature:

addAndWeld <to_shape> <from_shape> <weldthreshold_float>

The destination shape is the first parameter, the source shape is the second parameter, and the third parameter is a weld threshold; vertices that are within weld threshold distance are welded together.

Path Constraint

Once we've created a spline that we're satisfied with, we can use a path constraint to get an object to animate on top of the spline track. Do this by selecting the object then adding the constraint through Animation -> Constraints -> Path then select the path.

You can modify the properties of this constraint by selecting the constrained object and switching to the motion tab (to the side of the modify tab, looks like a wheel). For instance, to get the object to follow the rotation of the track, check the Follow option and set the appropriate axis below.

NOTE: For your A3, make sure to get the root bone to follow the track and not the train itself.