3 entries in Functions

“List” struct

Easily create associative array-like structures in 3dsmax

Friday, June 6th, 2008

As 3dsmax doesn’t allow for either associative arrays or dynamically-set object properties, it can be difficult to store unstructured, arbitrary variables.

Sometimes you just want to store lists of name/value pairs, to keep track of a few settings throughout your script, without relying on a host of variables, resorting to .ini files, or custom attributes.

Therefore, I set about writing a basic Dictionary, or List struct, similar to VB and other languages.

Example code

Here’s a really basic example of storing 5 named variables within a List:

names = #("five", "four", "three", "two", "one")
values = #(5,4,3,2,1)
lst = List()

for i = 1 to 5 do lst.addItem names[i] values[i]

So let’s get some data back out:

lst.getValue "two"
-- 2

lst.getIndex "two"
-- 4

lst.getValues()
-- #(5, 4, 3, 2, 1)

Or print the whole lot:

lst.print() -- unsorted

five:   5
four:   4
three:  3
two:    2
one:    1

How about some sorting:

lst.sort() -- by name, alphabetically

five:   5
four:   4
one:    1
three:  3
two:    2

lst.sort field:#value -- by value

one:    1
two:    2
three:  3
four:   4
five:   5

As you can see, it’s pretty straightforward stuff!

Properties and Methods

Properties

  • items – the key/value pairs.
    • Names can be a #name, “string”, or even an index
    • Values can be any MaxWrapper value (i.e. anything)

Setters

  • addItem – adds an item to the List, and if it already exists
  • setItem – synonym for the above

Getters

  • getValue – returns the value of the named item
  • getIndex – returns the index of the named item
  • getName – returns the name of the first item that matches the supplied value
  • getItem – returns the List item corresponding to the supplied name (typically, you wouldn’t use this, as you know the name component already, it’s just included for completeness)
  • getItems() – returns all items as an array of ListItem structs
  • getNames() – returns all names as an array
  • getValues() - returns all values as an array

Clear or delete

  • clear() – clears the lit of all items, and returns the empty ListItems array
  • deleteItem – deletes the named item from the list, and returns it
  • deleteIndex – deletes the item at the index, and returns it

Utilities

  • sort field: order: func: – sorts the list in a variety of ways, even supply a comparison function (see the max help on qsort)
  • print() – prints the List items to the Listener

Next version

Possible improvements in the next version might be:

  • .ini file integration, with save() and load() methods
  • Support for hierarchical Lists, perhaps getValue #(#parent, #child, #grandchild, …)

Download

Download List.struct.ms here.

Time Stamper

A struct to make light work of timing tasks, benchmarking, etc

Friday, May 9th, 2008

This struct basically makes timing things much easier by packaging a few useful methods together.

Now, instead of writing and retrieving timestamp variables, and formatting strings to the listener, you simply call struct methods such as start(), end(), and print(), or see a report of the process with getReport().

Example Code

Create a new Time Stamper, assigning a task name:

ts = timeStamper "Testing"

Time something, and alert the results in a messagebox:

ts.start()
-- your code here
ts.alert()

Benchmark a series of tests, and print the results to the listener when completed:

for i = 1 to 10 do (
        ts.start()
        -- your code here
        ts.end()
)
ts.print average:true
-- Average processing time for 'Testing' was 24.6162 seconds, based on 10 timed sessions.

Methods

Starting and stopping

  • start() – start timing. Returns start time
  • end() – end timing. Returns last timing duration
  • reset() – reset all timing data

Getting results in English

  • print average: difference: – end timing and print results to listener
  • prompt average: difference: – end timing and prompt results to notification area
  • alert average: difference: – end timing and alert the results in a messagebox

For the above methods, the following keyword arguments can be supplied:

  • average – returns the average result of all the timed tests since the TimeStamper was instantiated or last reset
  • difference – returns the comparative difference between another TimeStamper, in English, e.g.“Test 1′ was 2.58 times quicker than ‘Test 2′”

Getting results as numbers

  • getLast() – gets the last single timed session (alias for duration property)
  • getTotal() – gets the total of all timed sessions
  • getAverage() – gets the average of all timed sessions
  • getDifference difference: average: – Returns a 3-element array representing how much quicker one time stamper is than another.

The array’s elements are ordered like so:

  1. – how many times quicker the quickest Time Stamper was
  2. – the task name of the quicker time stamper
  3. – the task name of the slower time stamper

Getting results as an Excel-compatible report

  • getReport columns: step: output: – gets all timed sessions as a customizable report

The step property defines how many iterations to average/total values for, and defaults to 10.

The columns property can take any of the following name values:

  • #index – the numeric index of the row, e.g. 1, 2, 3
  • #step – the current step, e.g. 1, 11, 21
  • #stepaverage – the average of all measurements from the current step
  • #steptotal – the total of all the measurements from the current step
  • #slower – how much slower the current step was than the fastest step
  • #quicker – how much quicker the current step was than the slowest step
  • #total – the cumulative total from all measurements so far

The output property defines where the generate report will be output to. Values can be:

  • No value – the report will be returned as a string
  • “path/to/file.txt” – a file path. If the file exists it will be overwritten, if not it will be created
  • #window – a new script window
  • – a reference to a windowstream
  • – a reference to a stringstream

Download a report that was built using calls to getReport() in Excel 2007 or in Excel 2003

Properties

  • duration – the duration of the last timed session
  • durations – an array of all timed sessions
  • task – the name of the current timed task

Demo and case study

Check out a case study and download a demo script here:

Download

Download TimeStamper.ms.

Multimaterial From Folder

Create a set of multimaterials from a folder of textures

Sunday, July 29th, 2007

This MAXScript entry has not yet been completed…
(more…)