import bpy, re from . import blender_funcs # simple OBJ importer specific for NeoKCLCreate # import_obj_neokclcreate() function # read superbmd/blenxy collada file # used to import the collada file into Blender def import_obj_neokclcreate(context, filepath): # scene variable is always needed for something scene = bpy.context.scene # load the OBJ file into blender bpy.ops.import_scene.obj(filepath = filepath, axis_forward = '-Z', axis_up = 'Y') # select the single OBJ mesh and reduce its size to match blenxy's coordinate system obj = None for o in bpy.data.objects: # selection in blender api is confusing if (o.select == True): obj = o break blender_funcs.select_obj(scene, obj, False) obj.scale = obj.scale / 100 bpy.ops.object.transform_apply(location = True, rotation = True, scale = True) # deal with the infinite materials (merge those that are similar) # slow as a turtle, but works like a charm for i in range(len(obj.material_slots)): # i will not be bound to len(obj.material_slots) once a material is deleted if (i >= len(obj.material_slots)): break mat = obj.material_slots[i] cur_mat_name = re.sub("^(.*?)\| ", "", mat.name) # iterate over all the mesh faces for face in obj.data.polygons: face_mat = obj.material_slots[face.material_index] face_mat_name = re.sub("^(.*?)\| ", "", face_mat.name) # material with the same name found if (cur_mat_name == face_mat_name and i != face.material_index): bpy.data.materials.remove(face_mat.material) # remove the material data and the slot obj.active_material_index = face.material_index bpy.ops.object.material_slot_remove() face.material_index = i # change the index to be the material of the current loop # put the OBJ inside an armature object bpy.ops.object.armature_add() obj.parent = scene.objects.active # done! blender_funcs.disp_msg("NeoKCLCreate OBJ file imported!") return {'FINISHED'} ################################################# # Stuff down is for the menu appending # of the importer to work plus some setting stuff # comes from a Blender importer template # ExportHelper is a helper class, defines filename and # invoke() function which calls the file selector. from bpy_extras.io_utils import ExportHelper from bpy.props import StringProperty, BoolProperty, EnumProperty from bpy.types import Operator class import_neokclcreate_obj(Operator, ExportHelper): """Import a Collada file from SuperBMD (SuperBMD only)""" bl_idname = "import_scene.neokclcreate_obj" bl_label = "Import NeoKCLCreate OBJ (.OBJ)" # ExportHelper mixin class uses this filename_ext = ".obj" filter_glob = StringProperty(default = "*.obj", options = {'HIDDEN'}, maxlen = 255) # execute function def execute(self, context): return import_obj_neokclcreate(context, self.filepath) # Only needed if you want to add into a dynamic menu def menu_import_neokclcreate_obj(self, context): self.layout.operator(import_neokclcreate_obj.bl_idname, text="NeoKCLCreate OBJ (.obj)") bpy.utils.register_class(import_neokclcreate_obj) bpy.types.INFO_MT_file_import.append(menu_import_neokclcreate_obj) # test call bpy.ops.import_scene.neokclcreate_obj('INVOKE_DEFAULT')