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:
@@ -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
50
src/osg/ScriptEngine.cpp
Normal 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);
|
||||
}
|
||||
@@ -303,6 +303,7 @@ Registry::Registry()
|
||||
addFileExtensionAlias("frag", "glsl");
|
||||
addFileExtensionAlias("geom", "glsl");
|
||||
|
||||
addFileExtensionAlias("js", "V8");
|
||||
|
||||
#if defined(DARWIN_IMAGEIO)
|
||||
addFileExtensionAlias("jpg", "imageio");
|
||||
|
||||
@@ -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
|
||||
|
||||
13
src/osgPlugins/V8/CMakeLists.txt
Normal file
13
src/osgPlugins/V8/CMakeLists.txt
Normal 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)
|
||||
72
src/osgPlugins/V8/ReaderWriterV8.cpp
Normal file
72
src/osgPlugins/V8/ReaderWriterV8.cpp
Normal 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)
|
||||
82
src/osgPlugins/V8/V8ScriptEngine.cpp
Normal file
82
src/osgPlugins/V8/V8ScriptEngine.cpp
Normal 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();
|
||||
|
||||
}
|
||||
53
src/osgPlugins/V8/V8ScriptEngine.h
Normal file
53
src/osgPlugins/V8/V8ScriptEngine.h
Normal 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
|
||||
13
src/osgPlugins/lua/CMakeLists.txt
Normal file
13
src/osgPlugins/lua/CMakeLists.txt
Normal 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)
|
||||
51
src/osgPlugins/lua/LuaScriptEngine.cpp
Normal file
51
src/osgPlugins/lua/LuaScriptEngine.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
56
src/osgPlugins/lua/LuaScriptEngine.h
Normal file
56
src/osgPlugins/lua/LuaScriptEngine.h
Normal 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
|
||||
70
src/osgPlugins/lua/ReaderWriterLua.cpp
Normal file
70
src/osgPlugins/lua/ReaderWriterLua.cpp
Normal 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)
|
||||
13
src/osgPlugins/python/CMakeLists.txt
Normal file
13
src/osgPlugins/python/CMakeLists.txt
Normal 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)
|
||||
58
src/osgPlugins/python/PythonScriptEngine.cpp
Normal file
58
src/osgPlugins/python/PythonScriptEngine.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
52
src/osgPlugins/python/PythonScriptEngine.h
Normal file
52
src/osgPlugins/python/PythonScriptEngine.h
Normal 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
|
||||
70
src/osgPlugins/python/ReaderWriterPython.cpp
Normal file
70
src/osgPlugins/python/ReaderWriterPython.cpp
Normal 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)
|
||||
Reference in New Issue
Block a user