473,704 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

beginner questions on embedding/extending python with C++

Hi Everyone,

I am a beginner on cross language development. My problem at hand is to
build a python interface for a C++ application built on top of a 3D
game engine. The purpose of this python interface is providing a
convenient scripting toolkit for the application. One example is that
a user can write a python script like:
player = Player()
game.loadPlayer (player)
player.moveTo(l ocation)
To configure the game environment.

I am trying to figure out how to make this happen. I only have the
vague idea that this concerns embedding/extending python with C++, but
after reading Python Doc, SWIG, BOOST.Python, I am still not sure how
to architecture it.

Since the main program is still going to be the C++ application, I
guess we need to embedding the python scripts in the C++ code. So at
initialization stage, the python script needs to be loaded into the C++
code. And this code can be simple, like
player = Player()
game.loadPlayer (player)
But for this to work, the python code needs to know the Player class,
is it right? Does that mean I need to build a python wrapper class for
Player and "import Player" in the python code? But because this
application is built on top of a game engine, Player class inherits
many classes from there, I cannot possibly wrapping them all, right?
Also, some global objects are probably needed in this code of adding
players, how can the python code access them?

I know I probably don't have a grasp of basics here, please kindly
enlighten me!

Btw, if you can point me to any source code of non-trivial projects
utilizing SWIG/Boost.Python, that would be very helpful. I found the
examples on the tutorials are far too simple.

Thank you very much,
Qun

Aug 8 '06 #1
6 3002
Since the main program is still going to be the C++ application, I
guess we need to embedding the python scripts in the C++ code. So at
initialization stage, the python script needs to be loaded into the C++
code. And this code can be simple, like
player = Player()
game.loadPlayer (player)
But for this to work, the python code needs to know the Player class,
is it right? Does that mean I need to build a python wrapper class for
Player and "import Player" in the python code? But because this
application is built on top of a game engine, Player class inherits
many classes from there, I cannot possibly wrapping them all, right?
Also, some global objects are probably needed in this code of adding
players, how can the python code access them?
You should look into SIP besides the tools you already mentioned - IMHO it
is the best choice for wrapping C++.

And yes, you need to wrap classes - but only those that are of interest for
you! So if you need Player, wrap Player. No need to wrap it's base-classes,
unless you want these for other purposes, too.

And for global objects I'd create functions which return these.

I suggest you try and download a project that uses one of the possible
toolkits for wrapping - the most prominent user of SIP is of course PyQt.
Go and have a look at the source, how things are done. There aresome
tutorials I think, google should help you on that.

HTH Diez
Aug 8 '06 #2
On 8 Aug 2006 02:28:31 -0700, Qun Cao <qu****@gmail.c omwrote:
Hi Everyone,

I am a beginner on cross language development. My problem at hand is to
build a python interface for a C++ application built on top of a 3D
game engine. The purpose of this python interface is providing a
convenient scripting toolkit for the application.
As for me, Boost.Python is the way to go.

Fortunately you are not the first one, and I hope not the last one :-) :

http://language-binding.net/pypluspl...ing-pyplusplus
1. Python-OGRE
* http://lakin.weckers.net/index_ogre_python.html
* http://tinyurl.com/mvj8d

2. http://cgkit.sourceforge.net/ - contains Python bindings for Maya C++ SDK

3. PyOpenSG - https://realityforge.vrsource.org/view/PyOpenSG/WebHome
The goal of PyOpenSG is to provide python bindings for the OpenSG
scene graph.
Since the main program is still going to be the C++ application, I
guess we need to embedding the python scripts in the C++ code.
Boost.Python is the only tool that provides complete functionality(
extending and
embedding ). Also I think cgkit is dealing with the problem too.
But for this to work, the python code needs to know the Player class,
is it right?
Right.

Does that mean I need to build a python wrapper class for
Player and "import Player" in the python code? But because this
application is built on top of a game engine, Player class inherits
many classes from there, I cannot possibly wrapping them all, right?
It depends on how much functionality you want to export.
Also, some global objects are probably needed in this code of adding
players, how can the python code access them?
Boost.Python provides the functionality you need.
Btw, if you can point me to any source code of non-trivial projects
utilizing SWIG/Boost.Python, that would be very helpful. I found the
examples on the tutorials are far too simple.
Those are tutorials, they should be simple, right :-) ?

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
Aug 8 '06 #3
Qun Cao wrote:
Hi Everyone,

I am a beginner on cross language development. My problem at hand is to
build a python interface for a C++ application built on top of a 3D
game engine. The purpose of this python interface is providing a
convenient scripting toolkit for the application.
As well as this group/list, you may find some useful help at the
Gamedev.net Scripting Languages and Game Modifications forum:
http://www.gamedev.net/community/for...sp?forum_id=41

Many people there have mixed Python and C++ code in the context of game
applications and may have some good answers to your questions. After
deciding how to expose C++ classes and functions to Python, the main
problem you'll face, assuming you need the functionality, is how to
yield control between Python and C++ when a Python action is
long-running yet requires the C++ engine to be polled (eg. to handle
input, AI, graphics, etc). Previous threads there will give you some
hints on all these matters.

--
Ben Sizer

Aug 8 '06 #4
Thanks Diez,
It is a good relief that I only need to wrap the classes I need. I
decide to try Boost first because it seems to have a wider audience
than SIP, but I would definately look into SIP if I want to do Qt
development in the future.
Diez B. Roggisch wrote:
Since the main program is still going to be the C++ application, I
guess we need to embedding the python scripts in the C++ code. So at
initialization stage, the python script needs to be loaded into the C++
code. And this code can be simple, like
player = Player()
game.loadPlayer (player)
But for this to work, the python code needs to know the Player class,
is it right? Does that mean I need to build a python wrapper class for
Player and "import Player" in the python code? But because this
application is built on top of a game engine, Player class inherits
many classes from there, I cannot possibly wrapping them all, right?
Also, some global objects are probably needed in this code of adding
players, how can the python code access the
You should look into SIP besides the tools you already mentioned - IMHO it
is the best choice for wrapping C++.

And yes, you need to wrap classes - but only those that are of interest for
you! So if you need Player, wrap Player. No need to wrap it's base-classes,
unless you want these for other purposes, too.

And for global objects I'd create functions which return these.

I suggest you try and download a project that uses one of the possible
toolkits for wrapping - the most prominent user of SIP is of course PyQt.
Go and have a look at the source, how things are done. There aresome
tutorials I think, google should help you on that.

HTH Diez
Aug 9 '06 #5
Thanks for all the good pointers!
I am still reading throught them, but Boost seems to be the way to go!

Roman Yakovenko wrote:
On 8 Aug 2006 02:28:31 -0700, Qun Cao <qu****@gmail.c omwrote:
Hi Everyone,

I am a beginner on cross language development. My problem at hand is to
build a python interface for a C++ application built on top of a 3D
game engine. The purpose of this python interface is providing a
convenient scripting toolkit for the application.

As for me, Boost.Python is the way to go.

Fortunately you are not the first one, and I hope not the last one :-) :

http://language-binding.net/pypluspl...ing-pyplusplus
1. Python-OGRE
* http://lakin.weckers.net/index_ogre_python.html
* http://tinyurl.com/mvj8d

2. http://cgkit.sourceforge.net/ - contains Python bindings for Maya C++ SDK

3. PyOpenSG - https://realityforge.vrsource.org/view/PyOpenSG/WebHome
The goal of PyOpenSG is to provide python bindings for the OpenSG
scene graph.
Since the main program is still going to be the C++ application, I
guess we need to embedding the python scripts in the C++ code.

Boost.Python is the only tool that provides complete functionality(
extending and
embedding ). Also I think cgkit is dealing with the problem too.
But for this to work, the python code needs to know the Player class,
is it right?

Right.

Does that mean I need to build a python wrapper class for
Player and "import Player" in the python code? But because this
application is built on top of a game engine, Player class inherits
many classes from there, I cannot possibly wrapping them all, right?

It depends on how much functionality you want to export.
Also, some global objects are probably needed in this code of adding
players, how can the python code access them?

Boost.Python provides the functionality you need.
Btw, if you can point me to any source code of non-trivial projects
utilizing SWIG/Boost.Python, that would be very helpful. I found the
examples on the tutorials are far too simple.

Those are tutorials, they should be simple, right :-) ?

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
Aug 9 '06 #6
Hi All,
I am in the process also of trying to call Python script from a C++
windows app.
I have looked at the Boost site and am currently reading over the
Embedding portion of the tutorial. A question I have is that there
appear to be about 4 or 5 Boost items avaiable for download. Which one
should be downloaded to do the embedding?
Thanks
Jeff
Qun Cao wrote:
Thanks for all the good pointers!
I am still reading throught them, but Boost seems to be the way to go!

Roman Yakovenko wrote:
On 8 Aug 2006 02:28:31 -0700, Qun Cao <qu****@gmail.c omwrote:
Hi Everyone,
>
I am a beginner on cross language development. My problem at hand is to
build a python interface for a C++ application built on top of a 3D
game engine. The purpose of this python interface is providing a
convenient scripting toolkit for the application.
As for me, Boost.Python is the way to go.

Fortunately you are not the first one, and I hope not the last one :-) :

http://language-binding.net/pypluspl...ing-pyplusplus
1. Python-OGRE
* http://lakin.weckers.net/index_ogre_python.html
* http://tinyurl.com/mvj8d

2. http://cgkit.sourceforge.net/ - contains Python bindings for Maya C++ SDK

3. PyOpenSG - https://realityforge.vrsource.org/view/PyOpenSG/WebHome
The goal of PyOpenSG is to provide python bindings for the OpenSG
scene graph.
Since the main program is still going to be the C++ application, I
guess we need to embedding the python scripts in the C++ code.
Boost.Python is the only tool that provides complete functionality(
extending and
embedding ). Also I think cgkit is dealing with the problem too.
But for this to work, the python code needs to know the Player class,
is it right?
Right.

Does that mean I need to build a python wrapper class for
Player and "import Player" in the python code? But because this
application is built on top of a game engine, Player class inherits
many classes from there, I cannot possibly wrapping them all, right?
It depends on how much functionality you want to export.
Also, some global objects are probably needed in this code of adding
players, how can the python code access them?
Boost.Python provides the functionality you need.
Btw, if you can point me to any source code of non-trivial projects
utilizing SWIG/Boost.Python, that would be very helpful. I found the
examples on the tutorials are far too simple.
Those are tutorials, they should be simple, right :-) ?

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
Aug 9 '06 #7

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

Similar topics

1
1408
by: Mark Harrison | last post by:
Is the document Extending and Embedding the Python Interpreter http://www.python.org/doc/current/ext/ext.html easily available in any other form? Pointers to either PDF or a single web page would be most appreciated! Many TIA,
4
2787
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
2
2868
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an embedded scripting language (a very limited one). I would like to rip it out and replace it with Python. I am thinking that this would be relatively simple since the scripting language is a very small interface between the UI and the engine --...
1
1914
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some questions about python embedding: 1) Is there a good text book or other resource on embedding/extending? (I find it hard to learn only by the tutorial and C/Python API from the python.org site)
1
1835
by: Tommy Nordgren | last post by:
I want to write an application that embeds and extends (at least) the Python and Perl interpreters. Now i want to find as much as possible about the Python tools used for extending and embedding Python. To be more specific: My app should: 1. Parse an input file. 2. Call a script in some scripting language, to generate an output file, for example in C++. For task 2 I need to call an embedded interpreter, and also provide call backs from...
3
1363
by: Redefined Horizons | last post by:
I've got a third-part application that exposes a C API. I'd like to wrap it in Python. Is there a specific forum that covers extending and embedding Python, or are those type of questions O.K. on this list? Scott Huey
1
1856
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples shown in "Extending..." there are some things I ma not familiar with so I turn to the index in the Reference Manual, but they are not listed. For example, PyEval_CallObject and PyDict_GetAttrString. My question is this: Is the documentation out of...
3
1671
by: anonymisiert85 | last post by:
At the moment i can run python-string-code from C (MinGW, WinXP) But how can i register a C-function in python-RUNTIME and call this C function from python - without wrapper dll's or libs??? STEPS: initialize python regsiter foo() ########### don't know how to do this run python-script "c=foo(a,b)"
0
2111
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 callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
0
8688
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9135
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5932
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4439
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3136
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2485
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.