|
I found another way to expose functions from my MotionBuilder plugin to Python. As of Python 2.5, Python contains ctypes, a foreign function library that allows calling functions in DLLs or shared libraries.
Here’s an example. Starting with the orimpexpgames sample, I added the function I want to export to orimpexpgames.cxx:
__declspec(dllexport) char *hello() { return "hello world"; }
Now orimpexpgames.cxx is (with comments removed to save space here):
#include <fbsdk/fbsdk.h>
#ifdef KARCH_ENV_WIN
#include <windows.h>
#endif
__declspec(dllexport) char *hello() { return "hello world"; }
FBLibraryDeclare( orimpexpgames )
{
FBLibraryRegister( ORToolGamesExport )
}
FBLibraryDeclareEnd;
bool FBLibrary::LibInit() { return true; }
bool FBLibrary::LibOpen() { return true; }
bool FBLibrary::LibReady() { return true; }
bool FBLibrary::LibClose() { return true; }
bool FBLibrary::LibRelease(){ return true; }
I also added a module definition file, export.def, which is:
LIBRARY "orimpexpgames" EXPORTS
hello
I entered “export.def” in the “Module Definition File” property of the Linker Input tab of the orimpexpgames project property dialog. This tells Visual Studio to expose my hello function in the dll.
In the Python editor in MotionBuilder, I call my function like this:
>>> from ctypes import *
>>> print c_char_p(cdll.orimpexpgames.hello()).value
hello world >>>
See http://docs.python.org/library/ctypes.html.
There was one small hitch. In MotionBuilder 2009, ctypes is in \bin\win32\python25.zip, which is apparently from Python 2.5.2, while MotionBuilder is running 2.5.1. This results in:
>>> Traceback (most recent call last):
File "C:/Program Files/Autodesk/MotionBuilder 2009/bin/config/Scripts/hello.py", line 1, in <module>
from ctypes import *
File "C:\Program Files\Autodesk\MotionBuilder 2009\bin\win32\python25.zip\ctypes\__init__.py", line 20, in <module> Exception: ('Version number mismatch', '1.0.2', '1.0.3') >>>
I solved this by downloading the Python 2.5.1 source and zipping the Lib folder into a new python25.zip.
|