Skip to content

Instantly share code, notes, and snippets.

@Oppodelldog
Last active May 11, 2024 19:44
Show Gist options
  • Save Oppodelldog/51d816196f8bb7b54ab3ef8b2fe210fc to your computer and use it in GitHub Desktop.
Save Oppodelldog/51d816196f8bb7b54ab3ef8b2fe210fc to your computer and use it in GitHub Desktop.
Blender add-on that makes GLTF export easy. CTRL + SHIFT +D will export the selected object(s) into a configured target folder. Multiple selections will result in multiple exports. Object hierarchy is supported. To initialize the export path search for "Confiure Easy GLTF Export Target Path"
import bpy;
import os;
bl_info = {
"name": "Easy GLTF Export",
"author": "Oppodelldog",
"description": "This add-on makes GLTF export easy. ",
"blender": (3, 4, 1),
"category": "Export",
}
bpy.types.Scene.gltf_export_target_path = bpy.props.StringProperty()
addon_keymaps = []
class SetGLFTExportTargetPath(bpy.types.Operator):
bl_idname = "wm.easy_gltf_export_set_export_target_path"
bl_label = "Confiure Easy GLTF Export Target Path"
gltf_export_target_path: bpy.props.StringProperty(name="Easy GLTF Export Target Path")
def execute(self, context):
scene = context.scene
scene.gltf_export_target_path = self.gltf_export_target_path
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
self.gltf_export_target_path = context.scene.gltf_export_target_path
return wm.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
scene = context.scene
layout.prop(self, "gltf_export_target_path")
class EasyGLTFExportSelected(bpy.types.Operator):
bl_idname = "wm.easy_gltf_export_selected"
bl_label = "Easy GLFT Export Selected Object(s)"
def execute(self, context):
print("Easy GLTF Export: Starting")
scene = context.scene
if not scene.gltf_export_target_path:
self.report({'ERROR'}, "gltf export path not set")
return {'FINISHED'}
original_selected = allSelected(bpy.context.selected_objects)
selectList(original_selected, False);
export_count = 0
for obj in original_selected:
collectionName = get_collection_name(obj)
path = "{0}\{1}\{2}".format(scene.gltf_export_target_path, collectionName, obj.name)
if not os.path.exists(path):
os.makedirs(path)
filename = r'{0}\{1}.gltf'.format(path, obj.name)
selectBranch(obj, True)
bpy.ops.export_scene.gltf(filepath=filename, export_format="GLB", use_selection=True, export_apply=True,export_gn_mesh=True)
selectBranch(obj, False)
export_count = export_count + 1
print("Easy GLTF Export: exported to {0}".format(filename))
selectList(original_selected, True);
self.report({'INFO'}, "Easy GLTF Export finished ({0})".format(export_count))
print("Easy GLTF Export: Finished")
return {'FINISHED'}
classes = [
EasyGLTFExportSelected,
SetGLFTExportTargetPath
]
def get_collection_name(obj):
for collection in bpy.data.collections:
if obj.name in collection.objects:
return collection.name
return None
def selectList(objs, select):
for child in objs:
child.select_set(select)
def selectBranch(obj, select):
obj.select_set(select)
for child in obj.children:
child.select_set(select)
selectBranch(child, select)
def allSelected(objects):
all_selected = []
for obj in objects:
if obj.select_get():
all_selected.append(obj)
obj.select_set(False)
all_selected.extend(allSelected(obj.children))
return all_selected
def menu_func(self, context):
self.layout.operator(EasyGLTFExportSelected.bl_idname, text="Easy GLTF Export - Selected Object(s)")
self.layout.operator(SetGLFTExportTargetPath.bl_idname, text="Easy GLTF Export - Set Target Path")
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.VIEW3D_MT_view.append(menu_func)
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name="Object Mode", space_type="EMPTY")
kmi = km.keymap_items.new(EasyGLTFExportSelected.bl_idname, 'D', 'PRESS', ctrl=True, shift=True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name="Mesh", space_type="EMPTY")
kmi = km.keymap_items.new(EasyGLTFExportSelected.bl_idname, 'D', 'PRESS', ctrl=True, shift=True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name="Outliner", space_type="OUTLINER")
kmi = km.keymap_items.new(EasyGLTFExportSelected.bl_idname, 'D', 'PRESS', ctrl=True, shift=True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name="Sculpt Mode", space_type="EMPTY")
kmi = km.keymap_items.new(EasyGLTFExportSelected.bl_idname, 'D', 'PRESS', ctrl=True, shift=True)
addon_keymaps.append((km, kmi))
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
wm = bpy.context.window_manager
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment