diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index f1b9915a9c..0785604346 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -605,6 +605,14 @@ scripting::LuaLibrary Scene::luaLibrary() { "Returns the value the property, identified by " "the provided URI." }, + { + "getProperty", + &luascriptfunctions::property_getProperty, + {}, + "string", + "Returns a list of property identifiers that match the passed regular " + "expression" + }, { "loadScene", &luascriptfunctions::loadScene, diff --git a/src/scene/scene_lua.inl b/src/scene/scene_lua.inl index 964bd0989d..a7494138e3 100644 --- a/src/scene/scene_lua.inl +++ b/src/scene/scene_lua.inl @@ -401,6 +401,51 @@ int property_getValue(lua_State* L) { return 1; } +/** + * \ingroup LuaScripts + * getProperty + * Returns a list of property identifiers that match the passed regular expression + */ +int property_getProperty(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::property_getProperty"); + + std::string regex = ghoul::lua::value(L, 1); + lua_pop(L, 1); + + + // Replace all wildcards * with the correct regex (.*) + size_t startPos = regex.find("*"); + while (startPos != std::string::npos) { + regex.replace(startPos, 1, "(.*)"); + startPos += 4; // (.*) + startPos = regex.find("*", startPos); + } + + + + std::regex r(regex); + std::vector props = allProperties(); + std::vector res; + for (properties::Property* prop : props) { + // Check the regular expression for all properties + const std::string& id = prop->fullyQualifiedIdentifier(); + + if (std::regex_match(id, r)) { + res.push_back(id); + } + } + + lua_newtable(L); + int number = 1; + for (const std::string& s : res) { + lua_pushstring(L, s.c_str()); + lua_rawseti(L, -2, number); + ++number; + } + + return 1; +} + /** * \ingroup LuaScripts * getPropertyValue(string):