/
batchexport.py
64 lines (48 loc) · 1.78 KB
/
batchexport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import bpy
import os
bl_info = {
"name": "Batch Export",
"category": "Import-Export",
"version": (1, 0),
"blender": (2, 8, 0),
"location": "Info > File > Export > OBJ Batch Export",
"author": "Anh Mac"
}
def register():
bpy.utils.register_class(BatchExport)
bpy.types.TOPBAR_MT_file_export.append(menu_func)
def unregister():
bpy.utils.unregister_class(BatchExport)
bpy.types.TOPBAR_MT_file_export.remove(menu_func)
def menu_func(self, context):
self.layout.operator(BatchExport.bl_idname)
class BatchExport(bpy.types.Operator):
bl_idname = "file.obj_batch_export"
bl_label = "OBJ Batch Export"
bl_options = {'REGISTER'}
filepath = bpy.props.StringProperty(
name="File Path",
maxlen=1024,
subtype="DIR_PATH",
)
def execute(self, context):
# deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# loop through all the objects in the scene
for ob in context.scene.objects:
# make the current object active and select it
context.view_layer.objects.active = ob
ob.select_set(state=True)
# make sure that we only export meshes
if ob.type == 'MESH':
# export the currently selected object to its own file based on its name
bpy.ops.export_scene.obj(filepath=os.path.join(self.filepath, ob.name + '.obj'), use_selection=True)
# deselect the object and move on to another if any more are left
ob.select_set(state=False)
return {'FINISHED'}
def invoke(self, context, event):
self.filepath = ""
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
if __name__ == "__main__":
register()