Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/bastoma.py
Created May 27, 2015 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/529d435d5853e92dcc51 to your computer and use it in GitHub Desktop.
Save zeffii/529d435d5853e92dcc51 to your computer and use it in GitHub Desktop.
import bpy
import os
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty, IntProperty
from bpy.types import Operator
class ExportSelectedAsObj(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "export.selected_as_obj_files" # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Export Selected Objects as obj"
global_scale = IntProperty(default=1)
use_filter_folder = True
filename_ext = "."
def write_some_data(self, context, folder):
selected = bpy.context.selected_objects.copy()
bpy.ops.object.select_all(action='DESELECT')
for obj in selected:
name = obj.name.replace('.', '_')
obj.select = True
fullpath = os.path.join(folder, name + '.obj')
bpy.ops.export_scene.obj(filepath=fullpath)
obj.select = False
return {'FINISHED'}
def execute(self, context):
print(self.filepath)
sliced = os.path.dirname(self.filepath)
if os.path.isdir(sliced):
if os.path.exists(sliced):
self.filepath = sliced
else:
self.report({'WARNING'}, 'Not a valid path')
return {'FINISHED'}
return self.write_some_data(context, self.filepath)
# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ExportSelectedAsObj.bl_idname, text="Export selected as .Objs")
def register():
bpy.utils.register_class(ExportSelectedAsObj)
bpy.types.INFO_MT_file_export.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(ExportSelectedAsObj)
bpy.types.INFO_MT_file_export.remove(menu_func_import)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment