Last active
December 10, 2023 12:21
-
-
Save Oppodelldog/ddfd86e422debb5c04dd32218169c58e to your computer and use it in GitHub Desktop.
Blender add-on that makes Wavefront obj export easy. CTRL + SHIFT +E 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 OBJ Export Target Path"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy; | |
import os; | |
bl_info = { | |
"name": "Easy OBJ Export", | |
"author": "Oppodelldog", | |
"description": "This add-on makes Wavefront obj export easy. ", | |
"blender": (3, 4, 1), | |
"category": "Export", | |
} | |
bpy.types.Scene.export_target_path = bpy.props.StringProperty() | |
addon_keymaps = [] | |
class SetExportTargetPath(bpy.types.Operator): | |
bl_idname = "wm.easy_obj_export_set_export_target_path" | |
bl_label = "Confiure Easy OBJ Export Target Path" | |
export_target_path: bpy.props.StringProperty(name="Easy OBJ Export Target Path") | |
def execute(self, context): | |
scene = context.scene | |
scene.export_target_path = self.export_target_path | |
return {'FINISHED'} | |
def invoke(self, context, event): | |
wm = context.window_manager | |
self.export_target_path=context.scene.export_target_path | |
return wm.invoke_props_dialog(self) | |
def draw(self, context): | |
layout = self.layout | |
scene = context.scene | |
layout.prop(self, "export_target_path") | |
class EasyObjExportSelected(bpy.types.Operator): | |
bl_idname = "wm.easy_obj_export_selected" | |
bl_label = "Easy OBJ Export Selected Object(s)" | |
def execute(self, context): | |
print("Easy OBJ Export: Starting") | |
scene = context.scene | |
if not scene.export_target_path: | |
self.report({'ERROR'}, "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.export_target_path,collectionName,obj.name) | |
if not os.path.exists(path): | |
os.makedirs(path) | |
filename=r'{0}\{1}.obj'.format(path,obj.name) | |
selectBranch(obj,True) | |
bpy.ops.wm.obj_export(filepath=filename, export_selected_objects=True, export_object_groups=True) | |
selectBranch(obj,False) | |
export_count=export_count+1 | |
print("Easy OBJ Export: exported to {0}".format(filename)) | |
selectList(original_selected,True); | |
self.report({'INFO'}, "Easy OBJ Export finished ({0})".format(export_count)) | |
print("Easy OBJ Export: Finished") | |
return {'FINISHED'} | |
classes = [ | |
EasyObjExportSelected, | |
SetExportTargetPath | |
] | |
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(EasyObjExportSelected.bl_idname, text="Easy OBJ Export - Selected Object(s)") | |
self.layout.operator(SetExportTargetPath.bl_idname, text="Easy OBJ Export - Set Targtet Output 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(EasyObjExportSelected.bl_idname, 'E', '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(EasyObjExportSelected.bl_idname, 'E', '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(EasyObjExportSelected.bl_idname, 'E', '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() | |
return all_selected | |
if __name__ == "__main__": | |
register() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment