Registering a python function in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fernando

    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!

  • Matimus

    #2
    Re: Registering a python function in C

    On Aug 30, 2:21 pm, fernando <fernandofariaj un...@gmail.com wrote:
    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.



    Matt

    Comment

    • fernando

      #3
      Re: Registering a python function in C

      Thanks for the responses. To be more specific, this code is part of a
      Maya plugin. The funcion MFnPlugin::regi sterUI 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_Import Module... 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_Import Module doesn't even return, it should
      return 0 if something bad happened, right?

      Here's my code:

      if(Py_IsInitial ized())
      cout << "python is already initialized" << endl;
      if(!Py_IsInitia lized()){
      cout << "had do initialize python" << endl;
      Py_Initialize() ;
      }
      PyObject* mod= PyImport_Import Module("vzPyTes t");
      if(mod == 0){
      cout << "didn't load" << endl;
      }
      PyObject* func1 = PyObject_GetAtt rString(mod, "vzPyTest.test1 ");
      PyObject* func2 = PyObject_GetAtt rString(mod, "vzPyTest.test2 ");
      plugin.register UI(func1, func2);


      Thanks for the help!!!

      Comment

      • AnonMail2005@gmail.com

        #4
        Re: Registering a python function in C

        On Aug 31, 3:33 pm, fernando <fernandofariaj un...@gmail.com wrote:
        Thanks for the responses. To be more specific, this code is part of a
        Maya plugin. The funcion MFnPlugin::regi sterUI 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_Import Module... 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_Import Module doesn't even return, it should
        return 0 if something bad happened, right?
        >
        Here's my code:
        >
        if(Py_IsInitial ized())
        cout << "python is already initialized" << endl;
        if(!Py_IsInitia lized()){
        cout << "had do initialize python" << endl;
        Py_Initialize() ;}
        >
        PyObject* mod= PyImport_Import Module("vzPyTes t");
        if(mod == 0){
        cout << "didn't load" << endl;}
        >
        PyObject* func1 = PyObject_GetAtt rString(mod, "vzPyTest.test1 ");
        PyObject* func2 = PyObject_GetAtt rString(mod, "vzPyTest.test2 ");
        plugin.register UI(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.

        Comment

        • fernando

          #5
          Re: Registering a python function in C

          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!


          Comment

          • AnonMail2005@gmail.com

            #6
            Re: Registering a python function in C

            On Sep 3, 7:11 pm, fernando <fernandofariaj un...@gmail.com wrote:
            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.

            Comment

            Working...