Skip to content

Commit

Permalink
Added writeFile support
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14381 16af8721-9629-0410-8352-f15c8da7e697
  • Loading branch information
robertosfield committed Jul 23, 2014
1 parent 95f0554 commit 705d752
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/osgPlugins/lua/LuaScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <osg/io_utils>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

using namespace lua;

Expand Down Expand Up @@ -1812,6 +1813,28 @@ static int readNodeFile(lua_State * _lua)
return 0;
}


static int writeFile(lua_State * _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(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<osg::Object>(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),
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down

0 comments on commit 705d752

Please sign in to comment.