diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index 56c689b91..dc719a0dc 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -86,46 +86,25 @@ static int getChild(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) + if (n==2 && + lua_type(_lua, 1)==LUA_TTABLE && + lua_type(_lua, 2)==LUA_TNUMBER) { - if (lua_type(_lua, 1)==LUA_TTABLE && - lua_type(_lua, 2)==LUA_TNUMBER) + + osg::Group* group = lse->getObjectFromTable(1); + int index = static_cast(lua_tonumber(_lua, 2)); + if (group) { - std::string propertyName = lua_tostring(_lua, 2); - - osg::Object* object = 0; - lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, 1); - - if (lua_type(_lua, -1)==LUA_TUSERDATA) + if (index>=0 && index(group->getNumChildren())) { - object = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); - } - - lua_pop(_lua,1); - - int index = static_cast(lua_tonumber(_lua, 2)); - - osg::Group* group = dynamic_cast(object); - if (group) - { - if (index>=0 && index(group->getNumChildren())) - { - lse->pushObject(group->getChild(index)); - return 1; - } - else - { - OSG_NOTICE<<"Warning: child index out of range "<pushObject(group->getChild(index)); + return 1; } else { - OSG_NOTICE<<"Warning: cannot get child from non osg::Group object. "<(lua_topointer(_lua, lua_upvalueindex(1))); + int n = lua_gettop(_lua); /* number of arguments */ - if (n==3) + if (n==3 && + lua_type(_lua, 1)==LUA_TTABLE && + lua_type(_lua, 2)==LUA_TNUMBER && + lua_type(_lua, 3)==LUA_TTABLE) { - if (lua_type(_lua, 1)==LUA_TTABLE && - lua_type(_lua, 2)==LUA_TNUMBER && - lua_type(_lua, 3)==LUA_TTABLE) + + osg::Group* group = lse->getObjectFromTable(1); + osg::Node* child = lse->getObjectFromTable(3); + int index = static_cast(lua_tonumber(_lua, 2)); + if (group && child) { - std::string propertyName = lua_tostring(_lua, 2); - - osg::Object* object = 0; - - // get the group node. - { - lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, 1); - - if (lua_type(_lua, -1)==LUA_TUSERDATA) - { - object = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); - } - - lua_pop(_lua,1); - } - - osg::Group* group = dynamic_cast(object); - if (!group) - { - OSG_NOTICE<<"Warning: cannot set child to non osg::Group object. "<(lua_tonumber(_lua, 2)); - - // get the child node - { - lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, 3); - - if (lua_type(_lua, -1)==LUA_TUSERDATA) - { - object = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); - } - - lua_pop(_lua,1); - } - - osg::Node* child = dynamic_cast(object); - if (!child) - { - OSG_NOTICE<<"Warning: cannot set non osg::Node child to osg::Group. Child passed is of type: "<className()<=0 && index(group->getNumChildren())) { group->setChild(index, child); @@ -208,43 +148,19 @@ static int setChild(lua_State* _lua) static int addChild(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) + if (n==2 && + lua_type(_lua, 1)==LUA_TTABLE && + lua_type(_lua, 2)==LUA_TTABLE) { - if (lua_type(_lua, 1)==LUA_TTABLE && - lua_type(_lua, 2)==LUA_TTABLE) + osg::Group* group = lse->getObjectFromTable(1); + osg::Node* child = lse->getObjectFromTable(2); + if (group && child) { - osg::Object* object = 0; - lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, 1); - - if (lua_type(_lua, -1)==LUA_TUSERDATA) - { - object = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); - } - - lua_pop(_lua,1); - - osg::Object* child_obj = 0; - lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, 2); - - if (lua_type(_lua, -1)==LUA_TUSERDATA) - { - child_obj = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); - } - - lua_pop(_lua,1); - - - osg::Group* group = dynamic_cast(object); - osg::Node* child = dynamic_cast(child_obj); - if (group && child) - { - OSG_NOTICE<<"Group "<addChild(child); - return 0; - } + group->addChild(child); + return 0; } } @@ -254,33 +170,16 @@ static int addChild(lua_State* _lua) static int getNumChildren(lua_State * _lua) { + const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); + int n = lua_gettop(_lua); /* number of arguments */ - if (n==1) + if (n==1 && (lua_type(_lua, 1)==LUA_TTABLE)) { - if (lua_type(_lua, 1)==LUA_TTABLE) + osg::Group* group = lse->getObjectFromTable(1); + if (group) { - osg::Object* object = 0; - lua_pushstring(_lua, "object_ptr"); - lua_rawget(_lua, 1); - - if (lua_type(_lua, -1)==LUA_TUSERDATA) - { - object = *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))); - } - - lua_pop(_lua,1); - - osg::Group* group = dynamic_cast(object); - if (group) - { - lua_pushinteger(_lua, group->getNumChildren()); - return 1; - } - else - { - OSG_NOTICE<<"Warning: cannot get child from non osg::Group object. "<getNumChildren()); + return 1; } } @@ -321,6 +220,60 @@ static int newObject(lua_State * _lua) return 0; } +static int readObjectFile(lua_State * _lua) +{ + const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); + + int n = lua_gettop(_lua); /* number of arguments */ + if (n==1 && lua_type(_lua, 1)==LUA_TSTRING) + { + std::string filename = lua_tostring(_lua, 1); + osg::ref_ptr object = osgDB::readObjectFile(filename); + if (object.valid()) + { + lse->pushObject(object.get()); + return 1; + } + } + return 0; +} + +static int readImageFile(lua_State * _lua) +{ + const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); + + int n = lua_gettop(_lua); /* number of arguments */ + if (n==1 && lua_type(_lua, 1)==LUA_TSTRING) + { + std::string filename = lua_tostring(_lua, 1); + osg::ref_ptr image = osgDB::readImageFile(filename); + if (image.valid()) + { + lse->pushObject(image.get()); + return 1; + } + } + return 0; +} + +static int readNodeFile(lua_State * _lua) +{ + const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); + + int n = lua_gettop(_lua); /* number of arguments */ + if (n==1 && lua_type(_lua, 1)==LUA_TSTRING) + { + std::string filename = lua_tostring(_lua, 1); + osg::ref_ptr node = osgDB::readNodeFile(filename); + if (node.valid()) + { + lse->pushObject(node.get()); + return 1; + } + } + return 0; +} + LuaScriptEngine::LuaScriptEngine(): osg::ScriptEngine("lua"), _lua(0), @@ -364,6 +317,27 @@ void LuaScriptEngine::initialize() lua_setglobal(_lua, "new"); } + // provide global new method for reading Objects + { + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, readObjectFile, 1); + lua_setglobal(_lua, "readObjectFile"); + } + + // provide global new method for reading Nodes + { + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, readNodeFile, 1); + lua_setglobal(_lua, "readNodeFile"); + } + + // provide global new method for read Images + { + lua_pushlightuserdata(_lua, this); + lua_pushcclosure(_lua, readImageFile, 1); + lua_setglobal(_lua, "readImageFile"); + } + // Set up the __newindex and __index methods for looking up implementations of Object properties { luaL_newmetatable(_lua, "LuaScriptEngine.Object"); diff --git a/src/osgPlugins/lua/LuaScriptEngine.h b/src/osgPlugins/lua/LuaScriptEngine.h index a8f1110a3..fba5c5347 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.h +++ b/src/osgPlugins/lua/LuaScriptEngine.h @@ -91,6 +91,25 @@ class LuaScriptEngine : public osg::ScriptEngine void createAndPushObject(const std::string& compoundName) const; void pushObject(osg::Object* object) const; + template + T* getObjectFromTable(int pos) const + { + if (lua_type(_lua, pos)==LUA_TTABLE) + { + lua_pushstring(_lua, "object_ptr"); + lua_rawget(_lua, pos); + + osg::Object* object = (lua_type(_lua, -1)==LUA_TUSERDATA)? + *const_cast(reinterpret_cast(lua_touserdata(_lua,-1))) : + 0; + + lua_pop(_lua,1); + + return dynamic_cast(object); + } + else return 0; + } + protected: void initialize();