Notifications
Clear all

[Closed] Executing a script

MacroScript ModelExporter category:"Export"
   (
       -- open file dialog for saving
       output_name = getSaveFileName caption:"Save your file." types:"Speedsheet (*.ssh)|*.shh| All Files(*.*)|*.*|"
       
       if output_name != undefined then    -- if the user cancelled the file selection
       (
           output_file = create output_name  -- create file for writing, overwrite if already exists

           -- loop through each object in selection
           for iterator = 0 to selection.count do
           (
               format "Object name %s" selection[iterator].name to: output_file
           )
           close output_file
       )
   )
   

So I’ve written a pretty simple script that just writes some values out to a text file. How do I run it – in other words have it output that file.

Also, how do I create a menu bar for my MacroScript ( http://www.maxforums.org/thread.aspx?tid=415062 )? At the bottom of that page, the author says that he creates a button for his MacroScript and I have no idea how to do that. Does clicking on that button run the MacroScript (the problem that I mention above)?

1 Reply

Press Ctrl+E. This will evaluate the script AND create a copy of your source code in the folder \Usermacros of your installation (it might be under your local profile depending on OS and settings). So next time you launch Max, the code will be loaded automatically to ensure your script is still around. The file name will be something like Export-Model Exporter.mcr, based on the name and category you gave it.

Then you go to Customize>Customize User Interface, or right-click ANY of the toolbars and select Customize from there. In the Customize UI dialog, in the Toolbars tab, find the category of your script (Export) and drag the script (Model Exporter) to a toolbar. You can alternatively drag it to a Menu, QuadMenu or assign a keyboard shortcut using the other 3 tabs. Then remember to SAVE the user interface configuration to keep the changes around.

When you press the Model Explorer button (or select the menu/press the shortcut), your script will be run. NOTE that it has a bug – MAXScript indexing of arrays and collections is ONE-based, so iterating from 0 will cause an error in the first iteration.

MAXScript can also iterate through arrays and collections directly without a counter. So you can say


for theObject in selection do 
  format "Object Name = %
" theObject.name to:output_file

Here, the iterator will hold the actual selected object instead of an index.

NOTE that you don’t need %s since ANYTHING formatted out gets handled as string implicitly. But you need
for a new line otherwise all names will be in one line.