How To ... Create a Quick Preview

How To > Create a Quick Preview

The following macroScript will implement a highly simplified version of the Preview Animation feature of 3ds Max. We will access the content of the viewport directly and build an AVI file which will be then automatically loaded in the RAM player.

Related Topics:

Defining MacroScripts

Accessing Active Viewport Info, Type, and Transforms

Bitmap Values

Device_Independent_Bitmap_access

RAMPlayer

NATURAL LANGUAGE

Package the code as macroScript to be able to use as a button, menu item or shortcut.

Define an AVI output path name in the Previews system directory.

Get the size of the current viewport.

Create a bitmap with the size of the viewport and set the file name to the output path .

Loop through all animation frames in the current segment.

Set the slider to the time from the loop.

Capture the viewport Device Independent Bitmap.

Copy the DIB to the pre-defined bitmap.

Save the current frame to disk.

When ready with all frames, close the bitmap.

Force garbage collection manually to clear any used memory.

Load the ready animation in the RAM player.

MAXSCRIPT

macroScript QuickPreview category: "HowTo"
(
 preview_name = (getDir #preview)+"/quickpreview.avi"
 view_size = getViewSize()
 anim_bmp = bitmap view_size.x view_size.y filename:preview_name
 for t = animationrange.start to animationrange.end do
 (
  sliderTime = t
  dib = gw.getViewportDib()
  copy dib anim_bmp
  save anim_bmp
 )
 close anim_bmp
 gc()
 ramplayer preview_name ""
)

Step-By-Step

macroScript QuickPreview category:"HowTo"
(

The macroScript will be called QuickPreview . To use the script, you can go to Customize... and drag the script from the category "HowTo" to a toolbar, a menu, a quad menu or assign to a keyboard shortcut.

Defining Macro Scripts

preview_name = (getDir #preview)+"/quickpreview.avi"

Now we define the output path for the preview AVI file. Using the getDir function we access the current preview path specified in 3ds Max’ Customize>Configure Paths. We add the file name to the path separated with a slash. (we could also use double-backslash "\\" as directory delimiter). The resulting path will be stored in a user variable called preview_name.

3ds MaxSystem Directories

view_size = getViewSize()

Next we check the size of the currently active viewport. The result of the getViewSize function is a Point2 value containing the width and height of the viewport in pixels. We will store this value in another user variable called view_size .

Accessing Active Viewport Info, Type, and Transforms

anim_bmp = bitmap view_size.x view_size.y filename:preview_name

Using these two user variables, we can define a bitmap to store the preview. We supply the x and y component of the viewport size as the width and height parameters of the bitmap constructor, and set the filename to the already defined preview path.

Bitmap Values

for t = animationrange.start to animationrange.end do
(

A for loop will iterate through all frames in the current animation segment. The variable t will contain the frame number of each frame, starting with the start value of the animation range interval and ending with the end value.

For Loop

animationRange_System_Global

sliderTime = t

Using the t variable, we move the time slider from frame to frame.

3ds MaxSystem Globals

dib = gw.getViewportDib()

On each frame, we capture the viewport bitmap (Device Independent Bitmap, DIB) in a temporary variable.

Device_Independent_Bitmap_access

copy dib anim_bmp

Then we copy the content of the dib variable (which is the bitmap showing the viewport’s content) into the preview bitmap we created. This adds a new frame to the AVI preview animation.

Bitmap Values

save anim_bmp

Before repeating the same on the next frame, we save the current content of the bitmap to disk.

Bitmap Values

)--end t loop
close anim_bmp

Bitmap Values

gc()

When all frames have been stored, we can close the animation file and manually force garbage collection to ensure the memory used while copying bitmaps around has been released.

In 3ds Max 5, you can use gc light:true to free up memory.

Manual Garbage Collection

ramplayer preview_name ""

Finally, we call the RAM Player and provide the path of the preview animation as first parameter, and an empty string as the second. The two parameters tell the RAM player which two files to load. The empty string is a place holder for a non-existing animation – we want to preview just one file.

RAMPlayer

)

Using the Script

Evaluate the script. To use it, you can use Customize... to drag the script from the category "HowTo" to a toolbar, a menu, a quad menu or to assign to a keyboard shortcut.

Select the viewport you want to preview (you can drag the viewport division to make it smaller for faster results) and start the script. The animation will be played once, then the RAM Player should open and load the preview animation.

Back to

"How To" Tutorials Index Page