Python embeding question

(SmaTheGreek) #1

This thread is not related to blender. How can I embed and use python in order to modify the values of variables which belong to a c++ appliation ?

0 Likes

(MrRage) #2

Very painfully…

This page covers the basic idea very well. http://docs.python.org/ext/embedding.html

The problem I’ve found with doing development like this is the compiler. Python for windows is built using VC2003 and unless you have it its going to be very painful to create modules, which you need for python to access your C++ code. The thing I got stuck on was disutils is written only for VC2003. If Disutils worked with 2005 then it would be alright…

Added:
After reviewing that page it looks like you might not need to construct a package to use it - in wich case I could use VC2005 - So thats pretty cool.

0 Likes

(tedi) #3

I think I’ve seen some woodoo howtos on how to compile modules with mingw for windows. Anyhow, it was very voodoo …

0 Likes

(MrRage) #4

Yeah a lot of people say that.

This might be worth looking into.
http://www.cython.org/

I’m going to try writing something in cython then embed it into a C++ app to see if I can get python to access parts of my C++ app. If that works then cool and I’ll post an example.

0 Likes

(SmaTheGreek) #5

Why should I use external tools ?

I thought I could use something like that :

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime
"
                     "print 'Today is',ctime(time())
");
  Py_Finalize();
  return 0;
}

What I want is to create python plug-ins for my c++ application.
Something like the blender python editor …
Any ideas ?

0 Likes

(Duoas) #6

http://www.python.org/doc/ext/embedding.html

0 Likes

(SmaTheGreek) #7

I have read the pytho documention but what I want is a very simple example at which the values of a c++ app are modified by values generated from a python script (and the opposite)

0 Likes

(Duoas) #8

http://www.python.org/doc/ext/extending-with-embedding.html
You are not going to get a simpler example than provided here. Sorry.

[edit] Sorry, to be less terse: you’ll need to create some routines that pass values to/from the interpreter. For example, in the Blender API, there are a ton of GetThis() and SetThat() routines, which do exactly [ed]the kind of thing[/ed] you want. [ed]For example: Blender.Get(‘curframe’) gets the blender internal variable for the current frame and returns it to the script.[/ed]

Hope this helps.[/edit]

0 Likes

(Inmare) #9

Exactly that is covered in the documentation behind that link.

0 Likes

(MrRage) #10

I was going to post this earlier and I guess by now you probably don’t need it but section 5.3 of the document did have an example that did what you wanted.

http://www.python.org/doc/ext/pure-embedding.html

Added:
Woops… 5.4 is what you were trying to do and someone already posted that.

0 Likes