Added initial cut of basic scripting support, introducing an osg::Script object to wrap up the individual scripts, osg::ScriptCallback for assigning a Script as node callback and an osg::ScriptEngine base class that plugins implement to provided support for specific scripting languages.

Provided are lua, python and V8 (for javascript) plugins that just open up enough of a link to the respective libs to run a script, there is no scene graph <-> script communication in current implementation.
This commit is contained in:
Robert Osfield 2013-08-09 16:51:06 +00:00
parent babd6e0ee7
commit bd7ec9c5f7
21 changed files with 849 additions and 244 deletions

View File

@ -539,6 +539,9 @@ ELSE()
FIND_PACKAGE(Asio)
ENDIF()
FIND_PACKAGE(ZeroConf)
FIND_PACKAGE(Lua51)
FIND_PACKAGE(V8)
FIND_PACKAGE(PythonLibs)
ENDIF()
IF(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 8)

View File

@ -1,45 +1,3 @@
FIND_PACKAGE(Lua51)
FIND_PACKAGE(V8)
FIND_PACKAGE(PythonLibs)
IF (LUA_LIBRARIES AND LUA_INCLUDE_DIR)
SET(LUA_FOUND True)
ENDIF()
IF (V8_FOUND)
ADD_DEFINITIONS(-DUSE_V8)
SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${V8_LIBRARY})
INCLUDE_DIRECTORIES(${V8_INCLUDE_DIR} )
MESSAGE("We have found V8")
ELSE()
MESSAGE("We have NOT found V8")
ENDIF()
IF (LUA_FOUND)
ADD_DEFINITIONS(-DUSE_LUA)
SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${LUA_LIBRARY})
INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR} )
MESSAGE("We have found Lua")
ELSE()
MESSAGE("We have NOT found Lua")
ENDIF()
IF (PYTHONLIBS_FOUND)
MESSAGE("We have found Python")
SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${PYTHON_LIBRARY})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIR} )
ADD_DEFINITIONS(-DUSE_PYTHON)
ELSE()
MESSAGE("We have NOT found Python")
ENDIF()
SET(TARGET_SRC osgpresentation.cpp )
#### end var setup ###

View File

@ -6,6 +6,8 @@
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/ScriptEngine>
#include <osg/UserDataContainer>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
@ -13,162 +15,6 @@
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
// hacky experiment with create lua, v8 and python to run scripts.
#ifdef USE_LUA
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
void runLua(const std::string& str)
{
OSG_NOTICE<<std::endl<<"runLua("<<str<<")"<<std::endl;
lua_State* lua = lua_open();
luaL_openlibs(lua);
std::string filePath = str;
if(osgDB::fileExists(filePath))
{
if(luaL_dofile(lua, filePath.c_str()))
{
OSG_NOTICE << "LuaEngine::runFile - " << lua_tostring(lua, -1) << std::endl;
}
}
else
{
OSG_NOTICE<<" parameter="<<filePath<<" not a valid file."<<std::endl;
}
lua_close(lua);
}
#endif
#ifdef USE_PYTHON
#include <Python.h>
void runPython(const std::string& str)
{
OSG_NOTICE<<std::endl<<"runPython("<<str<<")"<<std::endl;
Py_InitializeEx(0);
PyObject* py_main = PyModule_GetDict(PyImport_AddModule("__main__"));
std::string filePath = str;
if(osgDB::fileExists(filePath))
{
FILE* f = fopen(filePath.c_str(), "r");
PyObject* r = PyRun_File(f, filePath.c_str(), Py_file_input, py_main, py_main);
fclose(f);
if(!r) {
r = PyErr_Occurred();
if(r) {
// The following snippet lets us get the return code. That is: if the
// script is stopped with sys.exit() or similar. We could use this
// return code to do something sensible... later.
if(PyErr_ExceptionMatches(PyExc_SystemExit)) {
PyObject* ty = 0;
PyObject* er = 0;
PyObject* tr = 0;
PyErr_Fetch(&ty, &er, &tr);
Py_DECREF(ty);
Py_DECREF(er);
Py_DECREF(er);
}
else {
PyErr_Print();
PyErr_Clear();
}
}
}
}
Py_Finalize();
}
#endif
#ifdef USE_V8
#include <v8.h>
void runV8(const std::string& str)
{
OSG_NOTICE<<std::endl<<"runV8("<<str<<")"<<std::endl;
v8::Isolate* isolate = v8::Isolate::New();
{
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope;
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
v8::Persistent<v8::Context> globalContext = v8::Context::New(NULL, global);
v8::Persistent<v8::ObjectTemplate> globalTemplate = v8::Persistent<v8::ObjectTemplate>::New(global);
std::string filePath = str;
if(osgDB::fileExists(filePath))
{
std::string js_source;
std::ifstream fin(filePath.c_str());
if (fin)
{
// read file in the crude way.
while(fin)
{
int c = fin.get();
if (c>=0 && c<=255)
{
js_source.push_back(c);
}
}
// Create a nested handle scope
v8::HandleScope local_handle_scope;
// Enter the global context
v8::Context::Scope context_scope(globalContext);
// Create a string containing the JavaScript source code.
v8::Handle<v8::String> source = v8::String::New(js_source.c_str());
// Compile the source code.
v8::Handle<v8::Script> script = v8::Script::Compile(source);
// Run the script to get the result.
v8::Handle<v8::Value> result = script->Run();
// Convert the result to an ASCII string and print it.
v8::String::AsciiValue ascii(result);
printf("%s\n", *ascii);
}
}
globalTemplate.Dispose();
globalContext.Dispose();
}
isolate->Dispose();
}
#endif
int main(int argc, char** argv)
{
@ -176,33 +22,77 @@ int main(int argc, char** argv)
osgViewer::Viewer viewer(arguments);
std::string str;
typedef std::list< osg::ref_ptr<osg::Script> > Scripts;
Scripts scripts;
#ifdef USE_LUA
while (arguments.read("--lua",str))
std::string filename;
while(arguments.read("--script",filename))
{
runLua(str);
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(filename);
if (script.valid()) scripts.push_back(script.get());
}
#endif
#ifdef USE_V8
while (arguments.read("--js",str))
{
runV8(str);
}
#endif
#ifdef USE_PYTHON
while (arguments.read("--python",str))
{
runPython(str);
}
#endif
// create the model
osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments);
if (!model) return 1;
// assgin script engine to scene graphs
model->getOrCreateUserDataContainer()->addUserObject(osgDB::readFile<osg::ScriptEngine>("ScriptEngine.lua"));
model->getOrCreateUserDataContainer()->addUserObject(osgDB::readFile<osg::ScriptEngine>("ScriptEngine.python"));
model->getOrCreateUserDataContainer()->addUserObject(osgDB::readFile<osg::ScriptEngine>("ScriptEngine.js"));
// assign scripts to scene graph
for(Scripts::iterator itr = scripts.begin();
itr != scripts.end();
++itr)
{
model->addUpdateCallback(new osg::ScriptCallback(itr->get()));
}
#if 0
std::string str;
osg::ref_ptr<osg::ScriptEngine> luaScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.lua");
if (luaScriptEngine.valid())
{
while (arguments.read("--lua", str))
{
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(str);
if (script.valid())
{
luaScriptEngine->run(script.get());
}
}
}
osg::ref_ptr<osg::ScriptEngine> v8ScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.V8");
if (v8ScriptEngine.valid())
{
while (arguments.read("--js",str))
{
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(str);
if (script.valid())
{
v8ScriptEngine->run(script.get());
}
}
}
osg::ref_ptr<osg::ScriptEngine> pythonScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.python");
if (pythonScriptEngine.valid())
{
while (arguments.read("--python",str))
{
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(str);
if (script.valid())
{
pythonScriptEngine->run(script.get());
}
}
}
#endif
viewer.setSceneData( model.get() );
return viewer.run();
}

View File

@ -1,27 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_CAMERANODE
#define OSG_CAMERANODE 1
#include <osg/Camera>
namespace osg {
#ifdef USE_DEPRECATED_API
typedef osg::Camera CameraNode;
#endif
}
#endif

108
include/osg/ScriptEngine Normal file
View File

@ -0,0 +1,108 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_SCRIPTENGINE
#define OSG_SCRIPTENGINE 1
#include <osg/Object>
#include <osg/NodeCallback>
#include <osg/NodeVisitor>
namespace osg
{
// forward declare
class ScriptEngine;
/* Script class for wrapping a script and the language used in the script.*/
class Script : public osg::Object
{
public:
Script():_modifiedCount(0) {}
Script(const std::string& language, const std::string& str): _language(language), _script(str), _modifiedCount(0) {}
Script(const Script& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): _language(rhs._language), _script(rhs._script), _modifiedCount(0) {}
META_Object(osg, Script)
void setLanguage(const std::string& language) { _language = language; dirty(); }
const std::string& getLanguage() { return _language; }
void setScript(const std::string& str) { _script = str; dirty(); }
const std::string& getScript() const { return _script; }
void dirty() { ++_modifiedCount; }
unsigned int getModifiedCount() const { return _modifiedCount; }
protected:
virtual ~Script() {}
std::string _language;
std::string _script;
unsigned int _modifiedCount;
};
/** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/
class OSG_EXPORT ScriptCallback : public osg::NodeCallback
{
public:
ScriptCallback(Script* script=0) : _script(script) {}
ScriptCallback(const ScriptCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::NodeCallback(rhs,copyop), _script(rhs._script) {}
META_Object(osg, ScriptCallback)
/** Set the script to call.*/
void setScript(osg::Script* script) { _script = script; }
/** Get the script to call.*/
osg::Script* getScript() { return _script.get(); }
/** Get the script to call.*/
const osg::Script* getScript() const { return _script.get(); }
/** find the ScriptEngine from looking at the UserDataContainers of nodes in scene graph above the ScriptCallback.*/
osg::ScriptEngine* getScriptEngine(osg::NodePath& nodePath);
/** NodeCallback method, calls the Script.*/
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
protected:
virtual ~ScriptCallback() {}
osg::ref_ptr<Script> _script;
};
/** ScriptEngine base class for integrating different scripting languages.
* Concrete ScriptEngine's are provided by osgDB::readFile<ScriptEngine> */
class ScriptEngine : public osg::Object
{
public:
/** get the scripting language supported by the ScriptEngine.*/
inline const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script) = 0;
protected:
ScriptEngine(const std::string& language):_language(language) { setName(language); }
virtual ~ScriptEngine() {}
std::string _language;
};
}
#endif

View File

@ -137,6 +137,7 @@ SET(TARGET_H
${HEADER_PATH}/RenderInfo
${HEADER_PATH}/SampleMaski
${HEADER_PATH}/Scissor
${HEADER_PATH}/ScriptEngine
${HEADER_PATH}/Sequence
${HEADER_PATH}/ShadeModel
${HEADER_PATH}/Shader
@ -323,6 +324,7 @@ SET(TARGET_SRC
Referenced.cpp
SampleMaski.cpp
Scissor.cpp
ScriptEngine.cpp
Sequence.cpp
ShadeModel.cpp
Shader.cpp

50
src/osg/ScriptEngine.cpp Normal file
View File

@ -0,0 +1,50 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/ScriptEngine>
#include <osg/UserDataContainer>
using namespace osg;
ScriptEngine* ScriptCallback::getScriptEngine(osg::NodePath& nodePath)
{
if (!_script) return 0;
for(osg::NodePath::reverse_iterator itr = nodePath.rbegin();
itr != nodePath.rend();
++itr)
{
osg::Node* node = *itr;
osg::UserDataContainer* udc = node->getUserDataContainer();
if (udc)
{
ScriptEngine* engine = dynamic_cast<ScriptEngine*>(udc->getUserObject(_script->getLanguage()));
if (engine) return engine;
}
}
return 0;
}
void ScriptCallback::operator()(Node* node, NodeVisitor* nv)
{
ScriptEngine* engine = getScriptEngine(nv->getNodePath());
if (engine && _script.valid())
{
engine->run(_script.get());
}
// note, callback is responsible for scenegraph traversal so
// they must call traverse(node,nv) to ensure that the
// scene graph subtree (and associated callbacks) are traversed.
traverse(node,nv);
}

View File

@ -303,6 +303,7 @@ Registry::Registry()
addFileExtensionAlias("frag", "glsl");
addFileExtensionAlias("geom", "glsl");
addFileExtensionAlias("js", "V8");
#if defined(DARWIN_IMAGEIO)
addFileExtensionAlias("jpg", "imageio");

View File

@ -258,6 +258,23 @@ ADD_SUBDIRECTORY(pvr)
ADD_SUBDIRECTORY(osc)
ADD_SUBDIRECTORY(trk)
####################################################
#
# ScriptEngines
#
IF (LUA_LIBRARIES AND LUA_INCLUDE_DIR)
ADD_SUBDIRECTORY(lua)
ENDIF()
IF (V8_FOUND)
ADD_SUBDIRECTORY(V8)
ENDIF()
IF (PYTHONLIBS_FOUND)
ADD_SUBDIRECTORY(python)
ENDIF()
####################################################
#
# Device integration plugins

View File

@ -0,0 +1,13 @@
SET(TARGET_H
V8ScriptEngine.h
)
SET(TARGET_SRC
V8ScriptEngine.cpp
ReaderWriterV8.cpp
)
SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${V8_LIBRARY})
INCLUDE_DIRECTORIES(${V8_INCLUDE_DIR} )
#### end var setup ###
SETUP_PLUGIN(v8)

View File

@ -0,0 +1,72 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/Registry>
#include "V8ScriptEngine.h"
class ReaderWriterV8 : public osgDB::ReaderWriter
{
public:
ReaderWriterV8()
{
supportsExtension("v8","JavaScript");
supportsExtension("js","JavaScript");
}
virtual const char* className() const { return "V8 JavaScript ScriptEngine plugin"; }
virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const
{
osg::ref_ptr<osg::Script> script = new osg::Script;
script->setLanguage("js");
std::string str;
while(fin)
{
int c = fin.get();
if (c>=0 && c<=255)
{
str.push_back(c);
}
}
script->setScript(str);
return script.release();
}
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
{
if (file=="ScriptEngine.V8") return new v8::V8ScriptEngine();
if (file=="ScriptEngine.js") return new v8::V8ScriptEngine();
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( file, options );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
osgDB::ifstream istream(fileName.c_str(), std::ios::in);
if(!istream) return ReadResult::FILE_NOT_HANDLED;
return readObject(istream, options);
}
};
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(v8, ReaderWriterV8)

View File

@ -0,0 +1,82 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include "V8ScriptEngine.h"
using namespace v8;
V8ScriptEngine::V8ScriptEngine():
osg::ScriptEngine("js"),
_isolate(0)
{
initialize();
}
V8ScriptEngine::V8ScriptEngine(const V8ScriptEngine& rhs, const osg::CopyOp&):
osg::ScriptEngine("js"),
_isolate(0)
{
initialize();
}
V8ScriptEngine::~V8ScriptEngine()
{
_isolate->Dispose();
_isolate = 0;
}
void V8ScriptEngine::initialize()
{
_isolate = v8::Isolate::New();
}
void V8ScriptEngine::run(osg::Script* script)
{
if (!script || !_isolate) return;
v8::Locker locker(_isolate);
v8::Isolate::Scope isolate_scope(_isolate);
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope;
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
_globalContext = v8::Context::New(NULL, global);
_globalTemplate = v8::Persistent<v8::ObjectTemplate>::New(global);
{
// Create a nested handle scope
v8::HandleScope local_handle_scope;
// Enter the global context
v8::Context::Scope context_scope(_globalContext);
// Create a string containing the JavaScript source code.
v8::Handle<v8::String> source = v8::String::New(script->getScript().c_str());
// Compile the source code.
v8::Handle<v8::Script> v8_script = v8::Script::Compile(source);
// Run the script to get the result.
v8::Handle<v8::Value> result = v8_script->Run();
// Convert the result to an ASCII string and print it.
v8::String::AsciiValue ascii(result);
printf("%s\n", *ascii);
}
_globalTemplate.Dispose();
_globalContext.Dispose();
}

View File

@ -0,0 +1,53 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef V8SCRIPTENGINE_H
#define V8SCRIPTENGINE_H
#include <osg/ScriptEngine>
#include <v8.h>
namespace v8
{
class V8ScriptEngine : public osg::ScriptEngine
{
public:
V8ScriptEngine();
V8ScriptEngine(const V8ScriptEngine& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(v8, V8ScriptEngine)
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script);
v8::Isolate* getIsolate() { return _isolate; }
protected:
void initialize();
virtual ~V8ScriptEngine();
v8::Isolate* _isolate;
v8::Persistent<v8::Context> _globalContext;
v8::Persistent<v8::ObjectTemplate> _globalTemplate;
};
}
#endif

View File

@ -0,0 +1,13 @@
SET(TARGET_H
LuaScriptEngine.h
)
SET(TARGET_SRC
LuaScriptEngine.cpp
ReaderWriterLua.cpp
)
SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${LUA_LIBRARY})
INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR} )
#### end var setup ###
SETUP_PLUGIN(lua)

View File

@ -0,0 +1,51 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include "LuaScriptEngine.h"
using namespace lua;
LuaScriptEngine::LuaScriptEngine():
osg::ScriptEngine("lua"),
_lua(0)
{
initialize();
}
LuaScriptEngine::LuaScriptEngine(const LuaScriptEngine& rhs, const osg::CopyOp&):
osg::ScriptEngine("lua"),
_lua(0)
{
initialize();
}
LuaScriptEngine::~LuaScriptEngine()
{
lua_close(_lua);
}
void LuaScriptEngine::initialize()
{
_lua = lua_open();
luaL_openlibs(_lua);
}
void LuaScriptEngine::run(osg::Script* script)
{
if (!script || !_lua) return;
if (luaL_dostring(_lua, script->getScript().c_str()))
{
OSG_NOTICE << "LuaScriptEngine::run(Script*) error: " << lua_tostring(_lua, -1) << std::endl;
}
}

View File

@ -0,0 +1,56 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef LUASCRIPTENGINE_H
#define LUASCRIPTENGINE_H
#include <osg/ScriptEngine>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
namespace lua
{
class LuaScriptEngine : public osg::ScriptEngine
{
public:
LuaScriptEngine();
LuaScriptEngine(const LuaScriptEngine& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(lua, LuaScriptEngine)
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script);
/** get the lua_State object.*/
lua_State* getLuaState() { return _lua; }
protected:
void initialize();
virtual ~LuaScriptEngine();
lua_State* _lua;
};
}
#endif

View File

@ -0,0 +1,70 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/Registry>
#include "LuaScriptEngine.h"
class ReaderWriterLua : public osgDB::ReaderWriter
{
public:
ReaderWriterLua()
{
supportsExtension("lua","lua script");
}
virtual const char* className() const { return "Lua ScriptEngine plugin"; }
virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const
{
osg::ref_ptr<osg::Script> script = new osg::Script;
script->setLanguage("lua");
std::string str;
while(fin)
{
int c = fin.get();
if (c>=0 && c<=255)
{
str.push_back(c);
}
}
script->setScript(str);
return script.release();
}
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
{
if (file=="ScriptEngine.lua") return new lua::LuaScriptEngine();
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( file, options );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
osgDB::ifstream istream(fileName.c_str(), std::ios::in);
if(!istream) return ReadResult::FILE_NOT_HANDLED;
return readObject(istream, options);
}
};
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(lua, ReaderWriterLua)

View File

@ -0,0 +1,13 @@
SET(TARGET_H
PythonScriptEngine.h
)
SET(TARGET_SRC
PythonScriptEngine.cpp
ReaderWriterPython.cpp
)
SET(TARGET_EXTERNAL_LIBRARIES ${TARGET_EXTERNAL_LIBRARIES} ${PYTHON_LIBRARY})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIR} )
#### end var setup ###
SETUP_PLUGIN(python)

View File

@ -0,0 +1,58 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include "PythonScriptEngine.h"
using namespace python;
PythonScriptEngine::PythonScriptEngine():
osg::ScriptEngine("python"),
_py_main(0)
{
initialize();
}
PythonScriptEngine::PythonScriptEngine(const PythonScriptEngine& rhs, const osg::CopyOp&):
osg::ScriptEngine("python"),
_py_main(0)
{
initialize();
}
PythonScriptEngine::~PythonScriptEngine()
{
Py_Finalize();
}
void PythonScriptEngine::initialize()
{
Py_InitializeEx(0);
_py_main = PyModule_GetDict(PyImport_AddModule("__main__"));
}
void PythonScriptEngine::run(osg::Script* script)
{
if (!script || !_py_main) return;
PyObject* r = PyRun_String(script->getScript().c_str(), Py_file_input, _py_main, _py_main);
if(!r) {
r = PyErr_Occurred();
if(r) {
PyErr_Print();
PyErr_Clear();
}
}
}

View File

@ -0,0 +1,52 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef PYTHONSCRIPTENGINE_H
#define PYTHONSCRIPTENGINE_H
#include <osg/ScriptEngine>
#include <Python.h>
namespace python
{
class PythonScriptEngine : public osg::ScriptEngine
{
public:
PythonScriptEngine();
PythonScriptEngine(const PythonScriptEngine& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(lua, PythonScriptEngine)
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script);
/** get the Python main object.*/
PyObject* getPythonMain() { return _py_main; }
protected:
void initialize();
virtual ~PythonScriptEngine();
PyObject* _py_main;
};
}
#endif

View File

@ -0,0 +1,70 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/Registry>
#include "PythonScriptEngine.h"
class ReaderWriterPython : public osgDB::ReaderWriter
{
public:
ReaderWriterPython()
{
supportsExtension("python","python script");
}
virtual const char* className() const { return "Python ScriptEngine plugin"; }
virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const
{
osg::ref_ptr<osg::Script> script = new osg::Script;
script->setLanguage("python");
std::string str;
while(fin)
{
int c = fin.get();
if (c>=0 && c<=255)
{
str.push_back(c);
}
}
script->setScript(str);
return script.release();
}
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
{
if (file=="ScriptEngine.python") return new python::PythonScriptEngine();
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( file, options );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
osgDB::ifstream istream(fileName.c_str(), std::ios::in);
if(!istream) return ReadResult::FILE_NOT_HANDLED;
return readObject(istream, options);
}
};
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(python, ReaderWriterPython)