473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Registering a python function in C

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!

Aug 30 '07 #1
5 2028
On Aug 30, 2:21 pm, fernando <fernandofariajun...@gmail.comwrote:
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):
Expand|Select|Wrap|Line Numbers
  1. PyObject* mod;
  2. PyObject* func;
  3.  
  4. mod = PyImport_ImportModule("mymodule");
  5. func = PyObject_GetAttrString( mod, "myfunc") ;
  6. foo(func);
  7.  
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

Aug 30 '07 #2
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!!!

Aug 31 '07 #3
On Aug 31, 3:33 pm, fernando <fernandofariajun...@gmail.comwrote:
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.

Aug 31 '07 #4
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!
Sep 3 '07 #5
On Sep 3, 7:11 pm, fernando <fernandofariajun...@gmail.comwrote:
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!
>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.

Sep 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob Gailer | last post by:
I need to register Python 2.1.3 so I can, in turn, install sqlite-python-0.3.1.win32-py2.1.exe, so I can in turn use pysqlite with ZOPE. Is there a way to register the python 2.1.3 that comes...
0
by: Sylwia | last post by:
Hi! I have the following problem :( After registering the "PythonService.exe" executable... C:\Python23\Lib\site-packages\win32>PythonService.exe -register I got the following error...
2
by: Helen | last post by:
I've got some clientside script that runs when the selected index of a select box changes. Now I need to swap my selectbox with an ASP.NET radiobuttonlist (to solve some layout issues). I still...
13
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
7
by: Chris Jewell | last post by:
Hi, I'm wondering what the best way of registering a data storage class with a data handler class is. At the moment I have two classes: class EpiCovars // Storage class { ....
1
by: pRony | last post by:
A warm welcome from me to everyone i am facing problems for registering python for using iis i tried out by running pyscript.py but was of no use an error is returned in the web page http/500...
1
by: shivkumar2004 | last post by:
Hi!, I am developing a chat system using vb.net in vs 2005. I am getting the following error while registering the events. error details: System.InvalidOperationException was unhandled...
2
by: linuxfedora | last post by:
I have written a C# program which has an activeX control (abc.ocx) in Visual Studio 2005, and used the setup and deployment project to create an setup.exe and setup.msi that act as an installer. ...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.