Skip to content

Commit

Permalink
Very basic support for importing files with multiple resources via th…
Browse files Browse the repository at this point in the history
…e API.
  • Loading branch information
andybak committed Dec 8, 2024
1 parent 274a6a3 commit ae83bff
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Assets/Scripts/API/ApiMethods.EditableModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace TiltBrush
{
Expand All @@ -39,6 +41,30 @@ public static void ImportWebModel(string url)
}
var destinationPath = Path.Combine("Models", uri.Host);
string filename = _DownloadMediaFileFromUrl(uri, destinationPath);
// Very basic workaround for dependent files like .bin and textures
// TODO
// 1. Handle GLB
// 2. Handle other formats
// 3. Handle zip files
// 4. Create subdirectories for each model
if (ext == "gltf")
{
// Split the url into a base uri and the filename
var baseUri = new Uri(uri, ".");

var fullLocalPath = Path.Combine(App.ModelLibraryPath(), uri.Host);

var jsonString = File.ReadAllText(Path.Combine(fullLocalPath, filename));
JObject jsonObject = JObject.Parse(jsonString);
List<string> externalFiles = jsonObject["buffers"].Select(j => j["uri"].Value<string>()).ToList();
externalFiles.AddRange(jsonObject["images"].Select(j => j["uri"].Value<string>()).ToList());
foreach (var externalFile in externalFiles)
{
var newUri = new Uri(baseUri, externalFile);
var subdir = Path.GetDirectoryName(externalFile);
_DownloadMediaFileFromUrl(newUri, Path.Combine(fullLocalPath, subdir));
}
}
ImportModel(Path.Combine(uri.Host, filename));
}

Expand Down

0 comments on commit ae83bff

Please sign in to comment.