From 86accdf891449bacfcf73328feac6a08903cd6bd Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Tue, 23 Jul 2024 05:11:57 -0400 Subject: [PATCH 1/4] Add support for importing reflection probes. Reflection Probes import as empties, so a Light Probe object is added to host the component and then parented to the empty. --- .../definitions/reflection_probe.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/addons/io_hubs_addon/components/definitions/reflection_probe.py b/addons/io_hubs_addon/components/definitions/reflection_probe.py index c9212b56..f683a0b6 100644 --- a/addons/io_hubs_addon/components/definitions/reflection_probe.py +++ b/addons/io_hubs_addon/components/definitions/reflection_probe.py @@ -10,6 +10,7 @@ from ..types import Category, PanelType, NodeType from ..ui import add_link_indicator from ...utils import rgetattr, rsetattr +from ...io.utils import import_component, assign_property import math import os @@ -751,6 +752,28 @@ def gather(self, export_settings, object): "index": gather_texture(self.envMapTexture, export_settings) } } + + @classmethod + def gather_import(cls, gltf, blender_host, component_name, component_value, import_report, blender_ob=None): + # Reflection Probes import as empties, so add a Light Probe object to host the component and parent it to the empty. + probe_type = 'CUBE' if bpy.app.version < (4, 1, 0) else 'SPHERE' + lightprobe_data = bpy.data.lightprobes.new(blender_host.name, probe_type) + lightprobe_data.influence_type ='BOX' + lightprobe_object = bpy.data.objects.new(blender_host.name, lightprobe_data) + lightprobe_object.location = blender_host.location + lightprobe_object.rotation_quaternion = blender_host.rotation_quaternion + lightprobe_object.scale = blender_host.scale + lightprobe_object.parent = blender_host + for collection in blender_host.users_collection: + collection.objects.link(lightprobe_object) + bpy.context.view_layer.update() + lightprobe_object.matrix_parent_inverse = blender_host.matrix_world.inverted() + + # Import the component + component = import_component(component_name, lightprobe_object) + lightprobe_data.influence_distance = component_value["size"] + assign_property(gltf.vnodes, component, + "envMapTexture", component_value["envMapTexture"]) @ classmethod def draw_global(cls, context, layout, panel): From 9c95d6a5d66a85ff5408b4aa5d29482b7cd3324e Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Tue, 23 Jul 2024 06:19:00 -0400 Subject: [PATCH 2/4] Prevent deduplication of textures that point to the same images when 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. --- addons/io_hubs_addon/io/gltf_importer.py | 6 +++--- addons/io_hubs_addon/io/utils.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/addons/io_hubs_addon/io/gltf_importer.py b/addons/io_hubs_addon/io/gltf_importer.py index 47b36cca..570caeb3 100644 --- a/addons/io_hubs_addon/io/gltf_importer.py +++ b/addons/io_hubs_addon/io/gltf_importer.py @@ -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 @@ -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: @@ -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] diff --git a/addons/io_hubs_addon/io/utils.py b/addons/io_hubs_addon/io/utils.py index 8f39c8e9..6649c74a 100644 --- a/addons/io_hubs_addon/io/utils.py +++ b/addons/io_hubs_addon/io/utils.py @@ -26,7 +26,7 @@ "gltfExtensionVersion": 4, } -imported_images = {} +imported_textures = {} # gather_texture/image with HDR support via MOZ_texture_rgbe @@ -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): @@ -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) From 682ba11f13e07bee0c3c448ae3b822b76c1ad8c9 Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Tue, 23 Jul 2024 07:46:21 -0400 Subject: [PATCH 3/4] Fix formatting and make sure the Reflection Probe ends up with the correct name. --- .../components/definitions/reflection_probe.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/addons/io_hubs_addon/components/definitions/reflection_probe.py b/addons/io_hubs_addon/components/definitions/reflection_probe.py index f683a0b6..a3a0ee5a 100644 --- a/addons/io_hubs_addon/components/definitions/reflection_probe.py +++ b/addons/io_hubs_addon/components/definitions/reflection_probe.py @@ -752,14 +752,16 @@ def gather(self, export_settings, object): "index": gather_texture(self.envMapTexture, export_settings) } } - + @classmethod def gather_import(cls, gltf, blender_host, component_name, component_value, import_report, blender_ob=None): # Reflection Probes import as empties, so add a Light Probe object to host the component and parent it to the empty. probe_type = 'CUBE' if bpy.app.version < (4, 1, 0) else 'SPHERE' - lightprobe_data = bpy.data.lightprobes.new(blender_host.name, probe_type) - lightprobe_data.influence_type ='BOX' - lightprobe_object = bpy.data.objects.new(blender_host.name, lightprobe_data) + reflecion_probe_name = blender_host.name + blender_host.name = f"{blender_host.name}_node" + lightprobe_data = bpy.data.lightprobes.new(reflecion_probe_name, probe_type) + lightprobe_data.influence_type = 'BOX' + lightprobe_object = bpy.data.objects.new(reflecion_probe_name, lightprobe_data) lightprobe_object.location = blender_host.location lightprobe_object.rotation_quaternion = blender_host.rotation_quaternion lightprobe_object.scale = blender_host.scale From 9180870aba1cf2364e89eefd6bb5a567470bf27d Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Tue, 23 Jul 2024 14:17:20 -0400 Subject: [PATCH 4/4] Switch the rotation mode of the Light Probe object to quaternion so that the rotation is properly applied. --- addons/io_hubs_addon/components/definitions/reflection_probe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/io_hubs_addon/components/definitions/reflection_probe.py b/addons/io_hubs_addon/components/definitions/reflection_probe.py index a3a0ee5a..e3723c25 100644 --- a/addons/io_hubs_addon/components/definitions/reflection_probe.py +++ b/addons/io_hubs_addon/components/definitions/reflection_probe.py @@ -763,6 +763,7 @@ def gather_import(cls, gltf, blender_host, component_name, component_value, impo lightprobe_data.influence_type = 'BOX' lightprobe_object = bpy.data.objects.new(reflecion_probe_name, lightprobe_data) lightprobe_object.location = blender_host.location + lightprobe_object.rotation_mode = 'QUATERNION' lightprobe_object.rotation_quaternion = blender_host.rotation_quaternion lightprobe_object.scale = blender_host.scale lightprobe_object.parent = blender_host