Added support for passing parameters in and out of scripts.

Implemented support for osg::ValueObject in LuaScriptEngine.
This commit is contained in:
Robert Osfield 2013-09-24 15:17:42 +00:00
parent 796314c339
commit 6d8f40d13a
10 changed files with 397 additions and 24 deletions

View File

@ -146,12 +146,13 @@ int main(int argc, char** argv)
presentation->setProperty("scale",1.0);
#if 0
osgPresentation::PrintSupportedProperties psp(std::cout);
presentation->accept(psp);
osgPresentation::PrintProperties pp(std::cout);
presentation->accept(pp);
#endif
osgPresentation::LoadAction load;
presentation->accept( load );
@ -223,8 +224,13 @@ int main(int argc, char** argv)
#endif
}
#endif
presentation->setName("[this is a test]");
#if 0
if (pi.setProperty(presentation.get(), "Name", std::string("[this is new improved test]")))
{
OSG_NOTICE<<"setProperty(presentation.get(), Name) succeeded."<<std::endl;
@ -402,7 +408,6 @@ int main(int argc, char** argv)
PRINT_TYPE(presentation.get(), mystring)
PRINT_TYPE(presentation.get(), mymatrix)
osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter;
if (pi.getSupportedProperties(event.get(), properties, true))
{
@ -418,10 +423,52 @@ int main(int argc, char** argv)
{
OSG_NOTICE<<"No supported properites found."<<std::endl;
}
#endif
osg::ref_ptr<osg::ScriptEngine> luaScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.lua");
if (luaScriptEngine.valid())
{
presentation->getOrCreateUserDataContainer()->addUserObject(luaScriptEngine.get());
std::string str;
while (arguments.read("--lua", str))
{
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(str);
if (script.valid())
{
presentation->addUpdateCallback(new osg::ScriptCallback(script.get(),"doStuff"));
}
}
// return viewer.run();
if (arguments.read("--test", str))
{
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(str);
if (script.valid())
{
osg::ScriptEngine::Parameters inputParameters;
osg::ScriptEngine::Parameters outputParameters;
inputParameters.push_back(new osg::StringValueObject("string","my very first string input"));
inputParameters.push_back(new osg::DoubleValueObject("double",1.234));
inputParameters.push_back(new osg::MatrixfValueObject("matrix",osg::Matrixf()));
osg::ref_ptr<osg::MatrixdValueObject> svo = new osg::MatrixdValueObject("return", osg::Matrixd());
outputParameters.push_back(svo.get());
if (luaScriptEngine->run(script.get(), "test", inputParameters, outputParameters))
{
OSG_NOTICE<<"Successfully ran script : return value = "<<svo->getValue()<<std::endl;
}
else
{
OSG_NOTICE<<"script run failed"<<std::endl;
}
}
}
}
return viewer.run();
}

View File

@ -56,7 +56,7 @@ class Script : public osg::Object
class OSG_EXPORT ScriptCallback : public osg::NodeCallback
{
public:
ScriptCallback(Script* script=0) : _script(script) {}
ScriptCallback(Script* script=0, const std::string& entryPoint="") : _script(script), _entryPoint(entryPoint) {}
ScriptCallback(const ScriptCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::NodeCallback(rhs,copyop), _script(rhs._script) {}
META_Object(osg, ScriptCallback)
@ -80,7 +80,8 @@ class OSG_EXPORT ScriptCallback : public osg::NodeCallback
virtual ~ScriptCallback() {}
osg::ref_ptr<Script> _script;
osg::ref_ptr<Script> _script;
std::string _entryPoint;
};
/** ScriptEngine base class for integrating different scripting languages.
@ -92,8 +93,18 @@ class ScriptEngine : public osg::Object
/** get the scripting language supported by the ScriptEngine.*/
inline const std::string& getLanguage() const { return _language; }
typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
/** run a Script.*/
virtual void run(osg::Script* script) = 0;
bool run(osg::Script* script)
{
// assumpt empty input and output paramters lists
Parameters inputParameters, outputParameters;
return run(script, "", inputParameters, outputParameters);
}
/** run a Script.*/
virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters) = 0;
protected:

View File

@ -40,7 +40,23 @@ void ScriptCallback::operator()(Node* node, NodeVisitor* nv)
ScriptEngine* engine = getScriptEngine(nv->getNodePath());
if (engine && _script.valid())
{
engine->run(_script.get());
// To handle the case where a NodeVisitor is created on the stack and can't be automatically ref counted
// we take a reference to prevent the inputParameters reference to the NodeVisitor making it's ref count going to zero and causing a delete.
ref_ptr<NodeVisitor> ref_nv(nv);
{
ScriptEngine::Parameters inputParameters;
inputParameters.push_back(node);
inputParameters.push_back(nv);
// empty outputParameters
ScriptEngine::Parameters outputParameters;
engine->run(_script.get(), _entryPoint, inputParameters, outputParameters);
}
// now release the ref_ptr used to protected the NodeVisitor from deletion.
ref_nv.release();
}
// note, callback is responsible for scenegraph traversal so

View File

@ -41,9 +41,9 @@ void V8ScriptEngine::initialize()
}
void V8ScriptEngine::run(osg::Script* script)
bool V8ScriptEngine::run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters)
{
if (!script || !_isolate) return;
if (!script || !_isolate) return false;
v8::Locker locker(_isolate);
v8::Isolate::Scope isolate_scope(_isolate);
@ -75,8 +75,9 @@ void V8ScriptEngine::run(osg::Script* script)
v8::String::AsciiValue ascii(result);
printf("%s\n", *ascii);
}
_globalTemplate.Dispose();
_globalContext.Dispose();
return true;
}

View File

@ -32,7 +32,7 @@ class V8ScriptEngine : public osg::ScriptEngine
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script);
virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters);
v8::Isolate* getIsolate() { return _isolate; }
@ -50,4 +50,4 @@ class V8ScriptEngine : public osg::ScriptEngine
}
#endif
#endif

View File

@ -12,6 +12,7 @@
*/
#include "LuaScriptEngine.h"
#include <osg/io_utils>
using namespace lua;
@ -40,12 +41,297 @@ void LuaScriptEngine::initialize()
luaL_openlibs(_lua);
}
void LuaScriptEngine::run(osg::Script* script)
bool LuaScriptEngine::loadScript(osg::Script* script)
{
if (!script || !_lua) return;
if (_loadedScripts.count(script)!=0) return true;
if (luaL_dostring(_lua, script->getScript().c_str()))
int loadResult = luaL_loadstring(_lua, script->getScript().c_str());
if (loadResult==0)
{
OSG_NOTICE << "LuaScriptEngine::run(Script*) error: " << lua_tostring(_lua, -1) << std::endl;
OSG_NOTICE<<"Loaded script"<<std::endl;
_loadedScripts.insert(script);
return true;
}
else
{
OSG_NOTICE << "LuaScriptEngine::luaL_loadstring(Script*) error: " << lua_tostring(_lua, -1) << std::endl;
return false;
}
}
bool LuaScriptEngine::run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters)
{
if (!script || !_lua) return false;
if (_loadedScripts.count(script)==0)
{
if (!loadScript(script)) return false;
if (lua_pcall(_lua, 0, 0, 0)!=0)
{
OSG_NOTICE<< "error initialize script "<< lua_tostring(_lua, -1)<<std::endl;
return false;
}
}
if (entryPoint.empty())
{
int callResult = lua_pcall(_lua, 0, LUA_MULTRET, 0);
if (callResult)
{
OSG_NOTICE << "LuaScriptEngine::call(Script*) error: " << lua_tostring(_lua, -1) << std::endl;
return false;
}
OSG_NOTICE << "Successful run " << std::endl;
return true;
}
else
{
lua_getfield(_lua, LUA_GLOBALSINDEX, entryPoint.c_str()); /* function to be called */
for(osg::ScriptEngine::Parameters::const_iterator itr = inputParameters.begin();
itr != inputParameters.end();
++itr)
{
pushParameter(itr->get());
}
if (lua_pcall(_lua, inputParameters.size(), outputParameters.size(),0)!=0)
{
OSG_NOTICE<<"Lua error : "<<lua_tostring(_lua, -1)<<std::endl;
return false;
}
for(osg::ScriptEngine::Parameters::const_iterator itr = outputParameters.begin();
itr != outputParameters.end();
++itr)
{
popParameter(itr->get());
}
return true;
}
return false;
}
class PushStackValueVisitor : public osg::ValueObject::GetValueVisitor
{
public:
lua_State* _lua;
PushStackValueVisitor(lua_State* lua) : _lua(lua) {}
inline void push(const char* str, double value)
{
lua_pushstring(_lua, str); lua_pushnumber(_lua, value); lua_settable(_lua, -3);
}
inline void pushElem(unsigned int i, double value)
{
lua_pushnumber(_lua, i); lua_pushinteger(_lua, value); lua_settable(_lua, -3);
}
virtual void apply(bool value) { lua_pushboolean(_lua, value ? 0 : 1); }
virtual void apply(char value) { lua_pushnumber(_lua, value); }
virtual void apply(unsigned char value) { lua_pushnumber(_lua, value); }
virtual void apply(short value) { lua_pushnumber(_lua, value); }
virtual void apply(unsigned short value) { lua_pushnumber(_lua, value); }
virtual void apply(int value) { lua_pushnumber(_lua, value); }
virtual void apply(unsigned int value) { lua_pushnumber(_lua, value); }
virtual void apply(float value) { lua_pushnumber(_lua, value); }
virtual void apply(double value) { lua_pushnumber(_lua, value); }
virtual void apply(const std::string& value) { lua_pushlstring(_lua, &value[0], value.size()); }
virtual void apply(const osg::Vec2f& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); }
virtual void apply(const osg::Vec3f& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); push("z", value.z()); }
virtual void apply(const osg::Vec4f& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); push("z", value.z()); push("w", value.w()); }
virtual void apply(const osg::Vec2d& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); }
virtual void apply(const osg::Vec3d& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); push("z", value.z()); }
virtual void apply(const osg::Vec4d& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); push("z", value.z()); push("w", value.w()); }
virtual void apply(const osg::Quat& value) { lua_newtable(_lua); push("x", value.x()); push("y", value.y()); push("z", value.z()); push("w", value.w()); }
virtual void apply(const osg::Plane& value) { lua_newtable(_lua); pushElem(0, value[0]); pushElem(1, value[1]); pushElem(2, value[2]); pushElem(3, value[3]); }
virtual void apply(const osg::Matrixf& value) { lua_newtable(_lua); for(unsigned int r=0; r<4; ++r) { for(unsigned int c=0; c<4; ++c) { pushElem(r*4+c, value(r,c)); } } }
virtual void apply(const osg::Matrixd& value) { lua_newtable(_lua); for(unsigned int r=0; r<4; ++r) { for(unsigned int c=0; c<4; ++c) { pushElem(r*4+c, value(r,c)); } } }
};
class GetStackValueVisitor : public osg::ValueObject::SetValueVisitor
{
public:
lua_State* _lua;
int _index;
int _numberToPop;
GetStackValueVisitor(lua_State* lua, int index) : _lua(lua), _index(index), _numberToPop(0) {}
void print(int index)
{
OSG_NOTICE<<"lua_type("<<index<<") = ";
switch(lua_type(_lua, index))
{
case(LUA_TNIL): OSG_NOTICE<<"LUA_TNIL "<<std::endl; break;
case(LUA_TNUMBER): OSG_NOTICE<<"LUA_TNUMBER "<<lua_tonumber(_lua, index)<<std::endl; break;
case(LUA_TBOOLEAN): OSG_NOTICE<<"LUA_TBOOLEAN "<<lua_toboolean(_lua, index)<<std::endl; break;
case(LUA_TSTRING): OSG_NOTICE<<"LUA_TSTRING "<<lua_tostring(_lua, index)<<std::endl; break;
case(LUA_TTABLE): OSG_NOTICE<<"LUA_TTABLE "<<std::endl; break;
case(LUA_TFUNCTION): OSG_NOTICE<<"LUA_TFUNCTION "<<std::endl; break;
case(LUA_TUSERDATA): OSG_NOTICE<<"LUA_TUSERDATA "<<std::endl; break;
case(LUA_TTHREAD): OSG_NOTICE<<"LUA_TTHREAD "<<std::endl; break;
case(LUA_TLIGHTUSERDATA): OSG_NOTICE<<"LUA_TLIGHTUSERDATA "<<std::endl; break;
default: OSG_NOTICE<<lua_typename(_lua, index)<<std::endl; break;
}
}
template<typename T>
void get2(T& value)
{
if (lua_istable(_lua, _index))
{
lua_getfield(_lua, _index, "x");
lua_getfield(_lua, _index-1, "y");
if (lua_isnumber(_lua, -2)) value.x() = lua_tonumber(_lua, -2);
if (lua_isnumber(_lua, -1)) value.y() = lua_tonumber(_lua, -1);
_numberToPop = 3;
}
}
template<typename T>
void get3(T& value)
{
if (lua_istable(_lua, _index))
{
lua_getfield(_lua, _index, "x");
lua_getfield(_lua, _index-1, "y");
lua_getfield(_lua, _index-2, "z");
if (lua_isnumber(_lua, -3)) value.x() = lua_tonumber(_lua, -3);
if (lua_isnumber(_lua, -2)) value.y() = lua_tonumber(_lua, -2);
if (lua_isnumber(_lua, -1)) value.z() = lua_tonumber(_lua, -1);
_numberToPop = 4;
}
}
template<typename T>
void get4(T& value)
{
if (lua_istable(_lua, _index))
{
lua_getfield(_lua, _index, "x");
lua_getfield(_lua, _index-1, "y");
lua_getfield(_lua, _index-2, "z");
lua_getfield(_lua, _index-3, "w");
if (lua_isnumber(_lua, -4)) value.x() = lua_tonumber(_lua, -4);
if (lua_isnumber(_lua, -3)) value.y() = lua_tonumber(_lua, -3);
if (lua_isnumber(_lua, -2)) value.z() = lua_tonumber(_lua, -2);
if (lua_isnumber(_lua, -1)) value.w() = lua_tonumber(_lua, -1);
_numberToPop = 5;
}
}
template<typename T>
void getMatrix(T& value)
{
if (lua_istable(_lua, _index))
{
for(unsigned int r=0; r<4; ++r)
{
for(unsigned c=0; c<4; ++c)
{
lua_rawgeti(_lua, _index, r*4+c);
if (lua_isnumber(_lua, -1)) value(r,c) = lua_tonumber(_lua, -1);
lua_pop(_lua, 1);
}
}
_numberToPop = 1;
}
}
virtual void apply(bool& value) { if (lua_isboolean(_lua, _index)) { value = (lua_toboolean(_lua, _index)!=0); _numberToPop = 1; } }
virtual void apply(char& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(unsigned char& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(short& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(unsigned short& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(int& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(unsigned int& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(float& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(double& value) { if (lua_isnumber(_lua, _index)) { value = lua_tonumber(_lua, _index)!=0; _numberToPop = 1; } }
virtual void apply(std::string& value) { if (lua_isstring(_lua, _index)) { value = std::string(lua_tostring(_lua, _index), lua_strlen(_lua, _index)); } OSG_NOTICE<<"got string value = "<<value<<std::endl; }
virtual void apply(osg::Vec2f& value) { get2(value); }
virtual void apply(osg::Vec3f& value) { get3(value); }
virtual void apply(osg::Vec4f& value) { get4(value); }
virtual void apply(osg::Vec2d& value) { get2(value); }
virtual void apply(osg::Vec3d& value) { get3(value); }
virtual void apply(osg::Vec4d& value) { get4(value); }
virtual void apply(osg::Quat& value) { get4(value); }
virtual void apply(osg::Plane& value)
{
if (lua_istable(_lua, _index))
{
lua_rawgeti(_lua, _index, 0);
lua_rawgeti(_lua, _index-1, 1);
lua_rawgeti(_lua, _index-2, 2);
lua_rawgeti(_lua, _index-3, 3);
if (lua_isnumber(_lua, -4)) value[0] = lua_tonumber(_lua, -4);
if (lua_isnumber(_lua, -3)) value[1] = lua_tonumber(_lua, -3);
if (lua_isnumber(_lua, -2)) value[2] = lua_tonumber(_lua, -2);
if (lua_isnumber(_lua, -1)) value[3] = lua_tonumber(_lua, -1);
_numberToPop = 5;
}
}
virtual void apply(osg::Matrixf& value) { getMatrix(value); }
virtual void apply(osg::Matrixd& value) { getMatrix(value); }
};
bool LuaScriptEngine::pushParameter(osg::Object* object)
{
OSG_NOTICE<<"pushParameter("<<object->className()<<")"<<std::endl;
osg::ValueObject* vo = dynamic_cast<osg::ValueObject*>(object);
if (vo)
{
PushStackValueVisitor pvv(_lua);
vo->get(pvv);
}
else
{
lua_pushstring(_lua, object->className());
}
return false;
}
bool LuaScriptEngine::popParameter(osg::Object* object)
{
OSG_NOTICE<<"popParameter("<<object->className()<<")"<<std::endl;
osg::ValueObject* vo = dynamic_cast<osg::ValueObject*>(object);
if (vo)
{
GetStackValueVisitor pvv(_lua, -1);
vo->set(pvv);
lua_pop(_lua, pvv._numberToPop);
}
else
{
if (lua_isstring(_lua, -1)) { OSG_NOTICE<<"popParameter() string = "<<lua_tostring(_lua, -1)<<std::endl; }
else { OSG_NOTICE<<"popParameter() lua_type = "<<lua_type(_lua, -1)<<std::endl; }
lua_pop(_lua, 1);
}
return false;
}

View File

@ -15,6 +15,7 @@
#define LUASCRIPTENGINE_H
#include <osg/ScriptEngine>
#include <osgDB/PropertyInterface>
extern "C" {
#include <lua.h>
@ -36,7 +37,7 @@ class LuaScriptEngine : public osg::ScriptEngine
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script);
virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters);
/** get the lua_State object.*/
lua_State* getLuaState() { return _lua; }
@ -47,10 +48,20 @@ class LuaScriptEngine : public osg::ScriptEngine
virtual ~LuaScriptEngine();
bool loadScript(osg::Script* script);
bool pushParameter(osg::Object* object);
bool popParameter(osg::Object* object);
lua_State* _lua;
typedef std::set< osg::ref_ptr<osg::Script> > ScriptSet;
ScriptSet _loadedScripts;
osgDB::PropertyInterface _pi;
};
}
#endif
#endif

View File

@ -41,9 +41,9 @@ void PythonScriptEngine::initialize()
_py_main = PyModule_GetDict(PyImport_AddModule("__main__"));
}
void PythonScriptEngine::run(osg::Script* script)
bool PythonScriptEngine::run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters)
{
if (!script || !_py_main) return;
if (!script || !_py_main) return false;
PyObject* r = PyRun_String(script->getScript().c_str(), Py_file_input, _py_main, _py_main);
@ -55,4 +55,6 @@ void PythonScriptEngine::run(osg::Script* script)
PyErr_Clear();
}
}
return true;
}

View File

@ -32,7 +32,7 @@ class PythonScriptEngine : public osg::ScriptEngine
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual void run(osg::Script* script);
virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters);
/** get the Python main object.*/
PyObject* getPythonMain() { return _py_main; }
@ -49,4 +49,4 @@ class PythonScriptEngine : public osg::ScriptEngine
}
#endif
#endif

View File

@ -58,7 +58,6 @@ CullVisitor::CullVisitor():
}
CullVisitor::CullVisitor(const CullVisitor& rhs):
Referenced(true),
NodeVisitor(rhs),
CullStack(rhs),
_currentStateGraph(NULL),