PYXVR

From VRwiki

Jump to: navigation, search


Contents

Overview

Updated Project page is now on: PYXVR@Teslacore

PYXVR is an extension module for XVR that allows to execute Python scripts inside XVR. This wiki is all about XVR and if more information are needed please consult What is XVR? and the ShowCase.

XVR is a scriptable Virtual Reality Engine that runs inside a Web Page and it allows the fast development of both Desktop VR applicatoins and Immersive VR applications in which advanced stereo visualization is enhanced by means of additional devices (like haptics in HapticWeb).



Example of Immersive XVR Application


The XVR engine is scripted using the custom S3D language that is an object oriented language with a Javascript-like syntax although it has dynamic typing, only polymorphic classes, a specialized vector type for manipulating numerical entities.

The PYXVR extends the capabilities of XVR by allowing the execution of Python code for various objectives:

  1. access to Python libraries (e.g. XML, filesystem, advanced networking through Twisted)
  2. multi-threading
  3. integration with existing Python code
  4. dynamic transformation and extension of the XVR application (plugins)
  5. debugging (not available for S3D)

The two scripts communicates each other, although is now possible to develop all the XVR application using Python. Existing source code in S3D can be leveraged by accessing it from the Python script.



Image:Pxyvrarchitecture.png

Demo

The minimal demo of PYXVR feature can be found (REMOVED), note that it requires the XVR plugin for Internet Explorer or FireFox 1.5

Eventually is possible to download here the standalone version. (in a previous release two files were missing)

Note: at the moment the demo is just a static cube displayed over a grid, with no animation, as shown in the code snippet.

XVR runs under Windows, but the PYXVR in standalone mode is able to run under Linux using WINE.

PYXVR Particle example under XGL

Details

PYXVR is implemented as an extension module for XVR that can be accessed offline and in the future it will be provided also for online usage.

Usage

Python is accessible through the class PythonEngine inside XVR, with thre available methods:

  • evalFile(filename) relative to the download area of XVR
  • call(ptyhon function name, p1, ... , pN)
  • eval(code) Evaluates Python code

Example:

py = PythonEngine();
py.evalFile("pyxvrapp.py");
py.call("OnInit");

The typical usage of the call function is to invoke Python during the OnXXXX events of XVR like OnFrame and OnTimer

From the Python side the XVR world is accessible through the global XVR object that exposes ALL the XVR functions. Alternatively is possible to use the pyxvr module to import the XVR constants, the most common functions.

Note: Python can access not only the native XVR functions (like SceneBegin) but also any script defined function

This is the minimal Python environment:

from pyxvr import *

mesh = None

def OnInit():
	global mesh
	mesh = CVmNewMesh("box.aam");  # construction of objects
	mesh.Normalize(1)
	SetCameraPosition([0,2,-10]);
	CameraSetTarget(0,0,0);
	
def OnFrame():
	global mesh
	SceneBegin();
	DrawGridPY(2);		# python
	mesh.Draw()
	glTranslate(0,0.5,0);	
	XVR.DrawGrid(3); 	# native XVR function not imported because it is in the Script
	SceneEnd();		

def DrawGridPY(n):	
	glLineWidth(n);
	
	glDisable(GL_LIGHTING);
	glColor(1,0.5,0.5);
	
	glBegin(GL_LINES);
	for i in range(-100,100,10):
		glVertex(i, 0,  100 );
		glVertex(i, 0, -100 );		
		glVertex( 100, 0, i );
		glVertex(-100, 0, i );
	glEnd();

The associated XVR script is:

 
#include <Script3d.h>
 
extern function PythonEngine;
var py;
 
function OnDownload(script)
{
	FileDownload("pyxvr.zip");
}
 
function OnInit(script)
{
	LoadModule( "pyxvr_0141.dll");	
	py = PythonEngine();
	py.evalFile("pyxvrapp.py");
	py.call("OnInit");
}
 
function OnFrame()
{
	py.call("OnFrame");
}
 
function DrawGrid(x)
{
  // ...
}

Type Exchange

The XVR and Python data type can be exchanged easily between the two engines:

From XVR:

  • string,bool,float,int are transformed in the equivalent Python types
  • array => Python list
  • vector => Python list of numbers (Conversion to array('f') is planned)
  • object => Python wrapper object that can be invoked by name
  • void => None

From Python:

  • string,bool, int, float => the equivalent XVR types
  • tuple and list => XVR vector if containing only numeric otherwise XVR array
  • array('f') or array('d') (planned) => XVR vector
  • None => void
  • other types are for the moment converted into string

Deployment

A PYXVR application is built around the PYXVR.ZIP (1.2MB) that contains the minimal Python interpreter:

  • python24.dll
  • msvcr71.dll (required python24.dll)
  • python24.zip (minimal python library)
  • zlib.pyd (for accessing the zip, although it could be removed)

The application is typically made of two files, a S3D script and the associated Python script, although for the S3D script is possible to use a stub that just invokes Python.

Performance

The invocation of XVR global functions is extremely efficient while for the moment the method invokation of XVR objects is performed by name.

Debugging

It is possible to debug the Python scripts in PYXVR using the Winpdb or the HAP Python Remote Debugger.

When using Winpdb the following two lines should be added to the Python script:

import rpdb2; rpdb2.start_embedded_debugger("Hello",True)

Where the first parameter is the password, and the second the flag to allow to disable encryption

If you need to use the GUI debugger remember also to install wxPython:

python lib\site-packages\winpdb.py -t

Image:Pyxvrdebugsm.PNG

And also debug1.png

Otherwise you can run the console debugger. This is the procedure.

Launch the script, for example using xvrglut or from the browser, and it will freeze waiting the connection with the debugger.

Launch the debugger:

python lib\site-packages\rpdb2.py -t

Set the password and connect:

> password Hello
> attach

Open Issues

There are still some open issues about PYXVR:

  1. Security for enabling its use on the Web
  2. Improving performance of XVR object invocation (now by name)
  3. Direct invocation of Python objects from XVR
  4. Reducing the data type exchange between languages by defining wrapping types for XVR vectors
  5. More testing with Multi-Threading and Stackless Python
  6. Reduce the download time when python24.dll or other libraries are installed. In the case of Linux use a cross-shared library allowing to use the installed Linux Python libpython24.so

Examples

PYXVR RSS

XVR currently do not provide a XML parser and moreover the code for parsing News Feeds should take into account the different formats (RSS and ATOM). The Universal Feed Parser is a very good module that handles all the aspects of RSS parsing. In this demo we have integrated such a module and provided to XVR.

The following snapshot shows a simple layout of the 20 most popular entries in Google Video coloured by relevance.

Image:Pyxvrrss.PNG

This application shows how Python is able to invoke methods of XVR classes. This is the Python code:

def feedparse(url, target):
	d = feedparser.parse(url);
	target.setTitle(d.feed.title);
	for x in d.entries:
		target.addEntry(x.title,x.link);
	return

While in the XVR script a Feed class is declared with the setTitle/addEntry methods. This is the invocation of feedparse:

 
feed = cFeed();
py.call("feedparse","http://video.google.com/videofeed?type=top100new&num=20",feed);	
OutputLN("Loaded Feed: ",feed.title);

PYXVR PhotoView

The PhotoView application written in XVR was able to load photos only if specified in the Web Page and it was not possible to use it for browsing photos in the local disk. The following snapshot is an example of the resulting application that locates photo on the disk using Python. Eventually it could be possible to generate the thumbnails using additional Python modules.

Image:Pyxvrphotoview.PNG

In this case the rendering is by S3D and Python provides functions for accessing files like the following:

def getFiles(path):
	return os.listdir(path);

def getGlob(path):
	return glob.glob(path);

def isDir(path):
	return os.path.isdir(path);
	
def getFileSize(path):
	return os.stat(path).st_size;

# useful for cached traversal
def getFilesCached(path):
	r = dircache.listdir(path);
	dircache.annotate(path,r);
	return r;

PYXVR Particles

PYXVR can be used for games or for making cool graphic effects.

In thic case the same application has been developed both in S3D and Python for comparing the performance. The update function of the particle dynamics involves only the scripting engine and it is independent to the XVR visualization.

The XVR version of this demo can be tried here

Image:Pyxvrparticle.png

A preliminary benchmarking with this application shows a different behavior in speed between the S3D version and the Python version. Note that the benchmarking is only taking into account the update of the particles and not the overhead caused by the call from Python to XVR, or the graphic rendering.

Evaluation for Particles' Update
Number of ParticlesS3DPYS3D WinePY WineJSAVM JS
5000-----53fps
1000023fps19fps20fps15fps12fps40fps
2000012fps10fps11fps8fps6fps35fps
400006fps5fps5fps4fps3fps17fps

Legend:

  • S3D is the native XVR code
  • PY is the PYXVR
  • S3D Wine is XVR running under linux using Wine
  • PY Wine is PYXVR running under linux using Wine (with the Windows Python DLL)
  • JS is the JSXVR a new experimental module for writing XVR application in JavaScript
  • AVM is the Adobe Virtual Machine. The new value for the benchmark is caused by using typed variables

TODO: test PYXVR using Psyco!

PYXVR Stackless

Stackless Python is a variant of Python that allows to define microthreads inside a Python thread that run cooperatively. It has been used extensively in a multiplayer game, and they could be used in VR applications for managing agents or other entities. In this demo we have used the Stackless Python to simulate a population of beings interacting each other.

The microthreads of Stackless are scheduled at every OnFrame and the execution is allowed for a certain number of Python instructions:

py.call("OnFrame",50);  // in the future estimate from time a good number of instructions!

The OnFrame function in Python schedules the microthreads:

def OnFrame(timeout):
  global last
  if last:
    last.insert()   # recover last microthread interrupted
  last = stackless.run(timeout)

The minimalist Agent has an internal loop that updates its position and changes its graphical representation, but it could have any behavior.

def Agent(id):
  life = random.randint(1,1000)
  pos = [random.random()*5-2.5,random.random()*3,0]
  vel = [random.random()*0.05-0.025,random.random()*0.04,0]
  for i in range(life):
      pos[0] = pos[0] + vel[0]
      pos[1] = pos[1] + vel[1]
      pos[2] = pos[2] + vel[2]
  XVR.SetAgentPosition(id, pos[0],pos[1],pos[2])
  schedule()

Image:Pyxvrlife.png

X3D for XVR (idea)

Currently XVR supports a subset of VRML97 and it do not cover its X3D evolution both in the VRML or XML representations. An interesting possibility for PYXVR is to provide a simplified loading of X3D scenes in one of the two formats. For the VRML representation it could be possible to use the vrml module from PyOpenGL while for the XML representation we are going to use the Python SAX parser.

  • Geometric Shapes => AAM model file and cVmNewMesh
  • Group, Transform => cVmObj
  • Text => cVmText

A feature needed to complement this loader is the AAM file generator, where AAM is the preferred mesh format used by XVR for the efficient representation of animated meshes. Such generator is useful also for applications that generate meshes, except in the case of dynamic deformations for which the performance penalty of the generation is not acceptable.

For example to create a cube mesh in PYXVR, and store it in the current directory:

from pyxvraam import writeAAM

vdata="""-0.5000 -0.5000 -0.5000 0.5000 -0.5000 -0.5000 -0.5000 0.5000 -0.5000 0.5000 0.5000 -0.5000 -0.5000 
 -0.5000 0.5000 0.5000 -0.5000 0.5000 -0.5000 0.5000 0.5000 0.5000 0.5000 0.5000"""
idata  ="""0 2 3 3 1 0 4 5 7 7 6 4 0 1 5 5 4 0 1 3 7 7 5 1 3 2 6 6 7 3 2 0 4 4 6 2"""
mat = { 'name':'wow','ambient': [1,1,1],'diffuse':[1,1,1],'specular':[0,0,0],'shininess':0,'transparency':0.5}
writeAAM("cube.aam", "Box01", [float(x) for x in vdata.split(' ')],[int(x) for x in idata.split(' ')],mat,True)

Another possibility of the AAM generation is for the construction of parametric meshes, a feature not available in the standard XVR. The following is an example of Twist GLSL shader applied over a mesh constructed at runtime using PYXVR here.

Image:Pyxvrtwist.PNG

EuroPython 2006

Title: Dual Scripting in a Virtual Reality Engine. Embedding Python in XVR

Content

In this talk we present the embedding of Python inside the eXtreme Virtual
Reality (XVR) Engine. XVR is an advanced and lightweight system for the development
of Virtual Reality applications both for the Web and for 3D Immersive systems. 

Such system has been extensively used in European Projects for the construction of
high quality 3D graphic applications enhanced with VR devices. XVR applications are
written in the S3D scripting language and compiled into a bytecode representation
executed by the XVR Virtual Machine. The scripting language and the virtual machine
are specialized and optimized for the manipulation of 3D entities and efficient
object oriented programming.

The XVR system allows to develop and deploy 3D application easily by asking
the developer only to write the core rendering loop and place the application in a
Web page. The XVR Engine provides natively a set of classes for creating Virtual
Reality applications ranging from 3D graphics (both high level classes and low level
OpenGL functions), 3D sound and physics. Moreover it is possible to extend such
features with external native modules providing the support for new devices,
simulation engines or rendering techniques. At runtime the external modules are
loaded and their classes integrated in the system namespace.

PYXVR is a XVR extension that allows to embed a Python script inside a XVR
application or putting it in another way write a Virtual Reality application using
the XVR Engine features. Typically the XVR application loads the PYXVR extension,
executes the scripts and invokes Python in response of the rendering (OnFrame) and
animation (OnTimer) events of the XVR Engine. 

The result is a dual script environment in which the two scripting engines cooperate
for providing the best of the two worlds. The Python scripts are able to access all
the functions and classes defined by XVR (both scripted and native), while the XVR
script accesses the Python scripts through the eval and callback functions

There are some advantages in using Python inside a XVR application. First there is
the possibility of modifying and extending the application using the script, because
the XVR application is completely defined at compilation time. Then there is the
possibility of using the Python libraries and the multi-threading for enhancing the
XVR application. Nevertheless the advantages there are some drawbacks that should be
taken into account. First there is the aspect of security. XVR applications are
typically executed in the Web Browser and the XVR classes respect the security
requirements of this environment. The PYXVR extension allows the unrestricted import
of modules and for this reason it is currently limited to local XVR applications. The
second disadvantage of PYXVR for 3D Web application is the weight of the Python
engine whose core shared library is in the order of one megabyte, a size that is
equivalent to the whole XVR engine comprising the Virtual Reality features.
Relatively to the execution speed, the S3D scripts are faster than
the Python ones in managing objects and external entities because during the
off-line compilation of the script all the names and functions are known and encoded
in the byte-code. The simpler type space of S3D provides also additional
optimizations when dealing with 3D entities.

The talk discusses the overall PYXVR extension, the aspects of integration
between the two environments, the multi-threaded features and finally a performance
evaluation of this solution.

Concluding the PYXVR extension presented in this talk can be used by XVR
developers for enhancing their XVR applications and at the same time by Python
developers for fast prototyping Virtual Reality application.

Primary Authors

  • Mr. RUFFALDI, Emanuele (PERCRO Lab, Scuola S.Anna, Pisa) <pit@sssup.it> (PYXVR Developer)
  • Mr. APRILE, Walter (PERCRO Lab, Scuola S.Anna, Pisa)
  • Mr. BACINELLI, Sandro (PERCRO Lab, Scuola S.Anna, Pisa) (XVR VM and S3D Developer)
  • Dr. CARROZZINO, Marcello (PERCRO Lab, Scuola S.Anna, Pisa) (XVR Graphics)

Co-authors:

  • Dr. TECCHIA, Franco (SSSA, Pisa - Laboratorio PERCRO) (XVR Head)
  • Prof. BERGAMASCO, Massimo (SSSA, Pisa - Laboratorio PERCRO) (PERCRO Laboratory Head)

Presenter:

  • Mr. RUFFALDI, Emanuele

Track classification:

Python Language and Libraries Games and Entertainment

Contacts

Please for any question contact Emanuele Ruffaldi (pit@sssup.it) Homepage

Personal tools