Sometimes, we want to manipulate Max from the outside. For example, I sent the motion information of the Kinect to Max a few days ago. max itself provides a way to register itself as an OLE Automation object, which is disabled by default. In the OLE Automation section of maxscript's built-in documentation, you can describe the relevant information.
To enable this operation, you only need to import one registry. For the Registry file in this document, see "OLE Automation --> maxscript. reg-registery File". You need to change the max version and installation path on your own.
You can also put this Max script in the scripts \ Startup directory of the Max installation path.
Register the maxscript of the OLE Interface
(
Local version, outstr, executablepath, regpath
Version = (maxversion () [1]/1000
Outstr = createfile "$ scripts/register_mxscom.reg"
Format "Windows Registry Editor Version 5.00 \ n" to: outstr
Format "[hkey_classes_root \ Max. Application] \ n" to: outstr
Format "@ = \" OLE Automation MAX application \ "\ n" to: outstr
Format "[hkey_classes_root \ Max. Application \ CLSID] \ n" to: outstr
Format "@ = \" {7fa22cb1-d26f-11d0-b260-00a0240ceea3} \ "\ n" to: outstr
Format "[hkey_classes_root \ Max. Application \ curver] \ n" version to: outstr
Format "@ = \" Max. application. % \ "\ n" version to: outstr
Format "hkey_classes_root \ CLSID \ {7fa22cb1-d26f-11d0-b260-00a0240ceea3} = OLE Automation max %. 0 Application \ n" version to: outstr
Format "hkey_classes_root \ CLSID \ {7fa22cb1-d26f-11d0-b260-00a0240ceea3} \ progid = max. application. % \ n" version to: outstr
Format "hkey_classes_root \ CLSID \ {7fa22cb1-d26f-11d0-b260-00a0240ceea3} \ versionindependentprogid = max. Application \ n" to: outstr
Executablepath = (getdir # maxroot) + "3dsmax.exe"
Format "hkey_classes_root \ CLSID \ {7fa22cb1-d26f-11d0-b260-00a0240ceea3} \ localserver32 = % \ n" executablepath to: outstr
Flush outstr
Close outstr
Regpath = "/s \" "+ (getdir # maxroot) +" scripts \ register_mxscom.reg \""
Shelllaunch "Regedit" regpath
Registeroleinterface # (filein, execute)
)
/*
These rows are used to test whether the registration is successful.
Maxapp = createoleobject "Max. Application"
Maxapp.exe cute "box ()"
Maxapp. filein "E: \ t. MS"
*/
This script will create a register_mxscom.reg file in the scripts directory under the max installation directory. If it is win7, you may need to manually import the file because the permission to prohibit writing data to the Registry.
If the manual import is still invalid, restart the computer and enable it.
Subsequently, you can use VBscript or JScript to operate Max. These two scripts require Windows Script Host (usually installed in the system)
The content of JScript is saved as a. js file, and the content of VBScript is saved as a. vbs file. If everything is correct, double-click any of the two files and create a cube in Max.
JScript
Maxapp = new activexobject ("Max. application ");
Maxapp. Execute ("box ()"); VBScript
Set max = Createobject ("Max. application ")
Max. Execute ("box ()")
Maybe we are not satisfied with operating Max in VBScript or JScript, such as the part related to the Kinect I mentioned earlier. We can also use C ++ or C. The C # method throws an exception. The Microsoft website interprets the exception as follows.
In addition, if python is used, you can use win32com. Client in pywin32 to operate Max. For more information, see the maxscript section of cgtalk.
Because many people are learning python, You need to download the corresponding Python version of pywin32 by adding the python method. Just search for it.
C ++
# Include "afxdisp. H"
# Include <iostream>
Using namespace STD;
Int main (INT argc, tchar * argv [])
{
: Coinitialize (null );
Coledispatchdriver DSP;
If (! DSP. createdispatch ("Max. application "))
{
Cout <L "Max. Application creation failed" <Endl;
Return-1;
}
Dispid propid;
BSTR propname [1];
Propname [0] = sysallocstring (L "execute ");
DSP. m_lpdispatch-> getidsofnames (iid_null, propname, 1, locale_system_default, & propid );
Static byte parms [] = vts_bstr;
Lpctstr lpszargvalue;
If (argc> 1)
{
Lpszargvalue = argv [1];
}
Else
{
Lpszargvalue = "box ()";
}
DSP. invokehelper (propid, dispatch_method, vt_empty, null, parms, lpszargvalue );
DSP. releasedispatch ();
: Couninitialize ();
Return 0;
}
C #
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace SharpMaxCom
{
class Program
{
static void Main(string[] args)
{
Type AutodeskMax = Type.GetTypeFromProgID("Max.Application");
Object MaxApplication = Activator.CreateInstance(AutodeskMax);
object[] parameter = new object[1];
parameter[0] = "box()";
try
{
AutodeskMax.InvokeMember("execute", BindingFlags.InvokeMethod | BindingFlags.Instance, System.Type.DefaultBinder, MaxApplication, parameter);
}
catch (Exception EM)
{
Console.WriteLine(EM.ToString());
}
Console.ReadKey();
}
}
}
Python
import win32com.client
MaxApplication = win32com.client.Dispatch("Max.Application")
MaxApplication._FlagAsMethod("Execute")
MaxApplication.Execute("Box()")