WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 06-03-2016, 01:38 PM
Ciccio Ciccio is offline
Member
 
Join Date: Jun 2016
Posts: 6
Import viz from c++

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;
}
Reply With Quote
 

Tags
c++, import, vizard 5.0

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to draw EyeLink drift correction to screen chris2307 Vizard 6 08-18-2014 02:22 PM
Problems with using WIRKS without the Kinect in a mirror setting Jennifer Vizard 0 06-03-2013 03:17 PM
Import 3DS to Vizard suwan116 Vizard 1 05-09-2011 01:28 PM
Best way to import map data from 3ds Max v-Salik Vizard 6 04-09-2010 04:40 AM
Pick porblem with arrays shivanangel Vizard 3 08-27-2007 10:10 AM


All times are GMT -7. The time now is 01:57 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC