Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Embedding as scripting engine

275 views
Skip to first unread message

Úlfur Kristjánsson

unread,
Jul 8, 2003, 6:50:55 AM7/8/03
to
Python... a great language, no doubt. And the abillity to be able to
extend it with C/C++ code, fantastic. But has anybody been able to
successfully embed Python in their application to use it as a
scripting language of sorts?

I am in the process of writing a game and from the documentation and
word of mouth gathered that Python would be just the thing for the
scripting part. And sure enough it is easy enough to run Python code
from C/C++. However, in order to be make it do something actually
useful for my game it would be handy to be able to pass in anything
besides the basic data types (strings, numerics) such as either an
instance of or a pointer to a gameobject. What I want to be able to do
is something like this:

GameObject go;

Py_Initialize()
...
PyObject* po = GameObject2PyObject(go);
...
result = PyObject_CallObject(func, "O", po );
...
go = PyObject2GameObject(result);

Theres alot of documentation and discussion out there that suggests
that this should be possible but noone seems to tell you how it's
actually done. Am I just missing something blatantly obvious?

Thanks
//ulk

Doug Tolton

unread,
Jul 8, 2003, 1:21:54 PM7/8/03
to
There are a couple of sources of information. Mark Hammond & Andy
Robinsons book "Python Programming on Win32" has an excelent section
on Dynamic Code Evaluation. This is in Chapter 8 Adding a Macro
language.

They also mention that there is a com server included with PythonWin
that is located at Python\win32com\servers\interp.py. This is a
python interpreter that can be bolted onto any com server.

If you are using a unix platform, you obviously won't be able to use
com, but the basic concepts will still be the same. You will need to
use Eval and Exec for dynamic code evaluation.

Good Luck

Doug Tolton

On 8 Jul 2003 03:50:55 -0700, theincre...@hotmail.com (Úlfur

Jp Calderone

unread,
Jul 8, 2003, 1:39:37 PM7/8/03
to
On Tue, Jul 08, 2003 at 03:50:55AM -0700, ?lfur Kristj?nsson wrote:
> Python... a great language, no doubt. And the abillity to be able to
> extend it with C/C++ code, fantastic. But has anybody been able to
> successfully embed Python in their application to use it as a
> scripting language of sorts?

Surprisingly, yes.

>
> I am in the process of writing a game and from the documentation and
> word of mouth gathered that Python would be just the thing for the
> scripting part. And sure enough it is easy enough to run Python code
> from C/C++. However, in order to be make it do something actually
> useful for my game it would be handy to be able to pass in anything
> besides the basic data types (strings, numerics) such as either an
> instance of or a pointer to a gameobject. What I want to be able to do
> is something like this:
>
> GameObject go;
>
> Py_Initialize()
> ...
> PyObject* po = GameObject2PyObject(go);
> ...
> result = PyObject_CallObject(func, "O", po );
> ...
> go = PyObject2GameObject(result);
>
> Theres alot of documentation and discussion out there that suggests
> that this should be possible but noone seems to tell you how it's
> actually done. Am I just missing something blatantly obvious?

Your question is to vague and short on details to be answered usefully.

What specific problems have you run into?

Jp

--
No, `Eureka' is Greek for `This bath is too hot.'
-- Dr. Who

Marcelo A. Camelo

unread,
Jul 8, 2003, 2:48:30 PM7/8/03
to
If you're using c++, take a look at boost::python, a fantastic
tool that can save you a lot of work when it comes to write the
bindings.

I would seriously consider extending instead of embedding.
I've done both and extending is, IMO, the way to go.

http://twistedmatrix.com/users/glyph/rant/extendit.html has some
good points on the subject. I second to all arguments presented
there.

--camelo

-----Original Message-----
From: python-l...@python.org [mailto:python-l...@python.org]
On Behalf Of Úlfur Kristjánsson
Sent: terça-feira, 8 de julho de 2003 07:51
To: pytho...@python.org
Subject: Embedding as scripting engine


Python... a great language, no doubt. And the abillity to be able to
extend it with C/C++ code, fantastic. But has anybody been able to
successfully embed Python in their application to use it as a scripting
language of sorts?

I am in the process of writing a game and from the documentation and


word of mouth gathered that Python would be just the thing for the
scripting part. And sure enough it is easy enough to run Python code
from C/C++. However, in order to be make it do something actually useful
for my game it would be handy to be able to pass in anything besides the
basic data types (strings, numerics) such as either an instance of or a
pointer to a gameobject. What I want to be able to do is something like
this:

GameObject go;

Py_Initialize()
...
PyObject* po = GameObject2PyObject(go);
...
result = PyObject_CallObject(func, "O", po );
...
go = PyObject2GameObject(result);

Theres alot of documentation and discussion out there that suggests that
this should be possible but noone seems to tell you how it's actually
done. Am I just missing something blatantly obvious?

Thanks
//ulk
--
http://mail.python.org/mailman/listinfo/python-list


Úlfur Kristjánsson

unread,
Jul 8, 2003, 5:42:27 PM7/8/03
to
Just thought I'd update my post as I have finally figured it out after
a week of trial and errors.

To pass an object "MyObject" into a Python script first create a
wrapper for it using SWIG. SWIG will generate everything needed by
Python to manipulate MyObject and a bunch of C code for creating,
casting and manipulating these objects. It amongst other things
creates a function:
PyObject* SWIG_NewPointerObj((void*)ptr, swig_type_info*, int)
This is used to create a new pointer to a structure of type
swig_type_info that again points to the data pointed to by ptr. The
last argument I am not to sure what is, but it works for me if I leave
it as 1. To use this function you will need to have a swig_type_info*
that describes your MyObject object, this is usually taken care of by
SWIG as well so if you have a lot of objects in your wrapper file it
is probably best to have them set up for you by making a call to the
SWIG_init() function that was generated for your wrapper.
To make a long story short it can be used like this:

{
#include "you_wrapper_code_created_by_swig"

MyObject object;
PyObject *mod, *dict, *func, *result;

Py_Initialize()
SWIG_init(); //Initialise SWIG types

mod = PyImport_ImportModule( modulename );
dict = PyModule_GetDict( mod );
func = PyDict_GetItemString( dict, "testObject" );
arg = SWIG_NewPointerObj((void*)&object, SWIGTYPE_p_IObject, 1);
result = PyObject_CallFunction( func, "O", arg);

Py_Finalize();

}


and in your python script:

def testObject( o ):
object = MyObjectPtr(o) #we need to create a pointer
#from the data that gets pased in
...
#Do what ever you like with the object
...

As it's a pointer to the actual object that gets passed to python you
will not have to worry about passing it back because any changes made
in python will affect the object created in C/C++, unless of course
you make a copy of it on the python side.

Anyway, that's the way I did it. It may be a bit of a hack, spit and
bubblegum, but it works and I'm quite pleased with myself because
noone else is willing to offer any assistance on the subject. I hope
this will helo anyone who finds himself in my situation.

//ulk


theincre...@hotmail.com (Úlfur Kristjánsson) wrote in message news:<df361dd5.03070...@posting.google.com>...

0 new messages