correct for re-arranged imports
[blender-addons-contrib.git] / io_directx_bel / __init__.py
1 # Blender directX importer
2 bl_info = {
3     "name": "DirectX Importer",
4     "description": "Import directX Model Format (.x)",
5     "author": "Littleneo (Jerome Mahieux)",
6     "version": (0, 16),
7     "blender": (2, 6, 1),
8     "location": "File > Import > DirectX (.x)",
9     "warning": "",
10     "wiki_url": "https://github.com/littleneo/directX_blender/wiki",
11     "tracker_url": "https://github.com/littleneo/directX_blender/issues",
12     "category": "Import-Export",
13     "dependencies": ""
14 }
15
16 if "bpy" in locals():
17     import imp
18     if "import_x" in locals():
19         imp.reload(import_x)
20     #if "export_x" in locals():
21     #    imp.reload(export_x)
22
23
24 import bpy
25 from bpy.props import (BoolProperty,
26                        FloatProperty,
27                        StringProperty,
28                        EnumProperty,
29                        )
30 from bpy_extras.io_utils import (ExportHelper,
31                                  ImportHelper,
32                                  path_reference_mode,
33                                  axis_conversion,
34                                  )
35
36 '''
37 class DisplayTree(bpy.types.Operator) :
38     bl_idname = 'city.selector'
39     bl_label = 'preview'
40
41     def execute(self,context) :
42 '''
43     
44     
45 class ImportX(bpy.types.Operator, ImportHelper):
46     '''Load a Direct x File'''
47     bl_idname = "import_scene.x"
48     bl_label = "Import X"
49     bl_options = {'PRESET', 'UNDO'}
50
51     filename_ext = ".x"
52     filter_glob = StringProperty(
53             default="*.x",
54             options={'HIDDEN'},
55             )
56     show_tree = BoolProperty(
57             name="Show x tokens tree",
58             description="display relationships between x items in the console",
59             default=False,
60             )
61     show_templates = BoolProperty(
62             name="Show x templates",
63             description="display templates defined in the .x file",
64             default=False,
65             )
66     show_geninfo = BoolProperty(
67             name="Show processing",
68             description="display details for each imported thing",
69             default=False,
70             )
71     
72     quickmode = BoolProperty(
73             name="Quick mode",
74             description="only retrieve mesh basics",
75             default=False,
76             )
77     
78     parented = BoolProperty(
79             name="Object Relationships",
80             description="import armatures, empties, rebuild parent-childs relations",
81             default=True,
82             )
83     
84     bone_maxlength = FloatProperty(
85             name="Bone length",
86             description="Bones max length",
87             min=0.1, max=10.0,
88             soft_min=0.1, soft_max=10.0,
89             default=1.0,
90             )
91     
92     chunksize = EnumProperty(
93             name="Chunksize",
94             items=(('0', "all", ""),
95                    ('4096', "4KB", ""),
96                    ('2048', "2KB", ""),
97                    ('1024', "1KB", ""),
98                    ),
99             default='2048',
100             description="number of bytes red in a row",
101             )
102     naming_method = EnumProperty(
103             name="Naming method",
104             items=(('0', "increment name if exists", "blender default"),
105                    ('1', "use existing", "this only append new elements"),
106                    ('2', "rename existing", "names are forced"),
107                    ('3', "replace existing", ""),
108                    ),
109             default='0',
110             description="behaviour when a name already exists in Blender Data",
111             )
112     use_ngons = BoolProperty(
113             name="NGons",
114             description="Import faces with more then 4 verts as fgons",
115             default=True,
116             )
117     use_edges = BoolProperty(
118             name="Lines",
119             description="Import lines and faces with 2 verts as edge",
120             default=True,
121             )
122     use_smooth_groups = BoolProperty(
123             name="Smooth Groups",
124             description="Surround smooth groups by sharp edges",
125             default=True,
126             )
127
128     use_split_objects = BoolProperty(
129             name="Object",
130             description="Import OBJ Objects into Blender Objects",
131             default=True,
132             )
133     use_split_groups = BoolProperty(
134             name="Group",
135             description="Import OBJ Groups into Blender Objects",
136             default=True,
137             )
138
139     use_groups_as_vgroups = BoolProperty(
140             name="Poly Groups",
141             description="Import OBJ groups as vertex groups",
142             default=False,
143             )
144
145     use_image_search = BoolProperty(
146             name="Image Search",
147             description="Search subdirs for any assosiated images " \
148                         "(Warning, may be slow)",
149             default=True,
150             )
151
152     split_mode = EnumProperty(
153             name="Split",
154             items=(('ON', "Split", "Split geometry, omits unused verts"),
155                    ('OFF', "Keep Vert Order", "Keep vertex order from file"),
156                    ),
157             )
158
159     global_clamp_size = FloatProperty(
160             name="Clamp Scale",
161             description="Clamp the size to this maximum (Zero to Disable)",
162             min=0.0, max=1000.0,
163             soft_min=0.0, soft_max=1000.0,
164             default=0.0,
165             )
166     
167     axis_forward = EnumProperty(
168             name="Forward",
169             items=(('X', "Left (x)", ""),
170                    ('Y', "Same (y)", ""),
171                    ('Z', "Bottom (z)", ""),
172                    ('-X', "Right (-x)", ""),
173                    ('-Y', "Back (-y)", ""),
174                    ('-Z', "Up (-z)", ""),
175                    ),
176             default='-Z',
177             )
178
179     axis_up = EnumProperty(
180             name="Up",
181             items=(('X', "Right (x)", ""),
182                    ('Y', "Back (y)", ""),
183                    ('Z', "Same (z)", ""),
184                    ('-X', "Left (-x)", ""),
185                    ('-Y', "Front (-y)", ""),
186                    ('-Z', "Bottom (-z)", ""),
187                    ),
188             default='Y',
189             )
190
191     def execute(self, context):
192         from . import bel
193         from . import import_x
194         if self.split_mode == 'OFF':
195             self.use_split_objects = False
196             self.use_split_groups = False
197         else:
198             self.use_groups_as_vgroups = False
199             
200         keywords = self.as_keywords(ignore=("axis_forward",
201                                             "axis_up",
202                                             "filter_glob",
203                                             "split_mode",
204                                             ))
205
206         keywords["naming_method"] = int(self.naming_method)
207         
208         global_matrix = axis_conversion(from_forward=self.axis_forward,
209                                         from_up=self.axis_up,
210                                         ).to_4x4()
211         keywords["global_matrix"] = global_matrix
212
213     
214         bel.fs.saveOptions('import_scene.x', self.as_keywords(ignore=(
215                                             "filter_glob",
216                                             "filepath",
217                                             )))
218         return import_x.load(self, context, **keywords)
219
220     def draw(self, context):
221         layout = self.layout
222         
223         # import box
224         box = layout.box()
225         col = box.column(align=True)
226         col.label('Import Options :')  
227         col.prop(self, "chunksize")
228         col.prop(self, "use_smooth_groups")
229         actif = not(self.quickmode)
230         row = col.row()
231         row.enabled = actif
232         row.prop(self, "parented")
233         if self.parented :
234             row = col.row()
235             row.enabled = actif
236             row.prop(self, "bone_maxlength")      
237         col.prop(self, "quickmode")
238         
239         # source orientation box
240         box = layout.box()
241         col = box.column(align=True)
242         col.label('Source Orientation :')      
243         col.prop(self, "axis_forward")
244         col.prop(self, "axis_up")
245
246         # naming methods box
247         box = layout.box()
248         col = box.column(align=True)
249         col.label('Naming Method :')
250         col.props_enum(self,"naming_method")
251
252         # info/debug box
253         box = layout.box()
254         col = box.column(align=True)
255         col.label('Info / Debug :')
256         col.prop(self, "show_tree")
257         col.prop(self, "show_templates")
258         col.prop(self, "show_geninfo")
259         
260         #row = layout.row(align=True)
261         #row.prop(self, "use_ngons")
262         #row.prop(self, "use_edges")
263         
264         '''
265         box = layout.box()
266         row = box.row()
267         row.prop(self, "split_mode", expand=True)
268
269         row = box.row()
270         if self.split_mode == 'ON':
271             row.label(text="Split by:")
272             row.prop(self, "use_split_objects")
273             row.prop(self, "use_split_groups")
274         else:
275             row.prop(self, "use_groups_as_vgroups")
276
277         row = layout.split(percentage=0.67)
278         row.prop(self, "global_clamp_size")
279
280         layout.prop(self, "use_image_search")
281         '''
282
283 def menu_func_import(self, context):
284     self.layout.operator(ImportX.bl_idname, text="DirectX (.x)")
285
286 #def menu_func_export(self, context):
287 #    self.layout.operator(ExportX.bl_idname, text="DirectX (.x)")
288
289 def register():
290     bpy.utils.register_module(__name__)
291
292     bpy.types.INFO_MT_file_import.append(menu_func_import)
293     #bpy.types.INFO_MT_file_export.append(menu_func_export)
294
295
296 def unregister():
297     bpy.utils.unregister_module(__name__)
298
299     bpy.types.INFO_MT_file_import.remove(menu_func_import)
300     #bpy.types.INFO_MT_file_export.remove(menu_func_export)
301
302 if __name__ == "__main__":
303     register()