diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index 53fee2cd8..0fd7cde13 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -15,6 +15,7 @@ #include #include +#include using namespace lua; @@ -1812,6 +1813,28 @@ static int readNodeFile(lua_State * _lua) return 0; } + +static int writeFile(lua_State * _lua) +{ + const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); + + int n = lua_gettop(_lua); /* number of arguments */ + if (n>=2 && lua_type(_lua, 1)==LUA_TTABLE && lua_type(_lua, 2)==LUA_TSTRING) + { + osg::Object* object = lse->getObjectFromTable(1); + std::string filename = lua_tostring(_lua, 2); + if (object) + { + osgDB::writeObjectFile(*object, filename); + return 1; + } + } + return 0; +} + + + + LuaScriptEngine::LuaScriptEngine(): osg::ScriptEngine("lua"), _lua(0), @@ -1862,6 +1885,13 @@ void LuaScriptEngine::initialize() lua_setglobal(_lua, "cast"); } + // provide global new method for reading Objects + { + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, readObjectFile, 1); + lua_setglobal(_lua, "readFile"); + } + // provide global new method for reading Objects { lua_pushlightuserdata(_lua, this); @@ -1883,6 +1913,13 @@ void LuaScriptEngine::initialize() lua_setglobal(_lua, "readImageFile"); } + // provide global new method for read Images + { + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, writeFile, 1); + lua_setglobal(_lua, "writeFile"); + } + // Set up the __newindex and __index methods for looking up implementations of Object properties { luaL_newmetatable(_lua, "LuaScriptEngine.Object");