.NET Recipes

From Tech Artists Wiki

Jump to: navigation, search

These are some common .NET things you can do with MAXScript. Please contribute!


Get temporary filename

Use this to create a temporary filename. This is the preferred way of creating temporary files, as it will make sure you are not overwriting anything and make sure you can actually write there (which isn't guaranteed for new Windows OSes, where writing to places like C:\\ can fail).

((DotNetClass "System.IO.Path").getTempFilename())

Download a file

Download a file from a URL. System.Net.WebClient also has other useful methods if your web interaction needs to be more involved. See osOps.download in the TAO MAXScript library.

url = "http://tech-artists.org/w/images/5/5e/Normalmap_objectspace.jpg"
destination = (dotNetClass "System.IO.Path").getTempFilename() --see "Get a temporary filename" recipe
(DotNetClass "System.Net.WebClient").downloadFile (dotNetObject "System.String" url) (dotNetObject "System.String" destination)

Extract a zipped file

You need to download the DotNetZip library from CodePlex to use this method. This function is included in the TAO MAXScript library.

fn extract zipfilename targetDir =
	local zip = (dotNetClass "Ionic.Utils.Zip.ZipFile").Read(dotnetobject "system.string" zipfilename)
	for i = 0 to zip.count - 1 do
		local z = zip.item[i]
		z.extract (dotNetObject "System.String" targetDir)

Remove a directory

You could, of course, use the shell command RMDIR instead, but this is a .NET way of doing things. This function is included in the TAO MAXScript library.

fn rmDir dir recurse:true =
		(dotNetClass "system.IO.directory").delete dir recurse
		format "Runtime error in rmDir: The directory % is not empty.\n" dir
	if doesFileExist dir then	--if it does exist, we've failed.

.NET messageboxes

.NET messageboxes give you much more functionality and flexibility than MXS messageboxes or queryboxes. There are good examples on the MSDN site as well.

msg = DotNetClass "System.Windows.Forms.MessageBox"
buttons = DotNetClass "System.Windows.Forms.MessageBoxButtons"
icons = DotNetClass "System.Windows.Forms.MessageBoxIcon"
result = msg.show "Isn't using .NET awesome?" "Confirm" buttons.YesNo icons.Question
if result == (DotNetClass "System.Windows.Forms.DialogResult").No then
Personal tools