Maya API is exposed in C++ or Python only. There is no .Net API, but in case someone needs to use a .Net assembly in his plug-in, here some code and instructions on how to proceed.
#include <metahost.h>
#pragma comment(lib, "mscoree.lib")
#include "mscorlib.tlh"
//#include <Mscoree.h>
//#include <comdef.h>
//#import
// "C:\\WINDOWS\\Microsoft.NET\\Framework\\v1.1.4322\\mscorlib.tlb"
// raw_interfaces_only rename( "ReportEvent", "reportEvent" )
ICLRMetaHost *pMetaHost =NULL ;
ICLRRuntimeInfo *pRuntimeInfo =NULL ;
ICorRuntimeHost *pCorRuntimeHost =NULL ;
CComPtr<mscorlib::_Assembly> spMyAssembly ;
//- Call InitializeCLR() from initializePlugin()
bool InitializeCLR (PCWSTR pszVersion /* =L"v4.0.30319" */) {
::CoInitialize (NULL) ;
//- ICorRuntimeHost and ICLRRuntimeHost are the two CLR hosting
//- interfaces supported by CLR 4.0. Here we demo the
//- ICorRuntimeHost interface that was provided in .NET v1.x,
//- and is compatible with all .NET Frameworks.
//- Load and start the .NET runtime.
HRESULT hr =CLRCreateInstance (CLSID_CLRMetaHost,
IID_PPV_ARGS(&pMetaHost)) ;
//- Get the ICLRRuntimeInfo corresponding to a particular CLR
//- version. It supersedes CorBindToRuntimeEx with
//- STARTUP_LOADER_SAFEMODE.
hr =pMetaHost->GetRuntime (pszVersion,
IID_PPV_ARGS(&pRuntimeInfo)) ;
//- Check if the specified runtime can be loaded into the
//- process. This method will take into account other runtimes
//- that may already be loaded into the process and set
//- pbLoadable to TRUE if this runtime can be loaded in an
//- in-process side-by-side fashion.
BOOL fLoadable =false ;
hr =pRuntimeInfo->IsLoadable (&fLoadable) ;
if ( FAILED(hr) || !fLoadable )
return (false) ;
//- Load the CLR into the current process and return a runtime
//- interface pointer. ICorRuntimeHost and ICLRRuntimeHost are
//- the two CLR hosting interfaces supported by CLR 4.0. Here
//- we demo the ICorRuntimeHost interface that was provided in
//- .NET v1.x, and is compatible with all .NET Frameworks.
hr =pRuntimeInfo->GetInterface (CLSID_CorRuntimeHost,
IID_PPV_ARGS(&pCorRuntimeHost)) ;
hr =pCorRuntimeHost->Start () ;
return (hr == S_OK) ;
}
//- Call TerminateCLR() from uninitializePlugin()
void TerminateCLR () {
spMyAssembly.Release () ;
if ( pMetaHost )
pMetaHost->Release () ;
if ( pRuntimeInfo )
pRuntimeInfo->Release () ;
if ( pCorRuntimeHost ) {
//- Note that after a call to Stop, the CLR cannot be
//- reinitialized into the same process. This step is
//- usually not necessary. You can leave the .NET
//- runtime loaded in your process.
HRESULT hr =pCorRuntimeHost->Stop () ;
pCorRuntimeHost->Release () ;
}
}
//- Call LoadMyAssembly() from initializePlugin()
//- After that you can call your .Net assembly code.
bool LoadMyAssembly () {
//- Load the NET assembly.
//- Get a pointer to the default AppDomain in the CLR.
CComPtr<IUnknown> spAppDomainThunk ;
HRESULT hr =pCorRuntimeHost->GetDefaultDomain (
&spAppDomainThunk) ;
CComQIPtr<mscorlib::_AppDomain> spDefaultAppDomain
(spAppDomainThunk) ;
//- Load the .NET assembly.
//- Must resides in Maya bin folder or GAC
bstr_t bstrMyAssembly (_T("myAssembly")) ;
hr =spDefaultAppDomain->Load_2(bstrMyAssembly,&spMyAssembly);
return (hr == S_OK) ;
}
Note that you can also use the .Net Interop API to access the Maya C++ API, if you wish to access Maya API from .Net code, but I would recommend not to. Instead expose a .Net functionality class that you can call from your code. The sample above is really how to get access from a Maya plug-in to a .Net assembly not using Maya. But we will explore more on this in a following post.
Comments
You can follow this conversation by subscribing to the comment feed for this post.