Calling MH as a library called from other programs

MakeHuman python API, python plugins, etc

Moderator: joepal

Calling MH as a library called from other programs

Postby m.e » Wed Jul 23, 2008 9:33 am

Is it possible to call this code from outside MH itself? What I am interested in is using MH as a plugin (see
http://www.makehuman.org/forum/viewtopic.php?f=4&t=53) so that I can animate the figures. As far as I can tell, MH at the moment only does still pictures. Also a lot of outstanding feature requests would be handled better by MH becoming part of a set of co-operating programs, rather than the MH team re-implementing features.

To this end, MH would need to be callable as a library. The API would need to allow the calling program to import pose data, either via location (eg as driven by Blender's armature system) or by parameters (eg driven by Blender IPOs). MH would need to be able to return mesh data and also positioning data -- for example, an object held in the hand means that MH's data for the position of the hand needs to be able to drive the object's position.

Hope this makes sense, otherwise ask and I will try to explain better.
m.e
 
Posts: 56
Joined: Wed Jul 23, 2008 9:20 am
Location: Shenzhen/Hong Kong

Re: Calling MH as a library called from other programs

Postby Manuel » Thu Jul 24, 2008 8:16 am

Yes, it's possible.
To make it callable from other python engines (in example, Blender), it must be compiled as python module, mh.pyd
So you can use it anywhere, just importing it with:

import mh

I've just cooked for you a special example:

http://www.dedalo-3d.com/lab/makehuman_ ... module.zip

there are 2 folders:
1) Example of mh as module usage
2) Example about how to compile it as python module instead executable.

Of course, both need a full python installed on your machine.

About (2), it's a very custom script, quickly done to work on windows...I've no time to port it on ubuntu,
but it should be even more simple :

Code: Select all
from distutils.core import setup, Extension

module1 = Extension('mh',
                    define_macros = [('FREEGLUT_STATIC', '1')],
                    include_dirs = ['C:\Python25\include','include','C:\MinGW\include'],
                    libraries = ['python25','freeglut','glu32','opengl32','winmm','gdi32'],
                    library_dirs = ['C:\MinGW\lib','C:\Python25\libs'],
                    sources = ['src/gltexture.c','src/core.c','src/glmodule.c','src/main2.c'])

setup (name = 'PackageName',
       version = '0.1',
       description = 'MakeHuman 3D proto library',
       author = 'Manuel Bastioni',
       author_email = 'mb@makehuman.org',
       url = 'http://www.makehuman.org',
       long_description = '''
A library to quick make 3d objects and 3d GUI.
''',
       ext_modules = [module1])


I call it with:

Code: Select all
python compile_pyd.py build --compiler=mingw32


Nothe that I've modified only the code of main.c, saving it as main2.c, for comparison.

I've copied the result (mh.pyd) is in the folder (1). If you have python on your machine, you are able to launch it with a double click on test.py...but, a more important thing for your question, you can place mh.pyd into a Blender directory, and use test.py as Blender script!

Cool, right?
If you have problems, just tell me...(maybe I reply in late, because in these days we are strognly working with some antrhopologist about MH ethcnic feature, and the GUI tu use it...)

Regards,

Manuel
Manuel
 

Re: Calling MH as a library called from other programs

Postby PapaRaven » Wed Jun 03, 2009 4:30 am

Firstly, sorry for thread-hopping.
From: http://www.makehuman.org/forum/viewtopic.php?f=9&t=195&p=2525#p2525
The API testing thread is where I started asking, but really I should have started here.

Despite having consistent Python versions between MH and Blender, any attempt to import the mh Python module into Blender simply kills Blender with a Segmentation fault. This is true for the interactive Blender Python window, as well as with a Blender text script. MakeHuman is from trunk, and Blender is 2.48a, and I've also tried their trunk (i.e. 2.49). I understand, and can see for myself, that the concept is perfectly sound since in the very same Blender directory, where I added symbolic links for all the MakeHuman directories and files, a normal Python (or IPython) session loaded mh as a module without a hitch.

In the blind guess department, I can only presume that MakeHuman is at odds with Blender for OpenGL resources, or some such thing. Or could they be duking it out over the way they manage memory? Mflerackers mentioned that mh.so was useful for Python debugging, so out of curiosity have any of you core folks tried this within Blender yourselves? -- Just for grins even?

I am fine with poking around until I find a solution/hack, but if anyone has some pointers or insights that might help me on this journey, I'd truly appreciate it.

Thanks
PapaRaven
 
Posts: 11
Joined: Sun Sep 14, 2008 5:10 am

Re: Calling MH as a library called from other programs

Postby mflerackers » Wed Jun 03, 2009 6:46 am

Is "import mh" the only statement in your script?
MakeHuman project Developer
mflerackers
 
Posts: 636
Joined: Thu Feb 05, 2009 11:53 am
Location: Kyoto

Re: Calling MH as a library called from other programs

Postby PapaRaven » Wed Jun 03, 2009 5:01 pm

Yup. I also tried running test.py (it needed some tweaks: some parameters needed to be wrapped as arrays, i.e. "[ ... ]").

Also (don't know if this is a valid test, but...) if I comment out "initGlobals();" in the "#ifdef MAKEHUMAN_AS_MODULE" block of main.c, then there is no segmentation fault. Granted, that doesn't mean that the fault comes immediately from assigning the values to G, but just I'm poking around and tryin' to see what's up. ;)
PapaRaven
 
Posts: 11
Joined: Sun Sep 14, 2008 5:10 am

Re: Calling MH as a library called from other programs

Postby PapaRaven » Wed Jun 03, 2009 6:22 pm

Up to a point commenting out the assignments within initGlobals gives no relief (I went down the line and added the assignments to the comment block). But at a point once "G.windowWidth = 800;" got included in the comment block, then the segmentation fault went away. At another point the crash wouldn't stop until I got to "G.fontOffset = 0;"...

Code: Select all
void initGlobals(void)
{
/*    G.world = NULL;
    G.nObjs = 0;
    G.fovAngle = 25;
    G.zoom = 60;
    G.rotX = 0;
    G.rotY = 0;
    G.translX = 0;
    G.translY = 0;
    G.windowHeight = 600; */
    G.windowWidth = 800;
    G.modifiersKeyState = 0;
    G.millisecTimer = 10;
    G.fontOffset = 0;
    G.pendingUpdate = 0;
    G.pendingTimer = 0;
    G.loop = 1;
    G.fullscreen = 0;
}


Now for the really funny part: Blender's main pull-down menu gets munged when mh is imported. The "File Add Timeline ..." etc. gets condensed down to, "F A Ti G R H". Wonky, eh? As I said, running in Python alone seems to work fine, but perhaps G needs to be situated differently for general compatibility or interoperability.(?)

Being as I'm learning this Python stuff as I go along (and hence C extensions to Python), I will try different ways of [re]defining G to see if that makes a difference.

:?
PapaRaven
 
Posts: 11
Joined: Sun Sep 14, 2008 5:10 am

Re: Calling MH as a library called from other programs

Postby PapaRaven » Wed Jun 03, 2009 7:00 pm

Ok - Changing, "extern Global G;" in core.h to, "static Global G;" ... and in main.c changing, "Global G;" to, "static Global G;" seems to make stuff happier. I tried this after running across this document which said ...
... It is strongly advised that all other symbols in your extension are declared static.
(http://starship.python.net/crew/mwh/toext/your-first-extension.html -- 2nd to last sentence on that page)
So I gave it a shot, and hey.

I'll give this a whirl, for now, and wait to hear from you folks. Perhaps this is only valid in the context of a module and would do absolutely horrible things in MakeHuman per se; I have no clue.
(I will tweak my build with the MAKEHUMAN_AS_MODULE macro so that my stand alone MakeHuman executable remains pure.)

:)
PapaRaven
 
Posts: 11
Joined: Sun Sep 14, 2008 5:10 am

Re: Calling MH as a library called from other programs

Postby mflerackers » Thu Jun 04, 2009 12:24 am

It might compile, but I don't think it will work, as by declaring them static in both h and c file, you actually create 2 different objects. So outside of main.c, you will use an uninitialized G object.
MakeHuman project Developer
mflerackers
 
Posts: 636
Joined: Thu Feb 05, 2009 11:53 am
Location: Kyoto

Re: Calling MH as a library called from other programs

Postby PapaRaven » Thu Jun 04, 2009 12:35 am

In that case calling it static in the .h file, and removing the declaration from the .c file also works. Other than that last oops, does this seem sound to you? I get the impression that this is the normal way of doing things for Python extension modules. :?:

[ Edit: ]
... Nevermind - glmodule.c, and any other consumers of core.h, would [still] end up with their very own copy of G as well. Hmm. Back to square zero...

[ Edit: ]
... Well, since it was the only thing that stopped the crash, I've wrapped the static declaration in a double macro in the .h file.
Code: Select all
#ifdef MAKEHUMAN_AS_MODULE
   #ifndef XYZPDQ
   #define XYZPDQ
   static Global G;
   #endif
#else
extern Global G;
#endif

For grins I tried out the MakeHuman executable with the static G; it really didn't like G being static; it got the Segmentation fault! Next steps: to verify whether the module works in Blender now, and then to see whether MakeHuman-embedded even makes any sense. :lol:
PapaRaven
 
Posts: 11
Joined: Sun Sep 14, 2008 5:10 am

Re: Calling MH as a library called from other programs

Postby Temujin » Fri Jun 05, 2009 1:55 pm

If this is of use to anyone, I hacked compile_module.bat into an sh, and compile_pyd.py to build on SuSE 10.3 :

compile_module.sh
Code: Select all
#!/bin/sh
python compile_pyd.py build


compile_pyd.py
Code: Select all
from distutils.core import setup, Extension

module1 = Extension('mh',
                    define_macros = [('FREEGLUT_STATIC', '1')],
                    include_dirs = ['/usr/include/python2.5','include'],
                    libraries = ['python2.5','glut','GLU','GL','glibmm-2.4','glib'],
                    library_dirs = ['/usr/lib','/usr/lib/python2.5'],
                    sources = ['src/gltexture.c','src/core.c','src/glmodule.c','src/main2.c'])

setup (name = 'PackageName',
       version = '0.1',
       description = 'MakeHuman 3D proto library',
       author = 'Manuel Bastioni',
       author_email = 'mb@makehuman.org',
       url = 'http://www.makehuman.org',
       long_description = '''
A library to quick make 3d objects and 3d GUI.
''',
       ext_modules = [module1])


This builds mh.so, and when I ran test.py, it output a 2845K postscript file called "sys", which displays nothing.

Other than that, I don't know how to use this module, but am looking foward to future developments :)
Rendered unconscious in 3D dreams
Temujin
 
Posts: 1
Joined: Fri Jun 05, 2009 1:38 pm

Next

Return to Python scripts

Who is online

Users browsing this forum: No registered users and 1 guest