dotNet Struct Methods

The dotNet Struct provides additional MAXScript methods for managing dotNet objects, classes, assemblies, event handlers etc.

Available in 3ds Max 9 and higher.

Assembly Loading:

<dotNetObject>dotNet.loadAssembly <assembly>

This method loads the specified assembly. The assembly can be specified as an assembly name or the filename of the dll containing the assembly.

The method first attempts to load an assembly from the application directory or from the global assembly cache using the assembly name. If that fails, a dll with the assembly name is searched for in the directory containing the primary .net assemblies.

If the assembly is found and successfully loaded, a dotNetObject value wrapping the .net assembly value is returned, otherwise a value of undefined is returned.

FOR EXAMPLE

dotnet.loadAssembly "system.xml"
dotnet.loadAssembly "system.xml.dll"
dotnet.loadAssembly "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\system.xml.dll"
dotnet.loadAssembly "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\system.xml"
NOTE:

You can do the same via existing dotNet classes:

FOR EXAMPLE

assembly = dotNetClass "System.Reflection.Assembly" r = assembly.loadfrom "F:\\FlashTrackBar\\bin\\Debug\\FlashTrackBar.dll"

Event Handlers:

dotNet.addEventHandler <dotNetControl> <EventNameString> <function>

This method adds an event handler to the dotNetControl passed as first argument.

The second argument specifies the event nameas string. The list of all available events for a given dotNetControl can be acquired by calling showEvents() on the dotNetControl value.

The third argument is the custom MAXScript function to be called when the event is triggered.

If the custom MAXScript function called by the handler expects two arguments, the first argument will contain the sender, the second the eventArg.

If the event handler function specifies only one argument, then only the eventArg will be passed.

If the event handler function doesn't specify any arguments, none will be passed.

See the example further on this page for details.

NOTE:You can register multiple functions with differing number of arguments for the same event using dotNet.addEventHandler() .
dotNet.removeAllEventHandlers <dotNetControl>

Removes all event handlers from the specified dotNetControl.

dotNet.removeEventHandler <dotNetControl> <EventNameString> <function>

Removes the specified event handler function from the given dotNetControl and event.

The arguments list is the same as with dotNet.addEventHandler() .

The callback function has to be specified as third argument because there can be multiple functions registered with the same event (see note above).

To remove all functions registered with a given event, see the following method:

dotNet.removeEventHandlers <dotNetControl> <EventNameString>

Removes all functions registered with the specified event of the dotNetControl.

EXAMPLE

(
fn buttonPressed1 a1 a2 =
(
print "1. Button Pressed - Two Arguments"
print a1
print a2
)
fn buttonPressed2 a1 =
(
print "2. Button Pressed - One Argument"
print a1
)
fn buttonPressed3 =
(
print "3. Button Pressed - No Arguments"
)
fn mouseEnter4 =
(
print "4. Mouse Entered The Button!"
)
fn mouseLeave5 =
(
print "5. Mouse Left The Button!"
)
local mButton = dotNetObject "System.windows.forms.button"
mButton.text = "Press Me"
mButton.size = dotNetObject "System.Drawing.Size" 100 100
mButton.location = dotNetObject "System.Drawing.Point" 10 10
local hForm = dotNetObject "System.Windows.Forms.Form"
hForm.size = dotNetObject "System.Drawing.Size" 120 140
hForm.controls.add mButton
dotNet.addEventHandler mButton "click" buttonPressed1
dotNet.addEventHandler mButton "click" buttonPressed2
dotNet.addEventHandler mButton "click" buttonPressed3
dotNet.addEventHandler mButton "MouseEnter" mouseEnter4
dotNet.addEventHandler mButton "MouseLeave" mouseLeave5
hForm.show()
ok
)

If you would evaluate the above script, them move the mouse over the button, click it, then move the mouse away from the button, then back in and out, the Listener output will look like this:

OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"5. Mouse Left The Button!"
"4. Mouse Entered The Button!"
"5. Mouse Left The Button!"

Adding the following line to the first (buttonPressed1) function will cause the first on Click event handler to be removed after the first click on the button! Clicking a second time will cause only the second and third functions to print to the Listener.

Within the function, the a1 argument contains the "sender", or the dotNetControl whose event was triggered.

dotNet.removeEventHandlers a1 "click" buttonPressed1
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"5. Mouse Left The Button!"
--additional clicks result in no output from function 1 --but all others work 

Adding the following line to the first (buttonPressed1) function will cause all on Click event handlers to be removed after the first click on the button!

Clicking a second time will not result in anything printed to the Listener, but when the mouse moves in and out the button, the other two functions will print.

dotNet.removeEventHandlers a1 "click"
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"5. Mouse Left The Button!"
"4. Mouse Entered The Button!"
"5. Mouse Left The Button!"
--additional clicks result in no output --but moving the mouse prints to the Listener 

Adding the following line to the first (buttonPressed1) function will cause ALL event handlers to be removed after the first click on the button, including the on Click, on MouseEnter and on MouseLeave events.

dotNet.removeAllEventHandlers a1
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
--additional clicks result in no output 

Additional Inspector Functions:

dotNet.showConstructors [<dotNetClass> | <dotNetObject> | <dotNetControl>] [to:<stream>]

Displays the constructors exposed by the specified DotNetClass, DotNetObject or DotNetControl.

If to is not specified, the output goes to Listerner, otherwise the output goes to the specified stream.

dotNet.getType<type_string>

Returns a dotNetObject value that wraps the System:Type specified by the type string.

Enums:

dotnet.combineEnums {<dotNetObject> | <number>}+

Combines System::Enum-derived objects that are to be treated as bit fields (these have a FlagsAttribute attribute). The System::Enum-derived objects must all be of the same type. The return value is of that type. If a number is specified, at least one System::Enum-derived object must also be specified (to provide the type).

dotnet.CompareEnums{<dotNetObject> | <number>} {<dotNetObject> | <number>}

Performs bit-wise comparison of the two value. Returns true if at least one corresponding bit is set in each value.

EXAMPLE

BindingFlags= dotNetClass "System.Reflection.BindingFlags"
dotNetClass:System.Reflection.BindingFlags
BindingFlags.Static.value__
8
BindingFlags.Public.value__
16
BindingFlags.FlattenHierarchy.value__
64
res = dotNet.combineEnums BindingFlags.Static BindingFlags.Public 2dotNetObject:System.Reflection.BindingFlags
res.value__
26
dotNet.compareEnums res 1
false
dotNet.compareEnums res BindingFlags.Static
true
dotNet.compareEnums res BindingFlags.Public
true
dotNet.compareEnums res BindingFlags.FlattenHierarchy
false

Value Conversion:

dotNet.ValueToDotNetObject <value> { <dotNetClass> | <dotNetObject> }

See MAXScript / DotNet Value Conversions for details.

Available in 3ds Max 2010 and higher.

Life Time Control:

dotNet.setLifetimeControl {<dotNetObject> | <dotNetClass>} {#mxs | #dotnet}

See DotNet Objects and Classes Life Time Control details.

Available in 3ds Max 2010 and higher.

See Also