Registering a python function in C

fernando fernandofariajunior at gmail.com
Fri Aug 31 15:33:04 EDT 2007


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!!!




More information about the Python-list mailing list