Skip to content

Commit

Permalink
Prevent deduplication of textures that point to the same images when …
Browse files Browse the repository at this point in the history
…importing textures so that any references to the textures return the correct image and don't fail.

The previous behavior could cause index out of bounds errors when trying to access textures and their images.
  • Loading branch information
Exairnous committed Jul 23, 2024
1 parent 86accdf commit 9c95d6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions addons/io_hubs_addon/io/gltf_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from io_scene_gltf2.blender.imp.gltf2_blender_material import BlenderMaterial
from io_scene_gltf2.blender.imp.gltf2_blender_scene import BlenderScene
from io_scene_gltf2.blender.imp.gltf2_blender_image import BlenderImage
from .utils import HUBS_CONFIG, import_image, import_all_images
from .utils import HUBS_CONFIG, import_image, import_all_textures
from ..components.components_registry import get_component_by_name
import traceback

Expand Down Expand Up @@ -142,7 +142,7 @@ def gather_import_scene_before_hook(self, gltf_scene, blender_scene, gltf):
gltf.import_settings['gltf_yup'] = gltf.data.asset.extras[
'gltf_yup']

import_all_images(gltf)
import_all_textures(gltf)

def gather_import_scene_after_nodes_hook(self, gltf_scene, blender_scene, gltf):
if not self.properties.enabled:
Expand Down Expand Up @@ -253,7 +253,7 @@ def patched_BlenderScene_create(gltf):
delayed_gathers.clear()
import_report.clear()

import_all_images(gltf)
import_all_textures(gltf)
orig_BlenderScene_create(gltf)
gltf_scene = gltf.data.scenes[gltf.data.scene]
blender_object = bpy.data.scenes[gltf.blender_scene]
Expand Down
16 changes: 8 additions & 8 deletions addons/io_hubs_addon/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"gltfExtensionVersion": 4,
}

imported_images = {}
imported_textures = {}

# gather_texture/image with HDR support via MOZ_texture_rgbe

Expand Down Expand Up @@ -414,16 +414,16 @@ def import_image(gltf, gltf_texture):
return blender_image_name, source


def import_all_images(gltf):
global imported_images
imported_images.clear()
def import_all_textures(gltf):
global imported_textures
imported_textures.clear()

if not gltf.data.textures:
return

for gltf_texture in gltf.data.textures:
for index, gltf_texture in enumerate(gltf.data.textures):
blender_image_name, source = import_image(gltf, gltf_texture)
imported_images[source] = blender_image_name
imported_textures[index] = blender_image_name


def import_component(component_name, blender_object):
Expand Down Expand Up @@ -467,8 +467,8 @@ def assign_property(vnodes, blender_component, property_name, property_value):
setattr(blender_component, "bone",
bone_vnode.blender_bone_name)
elif property_value['__mhc_link_type'] == "texture":
global imported_images
blender_image_name = imported_images[property_value['index']]
global imported_textures
blender_image_name = imported_textures[property_value['index']]
blender_image = bpy.data.images[blender_image_name]
setattr(blender_component, property_name, blender_image)

Expand Down

0 comments on commit 9c95d6a

Please sign in to comment.