Skip to content

Commit

Permalink
Merge pull request #299 from Exairnous/add-reflection-probe-import
Browse files Browse the repository at this point in the history
 Add support for importing reflection probes.
  • Loading branch information
Exairnous authored Jul 26, 2024
2 parents 0245990 + 9180870 commit 802b5b0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
26 changes: 26 additions & 0 deletions addons/io_hubs_addon/components/definitions/reflection_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -752,6 +753,31 @@ def gather(self, export_settings, object):
}
}

@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'
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_mode = 'QUATERNION'
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):
panel_type = PanelType(panel.bl_context)
Expand Down
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 802b5b0

Please sign in to comment.