Skip to content

Instantly share code, notes, and snippets.

@dirkk0
Created November 26, 2023 17:47
Show Gist options
  • Save dirkk0/9dc239a43ca0c3f730d908f29e0c0beb to your computer and use it in GitHub Desktop.
Save dirkk0/9dc239a43ca0c3f730d908f29e0c0beb to your computer and use it in GitHub Desktop.
Easy glTF/glb Exporter Blender addon
bl_info = {
"name": "Easy glTF/glb Exporter",
"blender": (2, 80, 0),
"category": "Experiment",
"author": "Dirk Krause",
}
import bpy, os
class EgePanel(bpy.types.Panel):
"""create the EGE panel"""
bl_label = "EGE"
bl_idname = "OBJECT_PT_EGE"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "EGE" # name of panel in 'n' menu
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(context.scene, "dir")
addon_keymaps = []
class EasyGlbExporter(bpy.types.Operator):
"""EasyGlbExporter"""
bl_idname = "ege.1"
bl_label = "EGE"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
print("panel dir name", context.scene.dir)
filepath = bpy.context.blend_data.filepath
dir_name = os.path.dirname(filepath)
print("blend file path", dir_name)
print("blend file name", bpy.path.basename(filepath))
file_name = bpy.path.basename(filepath).rsplit('.', maxsplit=1)[0]
print(file_name)
# self.report({'INFO'}, "")
print("context scene dir",bpy.context.scene.dir)
real_path = os.path.abspath(bpy.path.abspath(bpy.context.scene.dir))
print(real_path)
if context.scene.dir == "//":
my_path = dir_name + "/" + file_name + ".glb" # windows???
else:
my_path = real_path + "/" + file_name + ".glb"
print(bpy.data.filepath)
print("my path", my_path)
if bpy.data.filepath == "":
self.report({'ERROR'}, 'please save the file first')
else:
bpy.ops.export_scene.gltf(
filepath=my_path,
# export_format="GLTF_EMBEDDED"
export_format="GLB",
use_visible=True,
use_active_collection=True,
export_extras=True,
export_apply=True,
export_lights=True
)
self.report({'INFO'}, 'saved to ' + my_path)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(EasyGlbExporter.bl_idname)
def panel_update(self, context):
print("update")
def register():
bpy.utils.register_class(EasyGlbExporter)
bpy.types.VIEW3D_MT_object.append(menu_func)
# handle the keymap
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(EasyGlbExporter.bl_idname, 'T', 'PRESS', ctrl=True, shift=True)
# kmi.properties.total = 4
addon_keymaps.append((km, kmi))
bpy.types.Scene.dir = bpy.props.StringProperty(name="Path", subtype="DIR_PATH", default="//", update=panel_update)
# bpy.types.Scene.file = bpy.props.StringProperty(name="Path", subtype="FILE_PATH")
bpy.utils.register_class(EgePanel)
def unregister():
bpy.utils.unregister_class(EasyGlbExporter)
bpy.utils.unregister_class(EgePanel)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment