vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
476 stars 75 forks source link

Delete a image and all the other reference of the deleted image #98

Closed issacclee closed 3 years ago

issacclee commented 3 years ago

Hi, After having done some digging through the source code, I still haven't found a way to delete a image and all other reference of deleted image. I was wondering if such behavior does not align with the purpose of this project?

vpenades commented 3 years ago

I need more context to understand what you want to do.

If you want to load an existing model, delete an image from a material, and then save the model with that change, then, it's still possible to do it, but it's not easy to do, and need to be aware of potential issues.

issacclee commented 3 years ago

The scenario is that our application received bunch of gltf models that were exported from various CAD software, some CAD software's gltf exporter don't conform to gltf spec by the book, which leaves us with situation such as

  1. gltf image referencing a file that does not exist, while such image is reference by a texture object and subsequently a material object
  2. the file that referenced by an gltf image object is not valid jpg or png file
  3. name of the file that referenced by an gltf image object is not url safe

Based on that, I would like to use SharpGLTF to load those models, check the validity of each image object and remove the ones that can not be fixed(such as situation 1 and 2)

issacclee commented 3 years ago

Obviously when I m removing a gltf image with corrupted file, I would also like to remove the texture object that reference such image object and the relative map reference in material as well.

vpenades commented 3 years ago

As you have noticed, the Schema2 namespace has classes and APIs that only allow the add new elements to the DOM, not remove. In order to fully edit a glTF (and add/remove) elements, you need to convert from Schema2 to SceneBuilder, and then it's when you can remove the images from the materials.

But all this is, assuming the gltf is well formed. In fact, trying to convert from Schema2 to SceneBuilder will probably fail if the textures are missing or malformed, so you can't really use that path.

Simply put: SharpGLTF cannot be used to fix malformed glTF models.

I can see two possible solutions:

  1. If the problem is only missing/malformed texture images (jpegs, pngs), I would suggest using the GetSatellitePaths method, and then identify which files are missing or malformed, and then, create or replace them with valid images , at which point you could load the model into glTF without errors, then convert them to SceneBuilder, and doing the editing there.
  2. Completely skip using SharpGLTF and use NewtonSoft to load and edit the glTF's json (which is what I usually recomend to fix malformed glTF's because it's the only way to have full control over the DOM)

Ultimately, you should contact the developers that are exporting malformed glTFs, the problem definitively needs to be fixed on their end.

issacclee commented 3 years ago

Thanks @vpenades , much appreciated, I'll close this issue now.