|
Mastering
3D Studio MAX R3 |
You can save MAX files using SAVEMAXFILE; for this command it is necessary
to specify both the filename (with extension) and path. If you want to
know the current filename and path, you can use MAXFILENAME and MAXFILEPATH.
For instance, to save the current file in the current folder you should
use savemaxfile (maxfilepath + maxfilename).
Partial Loading and Saving
You can save a file partially using Save Selected. To do so in MAXScript,
you can use SAVENODES. To use it you need to specify the objects you want
to save, using an array of objects or a built-in array, like selection,
objects, lights, etc. For instance, you can save the active selection
using savenodes selection saved_selection.max.
You can also merge files, loading objects from other scenes. You can
do it in two different ways, by pointing at the objects you want to merge
or by calling up the Merge Objects dialog box. You also have options that
will define how the merge will work, either by deleting old duplicated
objects, by ignoring duplicates, or by displaying a dialog window asking
what to do when duplicates are found.
To merge files use MERGEMAXFILE. For instance, to merge the file you
just saved with SAVENODES, use mergemaxfile saved_selection.max
#prompt. It will display the Merge Objects dialog box, and the
script will continue to run after the Merge ends. If you want the old
duplicated objects to be deleted, use #deleteolddups. If
you want to ignore them, use #mergedups. And if you want
the user to decide, using a dialog box, use #promptdups.
You can specify which objects are to be merged. To do so, you would need
to know which objects are present in the scene you want to merge. This
is done using GETMAXFILEOBJECTNAMES. It will return an array containing
all objects in the file, so you can use it with the MERGEMAXFILE command.
For instance, if you want to merge the arm object in the
creature.max file, deleting the old one if it exists, you
would use
mergemaxfile “creature.max” #(#arm) #deleteolddups
Importing and Exporting Files
To import and export files you can use IMPORTFILE and EXPORTFILE. The
exporter will be defined by the extension of the file. By default the
Exporter Options dialog window will be displayed, but you can skip this
using #noprompt, and the exporter will use default options.
For instance, to export the current scene as an STL file, you would use
exportfile exported.stl. To import it back,
use importfile exported.stl #noprompt, skipping
the Import Options dialog window.
Other File
Commands
You can hold and fetch files through MAXScript, using HOLDMAXFILE and
FETCHMAXFILE. No parameters are needed for these commands.
You can also reset MAX using MAXScript. This is done with the RESETMAXFILE
command. You can also skip the dialog boxes that would appear using #noprompt.
|
|
| WARNING Use
resetmaxfile #noprompt with caution. If your MAX file
has not been saved, all changes will be lost.
|
Sometimes you might need to know whether a file has been saved or not.
You can use CHECKFORSAVE() to do so. It will return true if the file has
been saved and false if changes were made and not saved.
You can also quit MAX through scripts with the command QUITMAX. This
is useful if you are batch processing files for render, import, etc. Like
RESETMAXFILE, QUITMAX also has a #noprompt option.
Hands-on MAXScript: Batch Converting 3DS Files
Well create a script that converts all 3DS models from a selected
folder into MAX files. It is a Macro Script with a rollout and a button,
which will ask the user to select a folder containing the 3DS files, as
seen in Figure 17.2.
FIGURE
17.2 Batch 3DS Convert script
Then, the script will search and list the number of files found. If it
finds any 3DS files, the Convert button will be enabled, allowing the
file conversion. Either open the file convert_3ds.mcr from
the CD that accompanies this book, or start a new script and type everything
shown in Listing 17.2.
LISTING 17.2: Batch Converting 3DS Macro Script (convert_3d.mcr)
macroScript Convert_3DS
category:“Tools”
tooltip:“Convert 3DS Files to MAX”
(
rollout convert_3ds_params “Parameters”
(
local f,f_name, f_ext, f_path
button folder “Select Directory” width:120
label status “0 files found”
button go “Convert!” width:120 enabled:false
on folder pressed do
(
f_path = getsavepath caption:“Select 3DS Directory”
f = getfiles (f_path + “\\*.3ds”)
status.text = (f.count as string + “ files found”)
if f.count > 0 do go.enabled = true
)
on go pressed do
(
resetmaxfile()
for i in 1 to f.count do
(
resetmaxfile #noprompt
importfile f[i] #noprompt
f_name = getfilenamefile f[i]
f_path = getfilenamepath f[i]
savemaxfile (f_path + f_name + “.max”)
)
go.enabled = false
)
)
global convert_3ds_floater
try(closerolloutfloater convert_3ds_floater) catch()
convert_3ds_floater = newrolloutfloater “Batch 3DS Convert” 200 140
addrollout convert_3ds_params convert_3ds_floater
)
Evaluate the script and create a button in any toolbar for it. Test the
script, asking it to convert a series of 3DS files in any folder. (If
you do not have any 3DS files, export some from your scenes.)
Now you will see what is new in this script. Youve used a couple
of new commands, some which were not discussed earlier. Among those, you
can list GETSAVEPATH. It displays the Browse for Folder dialog box (illustrated
in Figure 17.3) and returns the selected folder path as a variable.
FIGURE
17.3 Browse for Folder dialog box
Right after that command, you have GETFILES, which searches for files
and lists all of them as an array. This allows us to search and know whether
there are files in the selected folder.
In the next action, you used GETFILENAMEFILE and GETFILENAMEPATH. These
commands filter a filename and display only the name (without the extension)
and only the path, respectively. These are used when you need to specify
a different filename extension, or to change filenames or paths.
Try variations of this script, using radio buttons to select the converted
file type. You can also select another folder for the output files. You
can also play with GETDIRECTORIES, which is a command that will list all
folders inside the selected folder, and make your script convert all 3DS
files in a directory structure.
Xref
You can also Xref scenes and objects using MAXScript. You will be able
to load, unload, and merge Xrefs. You can even access properties in an
Xref scene.
Xref Object
To load an Xref object, you can use XREFS.ADDNEWXREFOBJECT. It will require
two parameters, the filename and the object name.
The Xref object has several properties. For instance, you can select
a proxy object and file to the Xref objects using .proxyfilename
and .proxyobjectname. By default, Use Proxy is off unless
you enable it with .useproxy.
For instance, if you have a object named Arm in a scene named Creature
and you wanted to Xref that object, youd use p_arm = xrefs.addnewxrefobject
creature .max arm. If you wanted to use
a proxy object Box in the same file, you could type:
p_arm.proxyfilename = “Creature.max”
p_arm.proxyobjectname = “Box”
p_arm.useproxy = true
© 2000, Frol (selection,
edition, publication)
|