Registering a python function in C

F

fernando

Could someone post an example on how to register a python function as
a callback in a C function? It expects a pointer to PyObject... how do
I expose that? Basically, the signature of the function is
foo(PyObject* obj), where obj is the callback function... It's not
exactly extending or embedding, I've looked at those examples but they
don't really show how to do this...

Thanks for the help!
 
M

Matimus

Could someone post an example on how to register a python function as
a callback in a C function? It expects a pointer to PyObject... how do
I expose that? Basically, the signature of the function is
foo(PyObject* obj), where obj is the callback function... It's not
exactly extending or embedding, I've looked at those examples but they
don't really show how to do this...

Thanks for the help!

It most definitely is extending or embedding, it just doesn't follow
the examples provided. I can't be too specific because I don't know
what your environment is like. As in, have you already initialized
python and all of that? Do you have access to _any_ PyObject*
variables?

Here is something that _might_ help (not tested):
Code:
PyObject* mod;
PyObject* func;

mod = PyImport_ImportModule("mymodule");
func = PyObject_GetAttrString( mod, "myfunc") ;
foo(func);

It might be useful to look at the "Python/C API Reference Manual", not
just the extending and embedding stuff.

http://docs.python.org/api/api.html

Matt
 
H

Hrvoje Niksic

fernando said:
Could someone post an example on how to register a python function as
a callback in a C function?

If I understand correctly, your C function receives a Python function
(as a function object of type PyObject *), which you need to call from
C. To do that, call PyObject_CallFunction(obj, format, args...) where
format and args are documented in
http://docs.python.org/api/arg-parsing.html.

Does that help?

Also note that there is a dedicated mailing list for the Python/C
API; see http://mail.python.org/mailman/listinfo/capi-sig .
 
F

fernando

Thanks for the responses. To be more specific, this code is part of a
Maya plugin. The funcion MFnPlugin::registerUI takes a pointer to a
PyObject which is the function that will set up the UI for that
plugin. The code Matimus posted seems to me exactly like what I need
to do, except that maya gives me an error when I call
PyImport_ImportModule... I don't even have a chance to check the
return value, Maya simply gives up. I have checked that python is
initialized by the time I call this function, and the python path is
correct, I can load the module from the maya python interpreter. What
bugs me is that PyImport_ImportModule doesn't even return, it should
return 0 if something bad happened, right?

Here's my code:

if(Py_IsInitialized())
cout << "python is already initialized" << endl;
if(!Py_IsInitialized()){
cout << "had do initialize python" << endl;
Py_Initialize();
}
PyObject* mod= PyImport_ImportModule("vzPyTest");
if(mod == 0){
cout << "didn't load" << endl;
}
PyObject* func1 = PyObject_GetAttrString(mod, "vzPyTest.test1");
PyObject* func2 = PyObject_GetAttrString(mod, "vzPyTest.test2");
plugin.registerUI(func1, func2);


Thanks for the help!!!
 
A

AnonMail2005

Thanks for the responses. To be more specific, this code is part of a
Maya plugin. The funcion MFnPlugin::registerUI takes a pointer to a
PyObject which is the function that will set up the UI for that
plugin. The code Matimus posted seems to me exactly like what I need
to do, except that maya gives me an error when I call
PyImport_ImportModule... I don't even have a chance to check the
return value, Maya simply gives up. I have checked that python is
initialized by the time I call this function, and the python path is
correct, I can load the module from the maya python interpreter. What
bugs me is that PyImport_ImportModule doesn't even return, it should
return 0 if something bad happened, right?

Here's my code:

if(Py_IsInitialized())
cout << "python is already initialized" << endl;
if(!Py_IsInitialized()){
cout << "had do initialize python" << endl;
Py_Initialize();}

PyObject* mod= PyImport_ImportModule("vzPyTest");
if(mod == 0){
cout << "didn't load" << endl;}

PyObject* func1 = PyObject_GetAttrString(mod, "vzPyTest.test1");
PyObject* func2 = PyObject_GetAttrString(mod, "vzPyTest.test2");
plugin.registerUI(func1, func2);

Thanks for the help!!!
One thing I've learned is that the C interface can be tricky the first
time.

Is Maya a different python build than what is contained at python.org?
If so, I suggest you get your C program to work with the latest python
build
from python.org. Then see if you can get it to work with the Maya
version.
 
F

fernando

Is Maya a different python build than what is contained at python.org?
If so, I suggest you get your C program to work with the latest python
build
from python.org. Then see if you can get it to work with the Maya
version.

Ok, did that. If I write a normal C++ program and use the python
installed in my system, everything works ok and I can call the python
funtions. From within maya(where the C code is running as a plugin),
nothing happens. I tried removing my python installation so that only
the one that comes with maya is running, but then I have no python.h
or libs to compile against!! I found no help at the maya/python
newsgroup, is there anyone who has done this before???


Thanks for all the help!
 
M

Matt McCredie

Is Maya a different python build than what is contained at python.org?
Ok, did that. If I write a normal C++ program and use the python
installed in my system, everything works ok and I can call the python
funtions. From within maya(where the C code is running as a plugin),
nothing happens. I tried removing my python installation so that only
the one that comes with maya is running, but then I have no python.h
or libs to compile against!! I found no help at the maya/python
newsgroup, is there anyone who has done this before???

I don't really know how maya works with python. Is it possible to just
run arbitrary python code from maya? Can you get the version strings
and stuff?

That might give you a clue. It might just be that you need to compile
against a different version of Python. You could always just download
the different versions of Python and see if the included Python.h and
Python.lib work. I would go in this order: 2.3, 2.4, 2.2, 2.5.

Matt
 
A

AnonMail2005

Ok, did that. If I write a normal C++ program and use the python
installed in my system, everything works ok and I can call the python
funtions. From within maya(where the C code is running as a plugin),
nothing happens. I tried removing my python installation so that only
the one that comes with maya is running, but then I have no python.h
or libs to compile against!! I found no help at the maya/python
newsgroup, is there anyone who has done this before???

Thanks for all the help!
From what I understand, it seems like maya includes a python build/
download
when you download maya.

You should be able to get the libs and include files from there. It
also
looks like maya explicitly says what python build it uses when it is
started up interactively.

I would bet that your installation of maya and your (different)
installation of
python are conflicting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top