From a99215df547c86ebd8bceda5899fe547633a67d4 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Tue, 28 May 2024 16:53:01 +0200 Subject: [PATCH] safe gltf with fontcollection objects --- __init__.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/__init__.py b/__init__.py index a8c4faf..b71a356 100644 --- a/__init__.py +++ b/__init__.py @@ -328,14 +328,69 @@ class FONT3D_OT_SaveFontToFile(bpy.types.Operator): font3d_data = scene.font3d_data font3d = scene.font3d - selected_font = font3d_data.available_fonts[font3d_data.active_font_index] + fontcollection = bpy.data.collections.get("Font3D") - print(selected_font.font_name) + # needed to restore current state later + fontcollection_was_linked = False + previous_objects = [] + fontcollection_objects = [] + + # hide fontcollection + if fontcollection is None: + self.report({'INFO'}, f"{bl_info['name']}: There is no collection") + return {'CANCELLED'} + elif scene.collection.children.find(fontcollection.name) > 0: + scene.collection.children.unlink(fontcollection) + fontcollection_was_linked = True + + # collect and hide previous objects + for o in scene.objects: + previous_objects.append(o) + scene.collection.objects.unlink(o) + + # show fontcollection + # if scene.collection.children.find(fontcollection.name) < 0: + # scene.collection.children.link(fontcollection) + # link fontcollection + for o in fontcollection.objects: + fontcollection_objects.append(o) + fontcollection.objects.unlink(o) + scene.collection.objects.link(o) + + # get save data + selected_font = font3d_data.available_fonts[font3d_data.active_font_index] if selected_font == "": butils.ShowMessageBox("Warning", 'ERROR', "no font selected") return {'CANCELLED'} + print(selected_font.font_name) + # save as gltf + bpy.ops.export_scene.gltf( + filepath="/home/jrkb/Downloads/toast/maker.gltf", + check_existing=False, + export_format='GLTF_SEPARATE', + export_extras=True, + export_hierarchy_full_collections=True, + use_active_collection_with_nested=True, + ) + + # restore from previous state + def restore(): + for o in scene.objects: + scene.collection.objects.unlink(o) + + for o in previous_objects: + scene.collection.objects.link(o) + + for o in fontcollection_objects: + fontcollection.objects.link(o) + + if fontcollection_was_linked: + scene.collection.children.link(fontcollection) + + bpy.app.timers.register(restore, first_interval=5) + return {'FINISHED'}