Maxscript image file handling

When you are working with larger numbers of files in folders you want to be clever about getting them. I used to have a lots of different functions all with their own identical or very similar file processing. Often I’d hardcode in extensions (bad) and sometimes I’d want to search for files with a keyword.

Old:

fn texturehandling texturefolder= (

  image_diff_files = #()
  image_type = ".tga"

  -- Get all the TGA diffuse files
  diff_files = textureFolder  + "*diff*" + image_type
  thefiles = getfiles diff_files	

  for f in thefiles do
  (
    append image_diff_files (uppercase(getfilenamefile f))
  )

  image_diff_files -- return this
)

It works, but it has several issues. It assumes TGA files only, and it’s looking for filenames with “diff” as part of the filename. What if I want to look for specular maps with arbitrary names saved at BMP?

Define this in some common script header (so that it’s easily to find and extend):

textureTypes = #("*.tga","*.png","*.bmp","*.tif")

Then have this useful function:

-- Function to return an array of supported image files in a folder
-- Takes an optional search string
fn returnImageFiles inputFolder optionalFilter:"" = (
  theImageFiles = #()

  if optionalfilter == unsupplied then optionalfilter = ""

  search_files = inputFolder + optionalfilter

  -- Get array of image files in a given folder
  for ft = 1 to textureTypes.count do
  (
    tempfilelist = getfiles (search_files + textureTypes[ft])
    join theImageFiles tempfilelist
  )
  theImageFiles -- return this.
)

Then call it with this:

image_all_files = returnImageFiles textureFolder optionalFilter:""
image_diff_files = returnImageFiles textureFolder optionalFilter:"*diff*"

There are a few minor issues that I should clean up

  • I could have used a better name than optionalFilter, but it does the job.
  • I don’t check that the texture folder name ends with a slash
  • It doesn’t handle recursive folders

By the way, optional parameters in maxscript are very useful, though a little tricky at first.

One Response to “Maxscript image file handling”

  1. Max Payne 3 – PC Review Says:

    [...] rsart – Rick Stirling, games artist ul.legalfooter li{ list-style:none; float:left; padding-right:20px; } .accept{ display:none; border: 1px solid #000; background:#000; color:#fff; border-radius:3px; -moz-border-radius:3px; -webkit-border-radius:3px; padding:5px; } [...]

Thoughts on this?