Using Dictionary data types in 3ds Max

loocas | maxscript,Python,software,technical | Thursday, April 28th, 2011

I’ve had a few customers and clients asked me specifically about getting Dictionaries from 3ds Max to Python using our 3ds Max Python plugin, but I wasn’t able to answer with an elegant or productive way of handling these data types in the MAXScript environment.

Until recently, I’ve been handling Dictionaries two ways:

  1. String parsing (i.e. a very primitive way of handling foreign data types, not really recommended)
  2. Manual wrapping (i.e. passing in a List or Array object and converting it back to a Dictionary object)

String parsing is the worst possible way of handling such an issue. It’s very cumbersome, highly unintuitive and with MAXScript string capabilities, extremely difficult. Manual wrapping, on the other hand, is rather more elegant, faster and you can use other, known, data types to construct your future Dictionary and have that converted in Python natively. The down side is, you have to be very careful with the way you’re handling the future Dictionary. The thing is, only a tuple of list pairs can be successfully converted to a Dictionary in Python. This is a bit limiting as we don’t have any specific way to tell what is a Tuple and what is a List in our Python implementation in MAXScript as there are no such data types available. So, what I did was I had an Array sent to Python (an actual .NET Array object, by the way) I had that converted first to a Tuple, which is pretty straight forward and then have that converted to a Dictionary. Worked fine, but it was a bit difficult to construct more complex Dictionaries, especially nested Dictionaries, in MAXScript.

Recently I was writing some .NET code and I bumped into Collections namespace. I then investigated further the Hashtable objects and realized I could substitute Dictionaries in MAXScript with Hashtables using .NET classes and objects! Obviously, again, it’s not as elegant as native Python, but it’s closer than manual wrapping of Lists and Arrays!

All you need to do is instantiate a Hashtable object in MAXScript:

HT = dotNetObject "System.Collections.Hashtable"

and then you can call the .Add() method for adding the Key Value pairs! Pretty cool! Also, you can access the Values or Keys similarly to Python’s Dictionaries:

HT.Add "Key" "Value"
HT.Item["Key"]

This is far more productive and intuitive than my previous methods!

Also, then you can pass the Hashtable object directly to IronPython and have it converted to the native Dictionary object using the dict() method, without any issues! Again, this is a much straight forward way of doing such a, rather, complex data type conversion.

I’m currently implementing this approach to my Shotgun wrapper I use with our Python plugin. All seems to be working perfectly fine!

3 Comments »

  1. I know this post is 3 years old ,but this morning it is exactly what I needed.
    I have to map one rig to another with different bone names, and this is perfect.
    thank you!

    Comment by Logan Bender — August 28, 2014 @ 15:57

  2. You’re most welcome, Logan. :)

    Comment by loocas — September 26, 2014 @ 01:27

  3. Just perfect for my needs. Thank you! :)

    Comment by Guilherme D'Amoreira — January 13, 2017 @ 17:44

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck