Python wrapper and c++ core

Hi everyone!
Can you explain how panda’s python wrapper works?
I mean how can i add a c++ code to panda’s c++ core and make a python wrapper for it?
Is it panda’s python code that run first or panda’s c++ core?
Is there any main function in c++ code?
thank you

Our tool is called interrogate, and you can search the forums for more information about it. It runs when Panda is compiled; it automatically reads the C++ source code and generates Python wrappers for any functions marked “PUBLISHED” in the source. Then those wrappers get compiled and linked into the code so they’re available to call from Python.

The main loop remains in Python; the Python program simply makes calls into the Python wrappers. Calls back from Panda to Python are also possible, but far less common.

David

Thank you David.
I cant understand how scripting works.
Can you explain how this simple code works?

from direct.showbase.ShowBase import ShowBase
 
class MyApp(ShowBase):
 
    def __init__(self):
        ShowBase.__init__(self)
 
        # Load the environment model.
        self.environ = self.loader.loadModel("models/environment")
        # Reparent the model to render.
        self.environ.reparentTo(self.render)
 
 
app = MyApp()
app.run()

When i run this code Panda’s python wrapper start the main loop. But what’s happen when python wants to load a model and reparenting to render?I mean relation between c++ core and python main loop.

That’s an awfully big question.

The short answer is, there are Python wrapper functions that have the same name as their C++ equivalents. When Python code calls one of the Python wrapper functions, that wrapper function decodes the Python arguments it receives, and re-encodes them into their C++ equivalents, and then calls the corresponding C++ function. Then it takes the return value from the C++ function and creates a Python object around it, which it returns from the Python wrapper.

So, the net result is that Python calls all of these Python wrappers as if it is directly calling the corresponding C++ functions, and it gets the results as if those C++ functions were actually Python functions.

As to your code snippet, it is relying heavily on the code in ShowBase.py, which has the Python support structure to call all of the underlying Panda3D functions that open a window and start the main loop going. You can examine ShowBase.py yourself if you want to see some of this happening. Some of the functions that ShowBase calls are actually Panda’s C++ wrapper functions.

Most of the functions that you are calling in your snippet are actually Python functions from ShowBase and other Python sources; but some of them are C++ wrapper functions. For instance, self.environ.reparentTo() is one of these wrapper functions. When you call that function, it is calling the NodePath.reparentTo() wrapper function, which turns around and calls the equivalent NodePath::reparent_to() function in C++.

Most Python programmers don’t need to think about all this. You just imagine that the Panda3D objects are actually Python objects, and it mostly works the way you would expect.

David

Thank you very much for good answer.