Quantcast
Channel: The 3ds Max Blog
Viewing all articles
Browse latest Browse all 73

An Introduction to MAXScript

$
0
0

This is a MAXScript tutorial in the form of a MAXScript program that you can download and run. The program demonstrates a simple procedural animation of a walking stick-figure and an animated camera associated with the viewport. For more information on some of the concepts used in this script the following links will bring you to the relevant online MAXScript documentation:

Here is the source code of the program.

/*
MAXScript Tutorial
This script demonstrates creating a procedural animation using MaxScript. 
It generates a simple stick man from cylinders and a sphere, and animates 
him in a simple walk cycle.
Other concepts demonstrated include:
* Create simple primitives
* Link objects
* Create animation frames
* Set node transform properties in local coordinate space. 
* Use the sine function to achieve smooth back and forth motion
* Creating a camera with a target and rotating it
* Setting the active viewport, and associating it with a camera
*/
/* Clear the scene */
delete objects
/* Initialize variables to help define the objects */
armlength     = 15
leglength     = 15
armradius     = 2
legradius     = 2
/* Create Body Parts */
body        = cylinder radius:5 height:20 
leftarm     = cylinder radius:armradius height:armlength 
rightarm    = cylinder radius:armradius height:armlength 
leftleg     = cylinder radius:legradius height:leglength 
rightleg    = cylinder radius:legradius height:leglength 
head        = sphere radius:5 
/* Set the arm positions */
leftarm.pos.x     = -body.radius - armradius
rightarm.pos.x    = body.radius + armradius
leftarm.pos.z     = body.pos.z + body.height 
rightarm.pos.z    = body.pos.z + body.height 
/* Set the leg positions */
leftleg.pos.x     = leftarm.pos.x
leftleg.pos.y     = leftarm.pos.y
rightleg.pos.x    = rightarm.pos.x
rightleg.pos.y    = rightarm.pos.y
/* Rotate the arms and legs by 180 degrees so that they point downwards */
leftarm.rotation.x_rotation     = 180
rightarm.rotation.x_rotation    = 180
leftleg.rotation.x_rotation     = 180
rightleg.rotation.x_rotation    = 180
/* Set the head location */
head.pos.z         = body.pos.z + body.height + head.radius
/* Link the body parts to the body */
leftarm.parent  = body
rightarm.parent = body
leftleg.parent  = body
rightleg.parent = body
head.parent     = body
/* Move the body up so the legs are aligned to the x/y plane. */
body.pos.z = leglength
/*
Animate the body to move along the local Y axis a small amount per frame
The "animate on" context expression automatically creates 
key frames when animatable properties are modified
*/
animate on (
    for t = 0 to 100 by 5 do (
        /* 
	    The "at time" context expression gets/sets animatable properties at a specific time frame.
        */
        at time t (        
            /* Assure that transforms occur in the node's local coordinate system */
            in coordsys local (
                move body [0, -2.5, 0]
            )
        )
    )
)
/*
Rotate arms and legs
In order to get the movement to change direction smoothly I use a sine function: 
*/
animate on (    
    swingArc = 45  /* The number of degress each arm and leg swings */
    spd = 10       /* Controls the speed at which the swing occurs */
    for t = 0 to 100 by 5 do (
        at time t (
            leftarm.rotation.x_rotation  = sin (t * spd) * swingArc + 180
            rightarm.rotation.x_rotation = sin (t * spd + 180) * swingArc + 180
            leftleg.rotation.x_rotation  = sin (t * spd + 180) * swingArc + 180
            rightleg.rotation.x_rotation = sin (t * spd) * swingArc + 180
        )
    )
)
/* Create a free camera targeted to the body */
cam = freeCamera pos:[100, -100, 50]
cam.target = body
/* Set the lower-right viewport as the active viewport */
viewport.activeViewport = 4
/* Associated the active viewport with the camera */
viewport.setCamera cam
/* Animate the camera around the origin while moving up. */
animate on (
    for t = 0 to 100 do (
        at time t (
            /* Rotate about the z_axis at the origin 
            Uses the "about" context expression to set the pivot to perform rotations.
            */
            about [0, 0, 0] ( rotate cam 1 z_axis )            
            /* Move the camera upwards */
            move cam [0, 0, 1]
            /* Update the roll_angle controller of the camera so that we stay level with the x/y plane */
            cam.transform.controller.Roll_Angle.controller.value = 0
        )
    )
)

Viewing all articles
Browse latest Browse all 73

Latest Images

Trending Articles



Latest Images