Josh Posted November 14, 2023 Share Posted November 14, 2023 AmbientCG has about 3000 PBR materials and a simple REST web API. List categories: https://ambientcg.com/api/v2/categories_json List files in category: https://ambientcg.com/api/v2/full_json?include=imageData,downloadData&category=PavingStones These URLs can be loaded with FetchURL. There is a JSON parsing library included in the Scripts/System folder, dkjson.lua. This is used by the debugger. I have not used it myself for anything. More docs here: https://docs.ambientcg.com/api/v2/ Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted November 14, 2023 Author Share Posted November 14, 2023 Example: local json = require 'System/dkjson' local s = FetchUrl("https://ambientcg.com/api/v2/categories_json") local t = json.decode(s) if type(t) == "table" then local n for n = 1, #t do if (type(t[n]) == "table") then if type(t[n]["categoryName"]) == "string" then Print(t[n]["categoryName"]) end end end end Download and extract: DownloadFile("https://ambientcg.com/get?file=PavingStones137_1K-JPG.zip", GetPath(PATH_DESKTOP).."/test.zip" ) local pak = LoadPackage(GetPath(PATH_DESKTOP).."/test.zip") if pak then local dir = pak:LoadDir("") local n for n = 1, #dir do if pak:FileType(dir[n]) == 1 then pak:ExtractFile(dir[n], GetPath(PATH_DESKTOP).."/"..dir[n]) end end pak:Close() end Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
klepto2 Posted November 15, 2023 Share Posted November 15, 2023 Hi, I have tried various JSON parsers for lua. And they are all slow as hell (at least if the json string is a larger one). In my API -Module for Polyhaven i have integrated a function which converts the nlohman:json object into a lua table and this is magnitudes faster than any lua json library. Also the usage is very easy and lua like. void jsonToLua(const nlohmann::json& json, lua_State* L) { switch (json.type()) { case nlohmann::json::value_t::null: lua_pushnil(L); break; case nlohmann::json::value_t::boolean: lua_pushboolean(L, json.get<bool>()); break; case nlohmann::json::value_t::number_integer: lua_pushinteger(L, json.get<int>()); break; case nlohmann::json::value_t::number_unsigned: lua_pushinteger(L, json.get<unsigned>()); break; case nlohmann::json::value_t::number_float: lua_pushnumber(L, json.get<double>()); break; case nlohmann::json::value_t::string: lua_pushstring(L, json.get<std::string>().c_str()); break; case nlohmann::json::value_t::array: lua_newtable(L); for (size_t i = 0; i < json.size(); ++i) { lua_pushinteger(L, i + 1); // Lua arrays are 1-indexed jsonToLua(json[i], L); lua_settable(L, -3); } break; case nlohmann::json::value_t::object: lua_newtable(L); for (auto it = json.begin(); it != json.end(); ++it) { lua_pushstring(L, it.key().c_str()); jsonToLua(it.value(), L); lua_settable(L, -3); } break; default: // Handle other types if needed break; } } int lua_JsonToLua(lua_State* L) { std::string jsonString; if (lua_isstring(L, -1)) { jsonString = lua_tostring(L, -1); } auto json = nlohmann::json::parse(jsonString); jsonToLua(json, L); return 1; } extern "C" { __declspec(dllexport) int luaopen_PolyHavenApiClient(lua_State* L) { // Load the DLL and create an instance of PolyHavenApiClient lua_newtable(L); int sz = lua_gettop(L); lua_pushcfunction(L, lua_JsonToLua); lua_setfield(L, -2, "JsonToLua"); lua_settop(L, sz); return 1; } } With this you can do something like this: local assets = extension.api.getAssets(type,cat) --just a sample api call i use to retrieve the json data local assetTable = extension.json.JsonToLua(assets) -- convert json string to lua table Print(assetTable.bark_brown_01.name) --or iterating sorted by keys function pairsByKeys (t, f) local a = {} for n in pairs(t) do table.insert(a, n) end table.sort(a, f) local i = 0 -- iterator variable local iter = function () -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end for key,data in pairsByKeys(extension.currentAssets ) do Print(key.." Displayname: "..data.name) --Prints in this case eg: "bark_brown_01 Displayname: Bark Brown 01 end 1 Quote Windows 10 Pro 64-Bit-Version NVIDIA Geforce 1080 TI Link to comment Share on other sites More sharing options...
Josh Posted November 17, 2023 Author Share Posted November 17, 2023 Updated example that uses LoadTable() instead of the Lua JSON parser: local s = FetchUrl("https://ambientcg.com/api/v2/categories_json") local t = LoadTable(s) if type(t) == "table" then local n for n = 1, #t do if (type(t[n]) == "table") then if type(t[n]["categoryName"]) == "string" then Print(t[n]["categoryName"]) end end end end Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted November 18, 2023 Author Share Posted November 18, 2023 The Lua debugger is actually using Lua socket, not libcurl. A couple of things to note: Curl (curl.exe) is now included standard on WIndows 10 and 11. Having libcurl.dll in the application's root directory will cause the Steam build to crash, presumably because the Steamworks API is using a different version of the DLL: https://steamcommunity.com/groups/steamworks/discussions/0/6699728899096777474/ Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.