{"id":1392,"date":"2008-11-08T22:46:33","date_gmt":"2008-11-09T04:46:33","guid":{"rendered":"http:\/\/www.mrbluesummers.com\/wp\/?page_id=2"},"modified":"2011-12-02T07:24:22","modified_gmt":"2011-12-02T07:24:22","slug":"c-sharp-to-maxscript","status":"publish","type":"post","link":"http:\/\/www.mrbluesummers.com\/1392\/3d-tutorials\/c-sharp-to-maxscript","title":{"rendered":"C# to Maxscript"},"content":{"rendered":"

I figured I\u2019d do a quick tutorial about something a little more difficult, but still very important. I\u2019m going to take you step-by-step through integrating a maxscript document and a C# class library so that you can access the powerful and robust features of the dotnet framework in the rather limited environment that maxscript provides. This lets you do such powerful things as access databases, grab web-deployed content, and more. It is my opinion that dotnet connectivity is the best thing to ever happen to maxscript.<\/p>\n

This tutorial is written for C# users. However, the themes here apply to any language that is part of the common language runtime (CLR) framework. If you chose to use C++, VB, etc. you should still be able to compile a class library that can integrate in much the same way.<\/p>\n

Step 1: Make a new class library project in Visual Studio.<\/h1>\n
\"New<\/a><\/p>\n

New class library creation.<\/p>\n<\/div>\n

So from the startup screen in visual studio, go ahead and go to File > New > Project.<\/p>\n

Select a C# class library. I\u2019m going to name mine \u201cMaxscript Sandbox\u201d.<\/p>\n

\"Name<\/a><\/p>\n

Name your library.<\/p>\n<\/div>\n

Step 2: Write a test class.<\/h1>\n

Now let\u2019s write a test class for our new class library. In this screenshot I\u2019ve written a very simple class that contains a string you pass on creation. Once the object exists, you can access the name, or you can call a function that provides you with a sentence that includes the name. Simple enough!<\/p>\n

Rename the class to TestObject, and fill in the code you see below.<\/p>\n

public class TestObject\r\n{\r\n    string Name;\r\n    public TestObject(string thisName)\r\n    {\r\n        Name = thisName;\r\n    }\r\n\r\n    public string GetLongName()\r\n    {\r\n        return (\"You can call me \" + Name + \"!\");\r\n    }\r\n}<\/pre>\n
\"Step<\/a><\/p>\n

Insert some placeholder code.<\/p>\n<\/div>\n

Step 3: Compile.<\/h1>\n

From the drop-down along the top bar, change the build mode from Debug to Release. This means that the compiler will optimize the code for fast execution rather than for debugging and trying to find out what the problem is. On that note, I think there exists a way to debug the class from your script. However, it\u2019s outside the scope of this tutorial. If you\u2019re burning to debug, make a console app that wraps your class and tests functionality from there.<\/p>\n

\"Finished<\/a><\/p>\n

Change to "Release" compilation.<\/p>\n<\/div>\n

Now, hit F6 or go to Build > Build Solution.<\/p>\n

\"Build<\/a><\/p>\n

Build the solution to a DLL.<\/p>\n<\/div>\n

Step 4: Call the new object from Maxscript.<\/h1>\n

I\u2019ve commented a lot of the code in the next image, but it\u2019s actually a lot shorter than it looks. All you have to do is load the new class library you\u2019ve created, instantiate the object, and then do whatever you want with it.<\/p>\n

Notice that I\u2019ve moved the .dll file that resulted from building my class library to somewhere shorter. Normally, your class library would compile to a directory within the project folder you selected when you created the project (like C:\/\u2026\/Maxscript Sandbox\/ Maxscript Sandbox\/bin\/release\/Maxscript Sandbox.dll). I\u2019ve moved that file from that incredibly long directory to something more digestable like C:\/Temp\/Maxscript Sandbox.dll.<\/p>\n

\"Call<\/a><\/p>\n

Call the library from Maxscript.<\/p>\n<\/div>\n

dotnet.loadAssembly (\"C:\\Temp\\Maxscript Sandbox.dll\")\r\nThisTestObject = dotNetObject \"Maxscript_Sandbox.TestObject\" \"Burt\"\r\nprint (ThisTestObject.GetLongName())<\/pre>\n

When you run this program, and the dll is in the right place, you\u2019ll find that it outputs as though the object was a native max object. However it\u2019s actually running in the dotnet framework. You can read more about creating objects and the syntax surrounding the Maxscript\/C# connection in the documentation. This tutorial was just meant to clarify some of the logistical issues surrounding getting it working.<\/p>\n

\"The<\/a><\/p>\n

The final output from the script.<\/p>\n<\/div>\n

Until next time, happy scripting!<\/p>\n","protected":false},"excerpt":{"rendered":"

I figured I\u2019d do a quick tutorial about something a little more difficult, but still very important. I\u2019m going to take you step-by-step through integrating a maxscript document and a C# class library so that you can access the powerful and robust features of the dotnet framework in the rather limited environment that maxscript provides. This lets you do such powerful things as access databases, grab web-deployed content, and more. It is my opinion that dotnet connectivity is the best thing to ever happen to maxscript.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,16],"tags":[35,44,2565,88],"_links":{"self":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts\/1392"}],"collection":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/comments?post=1392"}],"version-history":[{"count":0,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts\/1392\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/media?parent=1392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/categories?post=1392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/tags?post=1392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}