Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® MotionBuilder® / Open Reality / Exposing functions to the Python
  RSS 2.0 ATOM  

Exposing functions to the Python
Rate this thread
 
13920
 
Permlink of this thread  
avatar
  • Petr Man
  • Posted: 30 June 2008 07:15 AM
  • Location: Czech republic
  • Total Posts: 11
  • Joined: 31 March 2008 10:27 AM

Hi,
is possible to expose own C++ functions to the Python scripting?? (like 3DS MAX SDK).
Thanks



Replies: 0
avatar

You would expose your own ORSDK class with boost::python, and it should compile fine. MotionBuilder should even pick up the DLL when starting up. I good place to start with would the boost site, but people definitely have this working, so you shouldn’t run into any major problems.

~Kristine



KRISTINE MIDDLEMISS | SENIOR DEVELOPER CONSULTANT
AUTODESK DEVELOPER NETWORK Media & Entertainment
http://www.autodesk.com/joinadn

Replies: 0
avatar

Boost.python Site:

http://wiki.python.org/moin/boost.python?highlight=(boost)



KRISTINE MIDDLEMISS | SENIOR DEVELOPER CONSULTANT
AUTODESK DEVELOPER NETWORK Media & Entertainment
http://www.autodesk.com/joinadn

Replies: 0
avatar
  • Petr Man
  • Posted: 02 September 2008 10:44 AM

Thank you very much! It was a very useful tip.

For others:
I needed to expose my own C++ functions (from my plugin dll) to Python.
Here is a example how to do it:

#include "boost/python.hpp"

void ExportedFn() {}

BOOST_PYTHON_MODULE
(MyModule)
{
  def
("ExportedFn"ExportedFn);  // Expose a fn
}

bool FBLibrary
::LibInit()
{
  Py_Initialize
();    // Inits python console
  
initMyModule();  // Inits own module
}

bool FBLibrary
::LibRelease()
{
  Py_Finalize
();    // Deinits python console
}

// In Python, just write:
import MyModule;
MyModule.ExportedFc();

Comments:
- MB contains boost_python.dll, for static linking I had to build lib for that dll. I used the boost 1.33.1 and Python 2.4.1. It works with MB 75 and MB 75 Ext2. To compiling I used the bjam, see bjam.
- I had a problem with compiling boost python and M$ STL Dinkumware. I used a namespace hack:

#include <typeinfo>
namespace std 
{
     typedef 
::type_info type_info;
}
#include <boost/python.hpp>


Replies: 0
avatar

Thanks for sharing, very cool!



KRISTINE MIDDLEMISS | SENIOR DEVELOPER CONSULTANT
AUTODESK DEVELOPER NETWORK Media & Entertainment
http://www.autodesk.com/joinadn

Replies: 0
avatar
  • KxL
  • Posted: 08 September 2008 04:20 AM

Nice, thanks for posting. I planned to look at this long time ago, but never really found time. So again thanks, and good to know that it is working!

Cheers



Replies: 0
avatar

Thanks Petr Man for posting this.  I spent quite some time trying to get this working, and wished for a more concrete example.  Now that I have it working, I thought I’d post that concrete example I had wished for.

I started with the orimpexpgames sample, and changed orimpexpgames.cxx to (comments removed):


#include “boost/python/module.hpp”
#include “boost/python/def.hpp”

#include



Replies: 0
avatar

OK, let’s try this again… The last post got mangled.

Thanks Petr Man for posting this.  I spent quite some time trying to get this working, and wished for a more concrete example.  Now that I have it working, I thought I’d post that concrete example I had wished for.

I started with the orimpexpgames sample, and changed orimpexpgames.cxx to (comments removed):

#include "boost/python/module.hpp"
#include "boost/python/def.hpp"

#include <fbsdk/fbsdk.h>

#ifdef KARCH_ENV_WIN
 #include <windows.h>
#endif

const char *hello() { return "hello world"}

BOOST_PYTHON_MODULE(SampleExporter)
{
 using 
namespace boost::python;
 
def("greet"hello)
}

FBLibraryDeclare( orimpexpgames )
{
 FBLibraryRegister( ORToolGamesExport )
}
FBLibraryDeclareEnd
;

bool FBLibrary::LibInit()
{
 Py_Initialize()
 initSampleExporter()
 
return true;
}

bool FBLibrary
::LibOpen() { return true}
bool FBLibrary
::LibReady() { return true}
bool FBLibrary
::LibClose() { return true}

bool FBLibrary
::LibRelease()
{
 Py_Finalize()
 
return true;
}

I used Visual Studio 2005 to build the sample.  I of course had to add include and lib paths to the project for boost and python, but I made no other modifications to the sample.  I did not have to use bjam.

In Python I have:

import SampleExporter
print SampleExporter.greet()


Replies: 0
avatar

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 1in <module>
    
from ctypes import *
  
File "C:\Program Files\Autodesk\MotionBuilder 2009\bin\win32\python25.zip\ctypes\__init__.py"line 20in <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.



Replies: 0
avatar
  • LPXO
  • Posted: 09 November 2009 08:02 AM

Anyone have any idea why i get this link error?

cannot open file ‘boost_python-vc80-mt-p-1_35.lib’

Ive tried 3 different installs of boost from BoostPro Computing and none of them lay down this file.  VC8 and VC9 yield the same result.

When i try to build the libraries myself it doesnt work.  First there is an error in one of the batch scripts which I spent time tracking down and fixing.  But when i run it, the .obj files get created but it never seems to generate the .lib files and doesn’t throw any errors or warnings.  This may be because I’m running Windows 7 64-bit.  Joy.



Replies: 1
/img/forum/dark/default_avatar.png

I had the same problem with 64bit.  I solved it by building boost specifically for 64bit with the following setting at the command line

bjam.exe --toolset=msvc-10.0 --build-type=complete architecture=x86 address-model=64 stage

If everything goes well it should build the boost python dll.  This file also needs coping into the SDK folder specified in motionbuilder for it to be picked up when mb starts.

Author: Ollie_D

Replied: 23 November 2011 12:12 PM  
avatar
  • lokkook
  • Posted: 01 November 2010 10:46 PM

Thanks for this thread.
I succeed in exposing my own ORSDK function to the python within MB 2010.
I am now trying to pass Model object as argument to those function.

For example, If I try to write a dummy function that returns a FBVector3d containing the position of a model, it fails :

const FBVector3d GetTranslation(HFBModel lModel)
{
 
return lModel->Translation;
}
Traceback (most recent call last):
  
File "<MotionBuilder>"line 1in <module>
Boost.Python.ArgumentErrorPython argument types in
    MCLLib
.GetTranslation(FBModel)
did not match C++ signature:
    
GetTranslation(class ORSDK80_RC1::FBModel *)

Does someone knows a way to use FB objects in such a way ?

Best regards,
L.



Replies: 0