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:
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