Skip to content

Commit

Permalink
Merge pull request #182 from Exairnous/handle-unsupported-hosts
Browse files Browse the repository at this point in the history
Handle components on unsupported hosts.
  • Loading branch information
keianhzo authored Dec 23, 2022
2 parents e08372a + d426486 commit 52b51d2
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 66 deletions.
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Audio(HubsComponent):
description="Loop",
default=True)

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
12 changes: 4 additions & 8 deletions addons/io_hubs_addon/components/definitions/audio_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from bpy.props import BoolProperty, FloatProperty, EnumProperty
from ..hubs_component import HubsComponent
from ..types import PanelType, NodeType, MigrationType
from ..utils import is_linked
from ..utils import is_linked, get_host_reference_message
from ..consts import DISTACE_MODELS, MAX_ANGLE
from math import degrees, radians

Expand Down Expand Up @@ -105,7 +105,7 @@ def gather(self, export_settings, object):
'coneOuterGain': self.coneOuterGain
}

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand All @@ -115,13 +115,9 @@ def migrate(self, migration_type, instance_version, host, migration_report, ob=N
self.coneOuterAngle)

if migration_type != MigrationType.GLOBAL or is_linked(ob):
host_type = "bone" if hasattr(host, "tail") else "object"
if host_type == "bone":
host_reference = f"\"{host.name}\" in \"{host.id_data.name_full}\""
else:
host_reference = f"\"{host.name_full}\""
host_reference = get_host_reference_message(panel_type, host, ob=ob)
migration_report.append(
f"Warning: The Media Cone angles may not have migrated correctly for the Audio Params component on the {host_type} {host_reference}")
f"Warning: The Media Cone angles may not have migrated correctly for the Audio Params component on the {panel_type.value} {host_reference}")

return migration_occurred

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def gather(self, export_settings, object):
'mediaConeOuterGain': self.mediaConeOuterGain,
}

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/audio_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def create_gizmo(cls, ob, gizmo_group):

return gizmo

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Image(HubsComponent):
items=PROJECTION_MODE,
default="flat")

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Link(HubsComponent):
href: StringProperty(name="Link URL", description="Link URL",
default="https://mozilla.org")

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
12 changes: 4 additions & 8 deletions addons/io_hubs_addon/components/definitions/loop_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bpy.types import PropertyGroup, Menu, Operator
from ..hubs_component import HubsComponent
from ..types import Category, PanelType, NodeType
from ..utils import redraw_component_ui
from ..utils import redraw_component_ui, get_host_reference_message

msgbus_owners = []

Expand Down Expand Up @@ -683,7 +683,7 @@ def unregister():

unregister_msgbus()

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down Expand Up @@ -714,13 +714,9 @@ def migrate(self, migration_type, instance_version, host, migration_report, ob=N
track.track_type = track_type

if migration_warning:
host_type = "bone" if hasattr(host, "tail") else "object"
if host_type == "bone":
host_reference = f"\"{host.name}\" in \"{host.id_data.name_full}\""
else:
host_reference = f"\"{host.name_full}\""
host_reference = get_host_reference_message(panel_type, host, ob=ob)
migration_report.append(
f"Warning: The Loop Animation component on the {host_type} {host_reference} may not have migrated correctly")
f"Warning: The Loop Animation component on the {panel_type.value} {host_reference} may not have migrated correctly")

return migration_occurred

Expand Down
12 changes: 4 additions & 8 deletions addons/io_hubs_addon/components/definitions/media_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..models import box
from ..hubs_component import HubsComponent
from ..types import Category, PanelType, NodeType, MigrationType
from ..utils import V_S1, is_linked
from ..utils import V_S1, is_linked, get_host_reference_message
from .networked import migrate_networked
from mathutils import Matrix, Vector

Expand Down Expand Up @@ -118,7 +118,7 @@ def create_gizmo(cls, ob, gizmo_group):

return gizmo

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand All @@ -128,13 +128,9 @@ def migrate(self, migration_type, instance_version, host, migration_report, ob=N
self.bounds = bounds

if migration_type != MigrationType.GLOBAL or is_linked(ob):
host_type = "bone" if hasattr(host, "tail") else "object"
if host_type == "bone":
host_reference = f"\"{host.name}\" in \"{host.id_data.name_full}\""
else:
host_reference = f"\"{host.name_full}\""
host_reference = get_host_reference_message(panel_type, host, ob=ob)
migration_report.append(
f"Warning: The Media Frame component's Y and Z bounds on the {host_type} {host_reference} may not have migrated correctly")
f"Warning: The Media Frame component's Y and Z bounds on the {panel_type.value} {host_reference} may not have migrated correctly")

return migration_occurred

Expand Down
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Model(HubsComponent):
src: StringProperty(name="Model URL", description="Model URL",
default="https://mozilla.org")

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class MorphAudioFeedback(HubsComponent):
default=1.0)

@classmethod
def poll(cls, context, panel_type):
return context.object.type == 'MESH'
def poll(cls, panel_type, host, ob=None):
return host.type == 'MESH'

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
4 changes: 2 additions & 2 deletions addons/io_hubs_addon/components/definitions/nav_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class NavMesh(HubsComponent):
}

@classmethod
def poll(cls, context, panel_type):
return context.object.type == 'MESH'
def poll(cls, panel_type, host, ob=None):
return host.type == 'MESH'
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,8 @@ def draw_global(cls, context, layout, panel):
icon='ERROR')

@ classmethod
def poll(cls, context, panel_type):
return context.object.type == 'LIGHT_PROBE'
def poll(cls, panel_type, host, ob=None):
return host.type == 'LIGHT_PROBE'

@ staticmethod
def register():
Expand Down
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def gather(self, export_settings, object):
}
}

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Video(HubsComponent):
description="Loop",
default=True)

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
31 changes: 18 additions & 13 deletions addons/io_hubs_addon/components/definitions/video_texture_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ class VideoTextureSource(HubsComponent):
name="FPS", description="FPS", default=15)

@classmethod
def poll(cls, context, panel_type):
ob = context.object
def poll(cls, panel_type, host, ob=None):
if panel_type == PanelType.OBJECT:
return hasattr(
ob, 'type') and (
ob.type == 'CAMERA' or [x for x in children_recursive(ob) if x.type == "CAMERA" and not x.parent_bone])
host, 'type') and (
host.type == 'CAMERA' or
[x for x in children_recursive(host) if x.type == "CAMERA" and not x.parent_bone])
elif panel_type == PanelType.BONE:
bone = context.active_bone
return [x for x in children_recursive(ob) if x.type == "CAMERA" and x.parent_bone == bone.name]
return [x for x in children_recursive(ob) if x.type == "CAMERA" and x.parent_bone == host.name]
return False

def draw(self, context, layout, panel):
super().draw(context, layout, panel)
if not VideoTextureSource.poll(context, PanelType(panel.bl_context)):
col = layout.column()
col.alert = True
col.label(text='No camera found in the object hierarchy',
icon='ERROR')
@classmethod
def get_unsupported_host_message(cls, panel_type, host, ob=None):
if panel_type == PanelType.BONE:
host_reference = f"\"{host.name}\" in \"{ob.name_full}\""
object_message = ""
else:
host_reference = f"\"{host.name_full}\""
object_message = " aren't cameras themselves and"

host_type = panel_type.value
message = f"Warning: Unsupported component on {host_type} {host_reference}, {host_type}s that{object_message} don't have a camera somewhere in their child hierarchy don't support {cls.get_display_name()} components"

return message
2 changes: 1 addition & 1 deletion addons/io_hubs_addon/components/definitions/waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def create_gizmo(cls, ob, gizmo_group):

return gizmo

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
migration_occurred = False
if instance_version < (1, 0, 0):
migration_occurred = True
Expand Down
18 changes: 11 additions & 7 deletions addons/io_hubs_addon/components/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .components_registry import get_components_registry
from .utils import redirect_c_stdout, get_host_components, is_linked
from .gizmos import update_gizmos
from .types import MigrationType
from .types import MigrationType, PanelType
import io
import sys
import traceback
Expand All @@ -14,14 +14,14 @@
file_loading = False


def migrate(component, migration_type, host, migration_report, ob=None):
def migrate(component, migration_type, panel_type, host, migration_report, ob=None):
instance_version = tuple(component.instance_version)
definition_version = component.__class__.get_definition_version()
was_migrated = False

if instance_version < definition_version:
was_migrated = component.migrate(
migration_type, instance_version, host, migration_report, ob=ob)
migration_type, panel_type, instance_version, host, migration_report, ob=ob)

if type(was_migrated) != bool:
print(f"Warning: the {component.get_display_name()} component didn't return whether a migration occurred.")
Expand All @@ -30,6 +30,10 @@ def migrate(component, migration_type, host, migration_report, ob=None):

component.instance_version = definition_version

if panel_type not in component.__class__.get_panel_type() or not component.__class__.poll(panel_type, host, ob=ob):
message = component.__class__.get_unsupported_host_message(panel_type, host, ob=ob)
migration_report.append(message)

return was_migrated


Expand All @@ -48,7 +52,7 @@ def migrate_components(
for component in get_host_components(scene):
try:
was_migrated = migrate(
component, migration_type, scene, migration_report)
component, migration_type, PanelType.SCENE, scene, migration_report)
except Exception as e:
was_migrated = True
error = f"Error: Migration failed for component {component.get_display_name()} on scene \"{scene.name_full}\""
Expand All @@ -66,7 +70,7 @@ def migrate_components(
for component in get_host_components(ob):
try:
was_migrated = migrate(
component, migration_type, ob, migration_report, ob=ob)
component, migration_type, PanelType.OBJECT, ob, migration_report, ob=ob)
except Exception as e:
was_migrated = True
error = f"Error: Migration failed for component {component.get_display_name()} on object \"{ob.name_full}\""
Expand All @@ -85,7 +89,7 @@ def migrate_components(
for component in get_host_components(bone):
try:
was_migrated = migrate(
component, migration_type, bone, migration_report, ob=ob)
component, migration_type, PanelType.BONE, bone, migration_report, ob=ob)
except Exception as e:
was_migrated = True
error = f"Error: Migration failed for component {component.get_display_name()} on bone \"{bone.name}\" in \"{ob.name_full}\""
Expand All @@ -103,7 +107,7 @@ def migrate_components(
for component in get_host_components(material):
try:
was_migrated = migrate(
component, migration_type, material, migration_report)
component, migration_type, PanelType.MATERIAL, material, migration_report)
except Exception as e:
was_migrated = True
error = f"Error: Migration failed for component {component.get_display_name()} on material \"{material.name_full}\""
Expand Down
19 changes: 16 additions & 3 deletions addons/io_hubs_addon/components/hubs_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ def post_export(self, export_settings, host, ob=None):
'''This is called by the exporter after the export process has finished'''
pass

def migrate(self, migration_type, instance_version, host, migration_report, ob=None):
def migrate(self, migration_type, panel_type, instance_version, host, migration_report, ob=None):
'''This is called when an object component needs to migrate the data from previous add-on versions.
The migration_type argument is the type of migration, GLOBAL represents file loads, and LOCAL represents things like append/link.
The panel_type argument is used to determine what data-block the component is on.
The instance_version argument represents the version of the component that will be migrated from, as a tuple.
The host argument is what the component is attached to, object/bone.
The migration_report argument is a list that you can append messages to and they will be displayed to the user after the migration has finished.
Expand All @@ -147,11 +148,23 @@ def get_properties(cls):
return {}

@classmethod
def poll(cls, context, panel_type):
def poll(cls, panel_type, host, ob=None):
'''This method will return true if this component's shown be shown or run.
This is currently called when checking if the component should be added to the components pop-up and when the components properties panel is drawn'''
This is currently called when checking if the component should be added to the components pop-up, when the components properties panel is drawn, and during migrations to warn about unsupported hosts.
The ob argument is guaranteed to be present only for objects/bones.'''
return True

@classmethod
def get_unsupported_host_message(cls, panel_type, host, ob=None):
'''This method will return the message to use if this component isn't supported on this host.
This is currently called during migrations.'''
from .utils import get_host_reference_message
host_reference = get_host_reference_message(panel_type, host, ob=ob)
host_type = panel_type.value
message = f"Warning: Unsupported component on {host_type} {host_reference}, {host_type}s don't support {cls.get_display_name()} components"

return message

@staticmethod
def register():
'''This is called by the Blender runtime when the component is registered.
Expand Down
3 changes: 2 additions & 1 deletion addons/io_hubs_addon/components/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def invoke(self, context, event):
# Filter components that are not targeted to this object type or their poll method call returns False
def filter_source_type(cmp):
(_, component_class) = cmp
return not component_class.is_dep_only() and PanelType(panel_type) in component_class.get_panel_type() and component_class.poll(context, PanelType(panel_type))
host = get_object_source(context, panel_type)
return not component_class.is_dep_only() and PanelType(panel_type) in component_class.get_panel_type() and component_class.poll(PanelType(panel_type), host, ob=context.object)

components_registry = get_components_registry()
components_icons = get_components_icons()
Expand Down
1 change: 0 additions & 1 deletion addons/io_hubs_addon/components/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class PanelType(Enum):
OBJECT = 'object'
SCENE = 'scene'
MATERIAL = 'material'
OBJECT_DATA = 'data'
BONE = 'bone'


Expand Down
Loading

0 comments on commit 52b51d2

Please sign in to comment.