Skip to content

Commit

Permalink
Merge pull request #249 from MozillaReality/experimental/viewer
Browse files Browse the repository at this point in the history
Support for scene debugger
  • Loading branch information
keianhzo authored Dec 13, 2023
2 parents c9c74e8 + 7754ad4 commit 437f854
Show file tree
Hide file tree
Showing 16 changed files with 1,366 additions and 62 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ CMakeFiles
cmake_install.cmake
CMakeCache.txt
Makefile
lib

#Selenium
__hubs_selenium_profile
13 changes: 13 additions & 0 deletions addons/io_hubs_addon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from .utils import create_prefs_dir
from .utils import get_user_python_path
import sys
import bpy
from .io import gltf_exporter
from . import (nodes, components)
from . import preferences
from . import third_party
from . import debugger
from . import icons
bl_info = {
"name": "Hubs Blender Addon",
"author": "Mozilla Hubs",
Expand All @@ -17,13 +22,19 @@
"category": "Generic"
}

sys.path.insert(0, get_user_python_path())

create_prefs_dir()


def register():
icons.register()
preferences.register()
nodes.register()
components.register()
gltf_exporter.register()
third_party.register()
debugger.register()

# Migrate components if the add-on is enabled in the middle of a session.
if bpy.context.preferences.is_dirty:
Expand All @@ -40,6 +51,8 @@ def unregister():
components.unregister()
nodes.unregister()
preferences.unregister()
debugger.unregister()
icons.unregister()


# called by gltf-blender-io after it has loaded
Expand Down
28 changes: 0 additions & 28 deletions addons/io_hubs_addon/components/components_registry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from .types import NodeType
import bpy.utils.previews
import bpy
from bpy.props import BoolProperty, StringProperty, CollectionProperty, PointerProperty
from bpy.types import PropertyGroup

import importlib
import inspect
import os
from os import listdir
from os.path import join, isfile, isdir, dirname, realpath

from .hubs_component import HubsComponent
Expand Down Expand Up @@ -125,25 +123,6 @@ def unload_components_registry():
module.unregister_module()


def load_icons():
global __component_icons
__component_icons = {}
pcoll = bpy.utils.previews.new()
icons_dir = os.path.join(os.path.dirname(__file__), "icons")
icons = [f for f in listdir(icons_dir) if isfile(join(icons_dir, f))]
for icon in icons:
pcoll.load(icon, os.path.join(icons_dir, icon), 'IMAGE')
print("Loading icon: " + icon)
__component_icons["hubs"] = pcoll


def unload_icons():
global __component_icons
__component_icons["hubs"].close()
del __component_icons


__component_icons = {}
__components_registry = {}


Expand All @@ -152,11 +131,6 @@ def get_components_registry():
return __components_registry


def get_components_icons():
global __component_icons
return __component_icons["hubs"]


def get_component_by_name(component_name):
global __components_registry
return next(
Expand All @@ -166,7 +140,6 @@ def get_component_by_name(component_name):


def register():
load_icons()
load_components_registry()

bpy.utils.register_class(HubsComponentName)
Expand Down Expand Up @@ -201,7 +174,6 @@ def unregister():
glTF2ExportUserExtension.remove_excluded_property("hubs_component_list")

unload_components_registry()
unload_icons()

global __components_registry
del __components_registry
81 changes: 53 additions & 28 deletions addons/io_hubs_addon/components/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

from .types import PanelType, MigrationType
from .utils import get_object_source, has_component, add_component, remove_component, wrap_text, display_wrapped_text, is_dep_required, update_image_editors
from .components_registry import get_components_registry, get_components_icons, get_component_by_name
from .components_registry import get_components_registry, get_component_by_name
from ..preferences import get_addon_pref
from .handlers import migrate_components
from .gizmos import update_gizmos
from .utils import is_linked, redraw_component_ui
from ..icons import get_hubs_icons
import os


Expand All @@ -30,22 +31,26 @@ def poll(cls, context):
if panel_type == PanelType.SCENE:
if is_linked(context.scene):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot add components to linked scenes")
cls.poll_message_set(
"Cannot add components to linked scenes")
return False
elif panel_type == PanelType.OBJECT:
if is_linked(context.active_object):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot add components to linked objects")
cls.poll_message_set(
"Cannot add components to linked objects")
return False
elif panel_type == PanelType.MATERIAL:
if is_linked(context.active_object.active_material):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot add components to linked materials")
cls.poll_message_set(
"Cannot add components to linked materials")
return False
elif panel_type == PanelType.BONE:
if is_linked(context.active_bone):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot add components to linked bones")
cls.poll_message_set(
"Cannot add components to linked bones")
return False

return True
Expand All @@ -72,7 +77,7 @@ def filter_source_type(cmp):
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()
hubs_icons = get_hubs_icons()
filtered_components = dict(
filter(filter_source_type, components_registry.items()))

Expand Down Expand Up @@ -152,11 +157,11 @@ def draw(self, context):
if icon.find('.') != -1:
if has_component(obj, component_name):
op = column.label(
text=component_display_name, icon_value=components_icons[icon].icon_id)
text=component_display_name, icon_value=hubs_icons[icon].icon_id)
else:
op = column.operator(
AddHubsComponent.bl_idname, text=component_display_name,
icon_value=components_icons[icon].icon_id)
icon_value=hubs_icons[icon].icon_id)
op.component_name = component_name
op.panel_type = panel_type
else:
Expand Down Expand Up @@ -214,22 +219,26 @@ def poll(cls, context):
if panel_type == PanelType.SCENE:
if is_linked(context.scene):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot remove components from linked scenes")
cls.poll_message_set(
"Cannot remove components from linked scenes")
return False
elif panel_type == PanelType.OBJECT:
if is_linked(context.active_object):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot remove components from linked objects")
cls.poll_message_set(
"Cannot remove components from linked objects")
return False
elif panel_type == PanelType.MATERIAL:
if is_linked(context.active_object.active_material):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot remove components from linked materials")
cls.poll_message_set(
"Cannot remove components from linked materials")
return False
elif panel_type == PanelType.BONE:
if is_linked(context.active_bone):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot add components to linked bones")
cls.poll_message_set(
"Cannot add components to linked bones")
return False

return True
Expand Down Expand Up @@ -257,7 +266,8 @@ class MigrateHubsComponents(Operator):

def execute(self, context):
if self.is_registration:
migrate_components(MigrationType.REGISTRATION, do_beta_versioning=True)
migrate_components(MigrationType.REGISTRATION,
do_beta_versioning=True)
else:
migrate_components(MigrationType.LOCAL, do_beta_versioning=True)

Expand Down Expand Up @@ -288,7 +298,8 @@ def execute(self, context):
wm = context.window_manager
title = wm.hubs_report_last_title
report_string = wm.hubs_report_last_report_string
bpy.ops.wm.hubs_report_viewer('INVOKE_DEFAULT', title=title, report_string=report_string)
bpy.ops.wm.hubs_report_viewer(
'INVOKE_DEFAULT', title=title, report_string=report_string)
return {'FINISHED'}


Expand All @@ -314,12 +325,15 @@ def highlight_info_report(self):
while bpy.ops.info.select_pick(
context_override, report_index=index, extend=False) != {'CANCELLED'}:
index += 1
bpy.ops.info.select_pick(context_override, report_index=index, extend=False)
bpy.ops.info.select_pick(
context_override, report_index=index, extend=False)

def execute(self, context):
messages = split_and_prefix_report_messages(self.report_string)
info_report_string = '\n'.join([message.replace('\n', ' ') for message in messages])
self.report({'INFO'}, f"Hubs {self.title}\n{info_report_string}\nEnd of Hubs {self.title}")
info_report_string = '\n'.join(
[message.replace('\n', ' ') for message in messages])
self.report(
{'INFO'}, f"Hubs {self.title}\n{info_report_string}\nEnd of Hubs {self.title}")
bpy.ops.screen.info_log_show()
bpy.app.timers.register(self.highlight_info_report)
return {'FINISHED'}
Expand Down Expand Up @@ -403,13 +417,15 @@ def draw(self, context):

scroll_up = scroll_column.row()
scroll_up.enabled = start_index > 0
op = scroll_up.operator(ReportScroller.bl_idname, text="", icon="TRIA_UP")
op = scroll_up.operator(ReportScroller.bl_idname,
text="", icon="TRIA_UP")
op.increment = -1
op.maximum = maximum_scrolling

scroll_down = scroll_column.row()
scroll_down.enabled = start_index < maximum_scrolling
op = scroll_down.operator(ReportScroller.bl_idname, text="", icon="TRIA_DOWN")
op = scroll_down.operator(
ReportScroller.bl_idname, text="", icon="TRIA_DOWN")
op.increment = 1
op.maximum = maximum_scrolling

Expand All @@ -419,7 +435,8 @@ def draw(self, context):

scroll_percentage = column.row()
scroll_percentage.enabled = False
scroll_percentage.prop(wm, "hubs_report_scroll_percentage", slider=True)
scroll_percentage.prop(
wm, "hubs_report_scroll_percentage", slider=True)

layout.separator()

Expand Down Expand Up @@ -457,7 +474,8 @@ def init_report_display_blocks(self):
if last_message is None:
final_block = True

current_block_lines = sum([len(message) for message in block_messages])
current_block_lines = sum([len(message)
for message in block_messages])
needed_padding_lines = self.lines_to_show - current_block_lines

message_iter = iter(block_messages)
Expand Down Expand Up @@ -506,7 +524,8 @@ class CopyHubsComponent(Operator):
def poll(cls, context):
if is_linked(context.scene):
if bpy.app.version >= (3, 0, 0):
cls.poll_message_set("Cannot copy components when in linked scenes")
cls.poll_message_set(
"Cannot copy components when in linked scenes")
return False

if hasattr(context, "panel"):
Expand All @@ -518,14 +537,17 @@ def poll(cls, context):

def get_selected_bones(self, context):
selected_bones = context.selected_pose_bones if context.mode == "POSE" else context.selected_editable_bones
selected_armatures = [sel_ob for sel_ob in context.selected_objects if sel_ob.type == "ARMATURE"]
selected_armatures = [
sel_ob for sel_ob in context.selected_objects if sel_ob.type == "ARMATURE"]
selected_hosts = []
for armature in selected_armatures:
armature_bones = armature.pose.bones if context.mode == "POSE" else armature.data.edit_bones
target_armature_bones = armature.data.bones if context.mode == "POSE" else armature.data.edit_bones
target_bones = [bone for bone in armature_bones if bone in selected_bones]
target_bones = [
bone for bone in armature_bones if bone in selected_bones]
for target_bone in target_bones:
selected_hosts.extend([bone for bone in target_armature_bones if target_bone.name == bone.name])
selected_hosts.extend(
[bone for bone in target_armature_bones if target_bone.name == bone.name])
return selected_hosts

def get_selected_hosts(self, context):
Expand Down Expand Up @@ -628,12 +650,14 @@ def execute(self, context):

# Load/Reload the first image and assign it to the target property, then load the rest of the images if they're not already loaded. This mimics Blender's default open files behavior.
primary_filepath = os.path.join(dirname, self.files[0].name)
primary_img = bpy.data.images.load(filepath=primary_filepath, check_existing=True)
primary_img = bpy.data.images.load(
filepath=primary_filepath, check_existing=True)
primary_img.reload()
self.hubs_component[self.target_property] = primary_img

for f in self.files[1:]:
bpy.data.images.load(filepath=os.path.join(dirname, f.name), check_existing=True)
bpy.data.images.load(filepath=os.path.join(
dirname, f.name), check_existing=True)

update_image_editors(old_img, primary_img)
redraw_component_ui(context)
Expand All @@ -657,7 +681,8 @@ def register():
bpy.utils.register_class(ViewReportInInfoEditor)
bpy.utils.register_class(CopyHubsComponent)
bpy.utils.register_class(OpenImage)
bpy.types.WindowManager.hubs_report_scroll_index = IntProperty(default=0, min=0)
bpy.types.WindowManager.hubs_report_scroll_index = IntProperty(
default=0, min=0)
bpy.types.WindowManager.hubs_report_scroll_percentage = IntProperty(
name="Scroll Position", default=0, min=0, max=100, subtype='PERCENTAGE')
bpy.types.WindowManager.hubs_report_last_title = StringProperty()
Expand Down
14 changes: 14 additions & 0 deletions addons/io_hubs_addon/components/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ def draw(self, context):
draw_components_list(self, context)


class HUBS_PT_ToolsPanel(bpy.types.Panel):
bl_idname = "HUBS_PT_ToolsPanel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "Hubs"
bl_category = "Hubs"
bl_context = 'objectmode'

def draw(self, context):
pass


class HubsScenePanel(bpy.types.Panel):
bl_label = 'Hubs'
bl_idname = "SCENE_PT_hubs"
Expand Down Expand Up @@ -217,6 +229,7 @@ def register():
bpy.utils.register_class(HubsMaterialPanel)
bpy.utils.register_class(HubsBonePanel)
bpy.utils.register_class(TooltipLabel)
bpy.utils.register_class(HUBS_PT_ToolsPanel)

bpy.types.TOPBAR_MT_window.append(window_menu_addition)
bpy.types.VIEW3D_MT_object.append(object_menu_addition)
Expand All @@ -229,6 +242,7 @@ def unregister():
bpy.utils.unregister_class(HubsMaterialPanel)
bpy.utils.unregister_class(HubsBonePanel)
bpy.utils.unregister_class(TooltipLabel)
bpy.utils.unregister_class(HUBS_PT_ToolsPanel)

bpy.types.TOPBAR_MT_window.remove(window_menu_addition)
bpy.types.VIEW3D_MT_object.remove(object_menu_addition)
Expand Down
Loading

0 comments on commit 437f854

Please sign in to comment.