summaryrefslogtreecommitdiff
path: root/obj_neokclcreate_import.py
blob: 9c597ffb444a47bbcc8eb0d4fe96809d26be1cd2 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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(obj, False, "OBJECT")
  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:
      print(face)
      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):
  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)")

# register func
@bpy.app.handlers.persistent
def register(dummy):
  try:
    bpy.utils.register_class(import_neokclcreate_obj)
    bpy.types.INFO_MT_file_import.append(menu_import_neokclcreate_obj)
  except:
    return
    
# unregister func
def unregister():
  try:
    bpy.utils.unregister_class(import_neokclcreate_obj)
    bpy.types.INFO_MT_file_import.remove(menu_import_neokclcreate_obj)
  except:
    return