I figured I’d do a quick tutorial about something a little more difficult, but still very important. I’m 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.

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.

Step 1: Make a new class library project in Visual Studio.

New class library creation.

So from the startup screen in visual studio, go ahead and go to File > New > Project.

Select a C# class library. I’m going to name mine “Maxscript Sandbox”.

Name your library.

Step 2: Write a test class.

Now let’s write a test class for our new class library. In this screenshot I’ve 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!

Rename the class to TestObject, and fill in the code you see below.

public class TestObject
{
string Name;
public TestObject(string thisName)
{
Name = thisName;
} public string GetLongName()
{
return ("You can call me " + Name + "!");
}
}

Insert some placeholder code.

Step 3: Compile.

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’s outside the scope of this tutorial. If you’re burning to debug, make a console app that wraps your class and tests functionality from there.

Change to "Release" compilation.

Now, hit F6 or go to Build > Build Solution.

Build the solution to a DLL.

Step 4: Call the new object from Maxscript.

I’ve commented a lot of the code in the next image, but it’s actually a lot shorter than it looks. All you have to do is load the new class library you’ve created, instantiate the object, and then do whatever you want with it.

Notice that I’ve 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:/…/Maxscript Sandbox/ Maxscript Sandbox/bin/release/Maxscript Sandbox.dll). I’ve moved that file from that incredibly long directory to something more digestable like C:/Temp/Maxscript Sandbox.dll.

Call the library from Maxscript.

dotnet.loadAssembly ("C:\Temp\Maxscript Sandbox.dll")
ThisTestObject = dotNetObject "Maxscript_Sandbox.TestObject" "Burt"
print (ThisTestObject.GetLongName())

When you run this program, and the dll is in the right place, you’ll find that it outputs as though the object was a native max object. However it’s 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.

The final output from the script.

Until next time, happy scripting!

C# to Maxscript的更多相关文章

  1. 【转】MaxScript.Net接收本地端口的消息执行

    MaxScript里开不了线程,但是可以用.Net的BackgroundWorker来做后台处理 BackgroundWorker Fn BackgroundTcpListenerDoWork the ...

  2. MaxScript重启3dsMax的重新思考

    前天看到一位大神写用MaxScript实现重启3dsMax的方法,用的是.net临时编译一个exe出来,然后用这个新的进程来关闭并开启新的max.感觉这种思路不错,或许可以用在别的地方.不过谈及max ...

  3. 关于 MAXScript 拷贝文件夹及内容到其他位置

    之前用 hiddenDOSCommand 本机测试通过,但是换其他电脑有时会不能用... fn xcopy oldfile newfile = ( newfilepath = newfile + &q ...

  4. 关于如何获取/清除 MAXScript 侦听器内的文本

    关于如何获取/清除 MAXScript 侦听器内的文本 用来保存记录?还没想到实际用处,先记上. macroRecorder as string listener as stringclearList ...

  5. 关于 MAXScript 中文路径返回上级目录(精简版)

    之前写过一个 关于 MAXScript 中文路径返回上级目录 的博文 今天无意中发现了一个更简单的方法 代码如下: fn newfile filepath = ( nf = getfilenamepa ...

  6. 关于 MAXScript 如何剪切文件夹

    MAXScript 中可以对文件进行创建删除复制等操作但是唯独不能删除文件夹... 网上搜了一下批处理的剪切方法,在 MAXScript 里调用一下就好了 fn xcopy oldfile newfi ...

  7. 关于 MAXScript 逐行写入文本

    官方帮助文档FileStream Values部分有相关介绍. fn format_txt filepath filetext = ( if doesFileExist filepath == tru ...

  8. 关于 MAXScript 获取全部文件

    MAXScript 官方文档里关于获取文件夹下所有文件的方法 fn getFilesRecursive root pattern = ( dir_array = GetDirectories (roo ...

  9. maxscript, 批量导出物体

    1,将场景中所有选中物体整体导出为一个fbx文件 exportfile filename #noprompt selectedOnly:true using:FBXEXP 2,将场景中所有选中物体各导 ...

  10. maxscript, 数组和字符串下标是从1开始的

    maxscript中数组和字符串下标是从1开始的.

随机推荐

  1. 基础数据结构 之 栈(python实现)

    栈是编程开发中的两种较为简单的数据结构.栈和队可用于模拟函数的递归.栈的特点是后进先出.其常用操作包括:出栈,入栈等.在出栈前,需判断栈是否为空.在入栈时,需判断栈是否已满. 下面给出一个用pytho ...

  2. Android Activity切换动画overridePendingTransition

    Activity在切换或者是退出的时候能够使用渐入,滑动,缩放等动态效果.使用的就是方法overridePendingTransition,能够直在Activity其中直接调用. overridePe ...

  3. 关于port的关闭——Linux

    本文出自:http://blog.csdn.net/svitter 引文出自:http://bbs.chinaunix.net/thread-775649-1-1.html 1.关闭服务 servic ...

  4. C++11 新特性之 Lambda表达式

    lambda表达式能够用于创建并定义匿名的函数对象,以简化编程工作 Lambda的语法例如以下: [函数对象參数](操作符重载函数參数)->返回值类型{函数体} []内的參数指的是Lambda表 ...

  5. c语言_判断例子

    例一: #include "stdio.h" int main() { ; if(i) printf("hi"); if(!i) printf("hi ...

  6. JavaWeb中登陆功能

    首先我们要JavaWeb登陆的基本流程:JSP页面发送请求-->Servlet-->Servlet通过调用方法从数据库中得到数据并将结果返回页面 我们先建立三个jsp页面,包括login. ...

  7. SQLServer-镜像配置

    实验环境:三台服务器分别为主服务器,镜像服务器,见证服务器,都加入域sql.com 1. 分别在三台服务器上安装SQL 2008 R2,安装数据库引擎和管理工具两个组件即可. 2. 镜像前准备工作. ...

  8. linux 基本命令 [转]

    linux 基本命令 1.ls  (list 显示当前目录下文件和目录 ls -l 详细显示 =ll ) [root@linux ~]# ls [-aAdfFhilRS] 目录名称 [root@lin ...

  9. HTML5 indexedDB数据库的入门学习(一)

    笔者早些时间看过web sql database,但是不再维护和支持,所以最近初步学习了一下indexedDB数据库,首先indexedDB(简称IDB)和web sql database有很大的差别 ...

  10. 实用工具推荐(Live Writer)(2015年05月26日)

    1.写博客的实用工具 推荐软件:Live Writer 使用步骤: 1.安装 Live Essential 2011,下载地址:http://explore.live.com/windows-live ...