Skip to content

Commit

Permalink
Consolidate image importing into one spot, fix background color impor…
Browse files Browse the repository at this point in the history
…t when there aren't any images, and add support for image assignment to the assign_property function.
  • Loading branch information
Exairnous committed Apr 2, 2024
1 parent f002386 commit ecadcc3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from bpy.props import FloatProperty, EnumProperty, FloatVectorProperty, PointerProperty, BoolProperty
from bpy.types import Image
from ...io.utils import import_component, assign_property, import_image
from ..hubs_component import HubsComponent
from ..types import Category, PanelType, NodeType
from ..utils import is_linked
Expand Down Expand Up @@ -163,23 +162,3 @@ def gather(self, export_settings, object):
}

return output

@classmethod
def gather_import(cls, gltf, blender_host, component_name, component_value, blender_ob=None):
blender_component = import_component(
component_name, blender_host)

images = {}
for gltf_texture in gltf.data.textures:
blender_image_name, source = import_image(gltf, gltf_texture)
images[source] = blender_image_name

for property_name, property_value in component_value.items():
if isinstance(property_value, dict) and property_value['__mhc_link_type'] == "texture":
blender_image_name = images[property_value['index']]
blender_image = bpy.data.images[blender_image_name]
setattr(blender_component, property_name, blender_image)

else:
assign_property(gltf.vnodes, blender_component,
property_name, property_value)
17 changes: 0 additions & 17 deletions addons/io_hubs_addon/components/definitions/reflection_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..hubs_component import HubsComponent
from ..types import Category, PanelType, NodeType, MigrationType
from ..ui import add_link_indicator
from ...io.utils import import_component, assign_property, import_image
from ...utils import rgetattr, rsetattr
from ..utils import get_host_reference_message
from ..models import reflection_probe
Expand Down Expand Up @@ -925,22 +924,6 @@ def create_gizmo(cls, ob, gizmo_group):

return gizmo

@classmethod
def gather_import(cls, gltf, blender_host, component_name, component_value, blender_ob=None):
blender_component = import_component(
component_name, blender_host)
images = {}
for gltf_texture in gltf.data.textures:
blender_image_name, source = import_image(gltf, gltf_texture)
images[source] = blender_image_name
for property_name, property_value in component_value.items():
if isinstance(property_value, dict) and property_value['__mhc_link_type'] == "texture":
blender_image_name = images[property_value['index']]
blender_image = bpy.data.images[blender_image_name]
setattr(blender_component, property_name, blender_image)
else:
assign_property(gltf.vnodes, blender_component,
property_name, property_value)

@ staticmethod
def register():
Expand Down
5 changes: 4 additions & 1 deletion 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
from .utils import HUBS_CONFIG, import_image, import_all_images
from ..components.components_registry import get_component_by_name
import traceback

Expand Down Expand Up @@ -138,6 +138,8 @@ 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)

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

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

imported_images = {}

# gather_texture/image with HDR support via MOZ_texture_rgbe


Expand Down Expand Up @@ -407,6 +409,19 @@ def import_image(gltf, gltf_texture):

return blender_image_name, source


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

if not gltf.data.textures:
return

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


def import_component(component_name, blender_object):
from ..components.utils import add_component, has_component
from ..components.components_registry import get_component_by_name
Expand Down Expand Up @@ -435,8 +450,14 @@ def assign_property(vnodes, blender_component, property_name, property_value):
if isinstance(property_value, dict):
if property_value.get('__mhc_link_type'):
if len(property_value) == 2:
setattr(blender_component, property_name,
vnodes[property_value['index']].blender_object)
if property_value['__mhc_link_type'] == "node":
setattr(blender_component, property_name,
vnodes[property_value['index']].blender_object)
elif property_value['__mhc_link_type'] == "texture":
global imported_images
blender_image_name = imported_images[property_value['index']]
blender_image = bpy.data.images[blender_image_name]
setattr(blender_component, property_name, blender_image)

else:
blender_subcomponent = getattr(blender_component, property_name)
Expand Down

0 comments on commit ecadcc3

Please sign in to comment.