ToC
ToC

Reputation: 1

Repeating the same task with multiple objects in a folder

I don't know how to write maxscript how to call multiple objects in a folder one by one, move them to the specified coordinates, and save them in the desired folder again. I would appreciate it if you could tell me how.

Upvotes: 0

Views: 454

Answers (1)

JonahHawk
JonahHawk

Reputation: 1

You will need an array (list) of file paths to each .max file. You can type this manually in your script or generate this list with a maxscript function as below.

Manually type a list like this:

theFile = #("c:\filePath\filename1.max", "c:\filePath\filename2.max", etc)

You can use this function to find all file types in a given folder:

fn getFilesRecursive root pattern =
    (
        dir_array = GetDirectories (root+"/*")
        for d in dir_array do
        (
            join dir_array (GetDirectories (d+"/*"))
        )
        
        append dir_array (root + "/")       
        theFileList = #()
        for f in dir_array do
        (
            join theFileList (getFiles (f + pattern))
        )
        
        theFileList 
    )

The getFilesRecursive function takes two variables as input; Root and Pattern. It will search through all subfolders in the Root folder you give it. So elsewhere in the script you would run the function like this:

theFiles = getFilesRecursive @"c:\path\to\yourFolder\"  "*.max"

This stores the array of .max files in the variable, theFiles. Make sure to include the @ symbol to make the path literal or the backslashes will cause problems.

Now that you have a list of file, you can loop through them with the loadMaxFile and saveMaxFile functions with your code for moving the objects in between.

for f in theFiles do
    (
        loadMaxFile f useFileUnits:false quiet:true
        -- move the objects to 0,0,0
        for obj in objects where classof obj == geometryclass do (
            obj.position = [0,0,0]
        )
        saveMaxFile f clearNeedSaveFlag:true quiet:true saveAsVersion:2020
    )

Look at the Maxscript help docs for the functions, loadMaxFile and saveMaxFile to get a sense of the options. In my code above, I am telling Max to ignore the units in the file so that it does not switch my system units in this process. Also, it sets Quiet Mode to true so that it tries to suppress any dialogs that might pop up in the process of opening the files. The saveAsVersion gives you an opportunity to save back to a previous version of Max if you work in a mixed version environment.

Upvotes: 0

Related Questions