Passing C++ object to script Python
Hi, I have created a simple C++ class, and can successfully use in my python scripts. But my aim is to create a C++ instance, and pass it to my python script. The script will modify the instance and return it to the C++ application example : //---------------- example.h ---------------- #include <iostream> #include <string> class Personage { private: std::string m_name; int m_power; public: Personage(std::string name, int power); void setPower(); }; //---------------- example.cpp ---------------- #include "example.h" #include <Python.h> using namespace std;Personage::Personage(string name, int power) { m_name = name; m_power = power; } void Personage::setPower(int power) { m_power = power; } so, what should I do in the main to send an instance Personage to the script Python ? Any help would be appreciated
On 04/27/2012 08:07 AM, Yoann Chaumy wrote:
so, what should I do in the main to send an instance Personage to the script Python ?
Something like the following (assumes the Python interpreter is already properly initialized): void call_script(std::string const &script, Personage &p) { namespace bpl = boost::python; bpl::dict global; global["personage"] = p; // inject 'p' into script's environment bpl::exec_file(script.c_str(), global); // run it } HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin...
Hi, thank you Stefan, but a have buliding error : LINK : fatal error LNK1104: cannot open file 'boost_python-vc100-mt-1_48.lib' my first C++ program to execute my Python script was : // ------------ main.cpp ------------ #include <Python.h> #include <fstream> #include <iostream>using namespace std;int main() { int fin; //Test d'existance du fichier ifstream fichier("personnage.py"); if (fichier.fail()) { cout << "Fichier de script introuvable : " << "personnage.py" <<"\n"; return 0; } // open python script FILE* pyFile = fopen("personnage.py", "r"); Py_Initialize(); // Execute le script string exec ( "execfile(r\"" ); // note r for raw Python string. exec += "personnage.py" ; exec += "\")" ; PyRun_SimpleString( (char*) exec.c_str() ); cin >> fin; Py_Finalize(); return 0; } // ----------------------------------------- but now a have a building error with this code : // -------------- new main ------------- #include <Python.h> #include <fstream> #include <iostream> #include "../Boost_Python/personnage.h"#include <boost/python/module.hpp> #include <boost/python/def.hpp> #include <boost/python.hpp> using namespace std; void call_script(std::string const &script, Personnage &p) { namespace bpl = boost::python; bpl::dict global; global["personnage"] = p; // inject 'p' into script's environment bpl::exec_file(script.c_str(), global); // run it } int main() { int fin; //Test d'existance du fichier ifstream fichier("personnage.py"); if (fichier.fail()) { cout << "Fichier de script introuvable : " << "personnage.py" <<"\n"; return 0; } // Ouvre le script python a executer FILE* pyFile = fopen("personnage.py", "r"); Personnage p("toto", 500); Py_Initialize(); call_script("personnage.py", p); cin >> fin; Py_Finalize(); return 0; }My error is : LINK : fatal error LNK1104: cannot open file 'boost_python-vc100-mt-1_48.lib'I using VS2010 and have add C:\boost_1_48_0\stage\lib\boost_python-vc100-mt-1_48.lib to the additionnal Library Directories
participants (2)
-
Stefan Seefeld -
Yoann Chaumy