Hello,
I wrote my Vizard (5.0) program in py and every work perfectly. Right now, I'm trying to write a program c++ that load the python code and call all the different functions that I wrote (using Python.h).
Everything works fine if I don't import any vizard module but if in my python code I have one or more import viz (or related) the python return this error:
"viz module can only be imported by Vizard".
Can anyone help me?
this is my code python:
Code:
import sys
sys.path.append('C:/Program Files/WorldViz/Vizard5/python')
import viz
def add(a, b):
return a+b
and this is my main c++
Code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
static const char* PLUGIN_NAME = "Sample";
void PrintTotalRefCount()
{
#ifdef Py_REF_DEBUG
PyObject* refCount = PyObject_CallObject(PySys_GetObject((char*)"gettotalrefcount"), NULL);
std::clog << "total refcount = " << PyInt_AsSsize_t(refCount) << std::endl;
Py_DECREF(refCount);
#endif
}
std::string CallPlugIn(const std::string& ln)
{
PyObject* name = PyString_FromString(PLUGIN_NAME);
PyObject* pluginModule = PyImport_Import(name);
Py_DECREF(name);
if (!pluginModule)
{
PyErr_Print();
return "Error importing module";
}
PyObject* filterFunc = PyObject_GetAttrString(pluginModule, "filterFunc");
Py_DECREF(pluginModule);
if (!filterFunc)
{
PyErr_Print();
return "Error retrieving 'filterFunc'";
}
PyObject* args = Py_BuildValue("(s)", ln.c_str());
if (!args)
{
PyErr_Print();
Py_DECREF(filterFunc);
return "Error building args tuple";
}
PyObject* resultObj = PyObject_CallObject(filterFunc, args);
Py_DECREF(filterFunc);
Py_DECREF(args);
if (!resultObj)
{
PyErr_Print();
return "Error invoking 'filterFunc'";
}
const char* resultStr = PyString_AsString(resultObj);
if (!resultStr)
{
PyErr_Print();
Py_DECREF(resultObj);
return "Error converting result to C string";
}
std::string result = resultStr;
Py_DECREF(resultObj);
return result;
}
int main(int argc, char* argv[])
{
Py_Initialize();
PyObject* sysPath = PySys_GetObject((char*)"path");
PyObject* curDir = PyString_FromString(".");
PyList_Append(sysPath, curDir);
Py_DECREF(curDir);
std::string input;
CallPlugIn(input);
PrintTotalRefCount();
Py_Finalize();
system("PAUSE");
return 0;
}