How To > Output Object Data To File |
MAXScript lets you create text files on disk and output custom data. The following example will create a macroScript that will output the speed of the current object selection on each frame and its average speed to a text file.
Text Files Creation and Output
We create a new macroScript called SpeedSheet that will appear in the "HowTo" category.
The script will function only if there is at least one object selected. We check the number of objects in the current scene selection and execute the code only if the number is higher than 0.
output_name = getSaveFileName caption:"SpeedSheet File" types:"SpeedSheet (*.ssh)|*.ssh|All Files (*.*)|*.*|"
Then, we present the user with a standard windows File Saving dialog box. The dialog title bar will show the custom caption string "SpeedSheet File". We specify the default file type as .SSH (SpeedSheet) file. If the user types in a file name, the extension specified in the dialog is being added automatically.
Standard Open and Save File Dialogs
If the user canceled the file selection in the Save File Name dialog, the output_name variable will contain undefined. In such a case, we will have to skip the whole output code. If the file name is valid, we can go on.
Then, we create a new file for output using the specified file name. If the file already exists, it will be overwritten.
Right after creating the file, we write out the names of the selected objects. The at time context ensures that position data of the objects is printed out for the first frame in the animation. The format method uses a formatting string where variables are denoted with %. "\n" forces a new line.
To calculate the average speed, we initialize a variable to store all speed values for all frames.
Now, we go through all frames in the current animation segment. The variable t will contain a frame value which will change from the first to the last frame of the scene animation range.
AnimationRange_Time_Configuration
Using the current time from the for loop variable t , we read the position of the center of the object selection.
Then, we read the position of the selection center one frame before the current frame.
The frame speed on the current frame is equal to the distance traveled by the selection center since the last frame, multiplied by the frame number in a second. This gives us a speed in Generic Units per second.
Mid Topic ID: FrameRate_Time_Configuration
The average speed variable collects all frame speed values. Note that the same can be expressed in the full form as average_speed = average_speed + frame_speed .
The frame speed is written together with the frame number to the output file.
After the loop is over, the average speed can be calculated by dividing the sum of all frame speed values through the number of frames. The same can be written as average_speed = average_speed / (animationrange.end-animationrange.start) .
AnimationRange_Time_Configuration
The result is printed out to the output file.
The file is ready. We must close it before we can open it for reading.
Using the edit method, we open the file in a MAXScript editor window.
Evaluate the script and customize a toolbar by dragging the SpeedSheet button from the "HowTo" category. You can create any number of objects and animate their position. While the objects are selected, press the SpeedSheet button, specify the name of the output file and the result appears in a second.
If you want to see the content of the text file in an external editor, for example in Windows Notepad, you can replace the line,
which will launch the external application notepad.exe with the file name of the file as parameter.
You might want to improve the script by calculating real-world units (inches, feet, meters, and so on) instead of generic units.