Skip to content

Commit

Permalink
Merge pull request #227 from MozillaReality/pdf-support
Browse files Browse the repository at this point in the history
Add missing pdf support
  • Loading branch information
keianhzo authored Jul 24, 2023
2 parents 6069d7e + c1d01d4 commit 97f5423
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 13 deletions.
75 changes: 75 additions & 0 deletions addons/io_hubs_addon/components/definitions/pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from bpy.props import StringProperty, BoolProperty
from ..hubs_component import HubsComponent
from ..types import Category, PanelType, NodeType
from mathutils import Matrix
from math import radians


class PDF(HubsComponent):
_definition = {
'name': 'pdf',
'display_name': 'PDF',
'category': Category.MEDIA,
'node_type': NodeType.NODE,
'panel_type': [PanelType.OBJECT, PanelType.BONE],
'deps': ['networked'],
'icon': 'FILE_IMAGE',
'version': (1, 0, 0)
}
src: StringProperty(
name="PDF URL", description="The web address of the PDF", default='https://mozilla.org')
controls: BoolProperty(
name="Controls",
description="When enabled, shows pagination buttons when hovering your cursor over it in Hubs that allow you to switch pages",
default=True)

@classmethod
def gather_name(cls):
return 'image'

@classmethod
def create_gizmo(cls, ob, gizmo_group):
gizmo = gizmo_group.gizmos.new('GIZMO_GT_primitive_3d')
gizmo.draw_style = ('PLANE')
gizmo.use_draw_scale = False
gizmo.use_draw_offset_scale = True
gizmo.line_width = 3
gizmo.color = (0.8, 0.8, 0.8)
gizmo.alpha = 0.5
gizmo.hide_select = True
gizmo.scale_basis = 0.5
gizmo.use_draw_modal = True
gizmo.color_highlight = (0.8, 0.8, 0.8)
gizmo.alpha_highlight = 1.0
return gizmo

@classmethod
def update_gizmo(cls, ob, bone, target, gizmo):
if bone:
loc, rot, scale = bone.matrix.to_4x4().decompose()
# Account for the armature object's position
loc = ob.matrix_world @ Matrix.Translation(loc)
# Convert to A4 aspect ratio
scale[1] = 1.414
# Shrink the gizmo to fit within a 1x1m square
scale = scale * 0.3538
# Convert the scale to a matrix
scale = Matrix.Diagonal(scale).to_4x4()
# Convert the rotation to a matrix
rot = rot.normalized().to_matrix().to_4x4()
# Assemble the new matrix
mat_out = loc @ rot @ scale
else:
loc, rot, scale = ob.matrix_world.decompose()
# Apply the custom rotation
rot_offset = Matrix.Rotation(radians(90), 4, 'X').to_4x4()
new_rot = rot.normalized().to_matrix().to_4x4() @ rot_offset
# Convert to A4 aspect ratio
scale[1] = 1.414
# Shrink the gizmo to fit within a 1x1m square
scale = scale * 0.3538
# Assemble the new matrix
mat_out = Matrix.Translation(
loc) @ new_rot @ Matrix.Diagonal(scale).to_4x4()
gizmo.matrix_basis = mat_out
gizmo.hide = not ob.visible_get()
7 changes: 6 additions & 1 deletion addons/io_hubs_addon/components/hubs_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def get_name(cls):
return cls.__get_definition('name', cls.get_id())

@classmethod
def get_display_name(cls, default=__name__):
def get_display_name(cls, default=None):
default = cls.__name__ if default is None else default
return cls.__get_definition('display_name', default)

@classmethod
Expand Down Expand Up @@ -134,6 +135,10 @@ def migrate(self, migration_type, panel_type, instance_version, host, migration_
'''
return False

@classmethod
def gather_name(cls):
return cls.get_name()

@classmethod
def draw_global(cls, context, layout, panel):
'''Draw method to be called by the panel. This can be used to draw global component properties in a panel before the component properties.'''
Expand Down
5 changes: 2 additions & 3 deletions addons/io_hubs_addon/components/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import reduce

from .types import PanelType, MigrationType
from .utils import get_object_source, dash_to_title, has_component, add_component, remove_component, wrap_text, display_wrapped_text, is_dep_required, update_image_editors
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 ..preferences import get_addon_pref
from .handlers import migrate_components
Expand Down Expand Up @@ -144,8 +144,7 @@ def draw(self, context):

cmp_idx += 1
component_name = component_class.get_name()
component_display_name = dash_to_title(
component_class.get_display_name(component_name))
component_display_name = component_class.get_display_name()

op = None
if component_class.get_icon() is not None:
Expand Down
5 changes: 2 additions & 3 deletions addons/io_hubs_addon/components/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from bpy.props import StringProperty
from .types import PanelType
from .components_registry import get_component_by_name, get_components_registry
from .utils import get_object_source, dash_to_title, is_linked
from .utils import get_object_source, is_linked


def draw_component_global(panel, context):
Expand Down Expand Up @@ -45,8 +45,7 @@ def draw_component(panel, context, obj, row, component_item):
icon_only=True, emboss=False
)

display_name = component_class.get_display_name(
dash_to_title(component_id))
display_name = component_class.get_display_name()

top_row.label(text=display_name)

Expand Down
4 changes: 0 additions & 4 deletions addons/io_hubs_addon/components/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ def get_object_source(context, panel_type):
return context.object


def dash_to_title(s):
return s.replace("-", " ").title()


def children_recurse(ob, result):
for child in ob.children:
result.append(child)
Expand Down
4 changes: 2 additions & 2 deletions addons/io_hubs_addon/io/gltf_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ def add_hubs_components(self, gltf2_object, blender_object, export_settings):
data = component.gather(export_settings, blender_object)
if hasattr(data, "delayed_gather"):
self.delayed_gathers.append(
(component_data, component_class.get_name(), data))
(component_data, component_class.gather_name(), data))
else:
component_data[component_class.get_name()] = data
component_data[component_class.gather_name()] = data
else:
print('Could not export unsupported component "%s"' %
(component_name))
Expand Down
Binary file added tests/scenes/pdf.blend
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test/test_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,21 @@ describe('Exporter', function () {
}
});
});

it('can export pdf', function () {
let gltfPath = path.resolve(outDirPath, 'pdf.gltf');
const asset = JSON.parse(fs.readFileSync(gltfPath));

assert.strictEqual(asset.extensionsUsed.includes('MOZ_hubs_components'), true);
assert.strictEqual(utils.checkExtensionAdded(asset, 'MOZ_hubs_components'), true);

const node = asset.nodes[0];
assert.strictEqual(utils.checkExtensionAdded(node, 'MOZ_hubs_components'), true);

const ext = node.extensions['MOZ_hubs_components'];
assert.deepStrictEqual(ext['image'], { "controls": true, src: 'https://hubs.mozilla.com' });
assert.strictEqual(utils.UUID_REGEX.test(ext['networked']['id']), true);
});
});
});
});

0 comments on commit 97f5423

Please sign in to comment.