I made a post on my site about how to expose the .NET hashtable collection using MaxScript. This is purely informational and thought it might be helpful, so I’m cross posting it here.
I recently wanted to see if I could expose the .NET Hashtable collection to MaxScript. Well, you can with the DotNet exposure in MaxScript.
It was actually pretty straightforward. Unfortunately, the problem with using the DotNet version is that it requires both the hashtable keys and values be strings. So it may not be suitable for many situations. I suppose you could execute the string to turn it back into whatever object it was when it was added to the hashtable, but that’s generally a no-no practice in MaxScript.
In any case, here is the code:
struct mxsHashTable
(
_hashTable = dotNetObject "System.Collections.HashTable",
fn add key value = (
_hashTable.Add(dotNetObject "System.String" (key as string)) (dotNetObject "System.String" (value as string))
),
fn clear = (
_hashTable.Clear()
),
fn containsKey key = (
_hashTable.ContainsKey (key as string)
),
fn containsValue value = (
_hashTable.ContainsValue (value as string)
),
fn remove key = (
_hashTable.Remove (key as string)
),
fn lookup key = (
_hashTable.item[(dotNetObject "System.String" (key as string))]
),
fn modify key value = (
remove key
add key value
),
fn size = (
_hashTable.count
)
)
Here are some examples of how to use it:
hash = mxsHashTable()
hash.add "blue" "pants"
hash.add "red" "pants"
if hash.containsKey "blue" then (
print "True"
val = hash.lookup "blue"
print val
hash.modify "blue" "shorts"
val = hash.lookup "blue"
print val
size = hash.size()
print size
val = hash.containsValue "socks"
print val
) else (
print "False"
)
Executing the above code will produce the following results (taken from the MaxScript Listener window):
"True"
"pants"
"shorts"
2
false
false