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