import bpy, math from . import blender_funcs # just a simple OBJ exporter wrapper # it only selects all the meshes inside an armature and sets some OBJ exporter variables # write_kcl_obj function # function to write custom obj file for kcl conversion def write_kcl_obj(context, filepath): # this thing is always needed for stuff scene = bpy.context.scene # if nothing is selected end the exporter if (scene.objects.active == None or scene.objects.active.type != "ARMATURE"): if (scene.objects.active == None): blender_funcs.disp_msg("No Armature selected. Select one and try again.") else: blender_funcs.disp_msg("No Armature selected. Currently selecting: \"%s\"" % (scene.objects.active.name)) return {"FINISHED"} # get armature object armature = scene.objects.active print("\nArmature found: \"%s\"" % (armature.name)) # select it well blender_funcs.select_obj(armature, False, "OBJECT") # check if the armature contains only mesh objects inside for obj in armature.children: if (obj.type != "MESH"): blender_funcs.disp_msg("\"%s\": contains non-mesh object (%s)." % (armature.name, obj.name)) return {"FINISHED"} # select all the meshes inside the armature blender_funcs.select_obj(armature, True, "OBJECT") # export the object bpy.ops.export_scene.obj(filepath = filepath, axis_up = "Y", axis_forward = "-Z", use_selection = True, use_triangles = True, global_scale = 100, use_materials = True, use_normals = True) # done! blender_funcs.select_obj(armature, False, "OBJECT") blender_funcs.disp_msg("Meshes from \"%s\" exported!" % (armature.name)) return {"FINISHED"} # Stuff down is for the menu appending # of the exporter to work plus some setting stuff # comes from a Blender importer template from bpy_extras.io_utils import ExportHelper from bpy.props import StringProperty, BoolProperty, EnumProperty from bpy.types import Operator class export_obj_kcl(Operator, ExportHelper): """Export a OBJ file for KCL conversion""" bl_idname = "export_scene.obj_kcl" bl_label = "Export OBJ for KCL (.OBJ)" filename_ext = ".obj" filter_glob = StringProperty(default = "*.obj", options = {'HIDDEN'}, maxlen = 255) def execute(self, context): return write_kcl_obj(context, self.filepath) # Only needed if you want to add into a dynamic menu def menu_export_obj_kcl(self, context): self.layout.operator(export_obj_kcl.bl_idname, text="OBJ for KCL (.obj)") bpy.utils.register_class(export_obj_kcl) bpy.types.INFO_MT_file_export.append(menu_export_obj_kcl) # test call bpy.ops.export_scene.obj_kcl('INVOKE_DEFAULT')