From 34f71d023828b130c1946ef06f69ed3ad16738d0 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Wed, 15 Nov 2023 21:05:05 +0100 Subject: [PATCH] added blender export script --- example/aframe/sandbox/example.gltf | 1 - example/aframe/sandbox/index.glb | 1 + example/assets/index.py | 70 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) delete mode 120000 example/aframe/sandbox/example.gltf create mode 120000 example/aframe/sandbox/index.glb create mode 100644 example/assets/index.py diff --git a/example/aframe/sandbox/example.gltf b/example/aframe/sandbox/example.gltf deleted file mode 120000 index 98a339c..0000000 --- a/example/aframe/sandbox/example.gltf +++ /dev/null @@ -1 +0,0 @@ -assets/example.gltf \ No newline at end of file diff --git a/example/aframe/sandbox/index.glb b/example/aframe/sandbox/index.glb new file mode 120000 index 0000000..98f442f --- /dev/null +++ b/example/aframe/sandbox/index.glb @@ -0,0 +1 @@ +assets/index.glb \ No newline at end of file diff --git a/example/assets/index.py b/example/assets/index.py new file mode 100644 index 0000000..f40ec25 --- /dev/null +++ b/example/assets/index.py @@ -0,0 +1,70 @@ +# +# This is a convenient way to convert the scene to lowpoly +# (by adding decimate-modifiers) +# and then exporting a gltf to to .glb +# +# All this is done automatically when saving the blender file +# +import bpy +import os +from bpy_extras.io_utils import ImportHelper + +# uncomment below in case you want to hardcode the exported filename +data = { + #gltf_file="/some/path/foo.index.glb" +} + +def notify(message = "", title = "Message Box", icon = 'INFO'): + def draw(self, context): + self.layout.label(text=message) + bpy.context.window_manager.popup_menu(draw, title = title, icon = icon) + +# redirect print to all consoles +def print(data): + for window in bpy.context.window_manager.windows: + screen = window.screen + for area in screen.areas: + if area.type == 'CONSOLE': + override = {'window': window, 'screen': screen, 'area': area} + bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT") + + +# Function to add Decimate Modifier to objects without one (except those in the exclusion list) +def add_decimate_modifier_to_objects(): + for obj in bpy.data.objects: + print(obj.type) + if obj is not None and (obj.type == 'FONT' or (obj.type == 'MESH' and len(obj.data.polygons) > 8)): + if not obj.modifiers.get("Decimate"): + #if obj.name not in exclusion_list and "Decimate" not in obj.modifiers: + print("adding decimate-modifier to:"+obj.name) + bpy.context.view_layer.objects.active = obj + bpy.data.objects[obj.name].select_set(True) + + # Add Decimate Modifier with ratio 0.5 + bpy.ops.object.modifier_add(type='DECIMATE') + bpy.context.object.modifiers["Decimate"].ratio = 0.5 + +# Function to be called on file save +def on_save_handler(blenderdoc): + if 'gltf_file' not in data: + gltf_file = bpy.data.filepath.replace('.blend','.glb') + else: + gltf_file = data['gltf_file'] + + add_decimate_modifier_to_objects() + + # Export to glTF with specified settings and apply modifiers + bpy.ops.export_scene.gltf( + filepath=gltf_file, + export_format='GLB', + export_extras=True, + export_lights=True, + export_apply=True, + export_force_sampling=False, + ) + notify(os.path.basename(gltf_file),"OK export") + +# Register the handler +bpy.app.handlers.save_post.clear() +bpy.app.handlers.save_post.append(on_save_handler) +print("sourced gltf_export_on_save")