8ce76d1a38d33fd3c967bb86a062d10d2f9ad48e
[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     "api": 42615,
9     "location": "File > Import > DirectX (.x)",
10     "warning": "",
11     "wiki_url": "https://github.com/littleneo/directX_blender/wiki",
12     "tracker_url": "https://github.com/littleneo/directX_blender/issues",
13     "category": "Import-Export",
14     "dependencies": ""
15 }
16
17 if "bpy" in locals():
18     import imp
19     if "import_x" in locals():
20         imp.reload(import_x)
21     #if "export_x" in locals():
22     #    imp.reload(export_x)
23
24
25 import bpy
26 from bpy.props import (BoolProperty,
27                        FloatProperty,
28                        StringProperty,
29                        EnumProperty,
30                        )
31 from bpy_extras.io_utils import (ExportHelper,
32                                  ImportHelper,
33                                  path_reference_mode,
34                                  axis_conversion,
35                                  )
36 import bel
37 '''
38 class DisplayTree(bpy.types.Operator) :
39     bl_idname = 'city.selector'
40     bl_label = 'preview'
41
42     def execute(self,context) :
43 '''
44     
45     
46 class ImportX(bpy.types.Operator, ImportHelper):
47     '''Load a Direct x File'''
48     bl_idname = "import_scene.x"
49     bl_label = "Import X"
50     bl_options = {'PRESET', 'UNDO'}
51
52     filename_ext = ".x"
53     filter_glob = StringProperty(
54             default="*.x",
55             options={'HIDDEN'},
56             )
57     show_tree = BoolProperty(
58             name="Show x tokens tree",
59             description="display relationships between x items in the console",
60             default=False,
61             )
62     show_templates = BoolProperty(
63             name="Show x templates",
64             description="display templates defined in the .x file",
65             default=False,
66             )
67     show_geninfo = BoolProperty(
68             name="Show processing",
69             description="display details for each imported thing",
70             default=False,
71             )
72     
73     quickmode = BoolProperty(
74             name="Quick mode",
75             description="only retrieve mesh basics",
76             default=False,
77             )
78     
79     parented = BoolProperty(
80             name="Object Relationships",
81             description="import armatures, empties, rebuild parent-childs relations",
82             default=True,
83             )
84     
85     bone_maxlength = FloatProperty(
86             name="Bone length",
87             description="Bones max length",
88             min=0.1, max=10.0,
89             soft_min=0.1, soft_max=10.0,
90             default=1.0,
91             )
92     
93     chunksize = EnumProperty(
94             name="Chunksize",
95             items=(('0', "all", ""),
96                    ('4096', "4KB", ""),
97                    ('2048', "2KB", ""),
98                    ('1024', "1KB", ""),
99                    ),
100             default='2048',
101             description="number of bytes red in a row",
102             )
103     naming_method = EnumProperty(
104             name="Naming method",
105             items=(('0', "increment name if exists", "blender default"),
106                    ('1', "use existing", "this only append new elements"),
107                    ('2', "rename existing", "names are forced"),
108                    ('3', "replace existing", ""),
109                    ),
110             default='0',
111             description="behaviour when a name already exists in Blender Data",
112             )
113     use_ngons = BoolProperty(
114             name="NGons",
115             description="Import faces with more then 4 verts as fgons",
116             default=True,
117             )
118     use_edges = BoolProperty(
119             name="Lines",
120             description="Import lines and faces with 2 verts as edge",
121             default=True,
122             )
123     use_smooth_groups = BoolProperty(
124             name="Smooth Groups",
125             description="Surround smooth groups by sharp edges",
126             default=True,
127             )
128
129     use_split_objects = BoolProperty(
130             name="Object",
131             description="Import OBJ Objects into Blender Objects",
132             default=True,
133             )
134     use_split_groups = BoolProperty(
135             name="Group",
136             description="Import OBJ Groups into Blender Objects",
137             default=True,
138             )
139
140     use_groups_as_vgroups = BoolProperty(
141             name="Poly Groups",
142             description="Import OBJ groups as vertex groups",
143             default=False,
144             )
145
146     use_image_search = BoolProperty(
147             name="Image Search",
148             description="Search subdirs for any assosiated images " \
149                         "(Warning, may be slow)",
150             default=True,
151             )
152
153     split_mode = EnumProperty(
154             name="Split",
155             items=(('ON', "Split", "Split geometry, omits unused verts"),
156                    ('OFF', "Keep Vert Order", "Keep vertex order from file"),
157                    ),
158             )
159
160     global_clamp_size = FloatProperty(
161             name="Clamp Scale",
162             description="Clamp the size to this maximum (Zero to Disable)",
163             min=0.0, max=1000.0,
164             soft_min=0.0, soft_max=1000.0,
165             default=0.0,
166             )
167     
168     axis_forward = EnumProperty(
169             name="Forward",
170             items=(('X', "Left (x)", ""),
171                    ('Y', "Same (y)", ""),
172                    ('Z', "Bottom (z)", ""),
173                    ('-X', "Right (-x)", ""),
174                    ('-Y', "Back (-y)", ""),
175                    ('-Z', "Up (-z)", ""),
176                    ),
177             default='-Z',
178             )
179
180     axis_up = EnumProperty(
181             name="Up",
182             items=(('X', "Right (x)", ""),
183                    ('Y', "Back (y)", ""),
184                    ('Z', "Same (z)", ""),
185                    ('-X', "Left (-x)", ""),
186                    ('-Y', "Front (-y)", ""),
187                    ('-Z', "Bottom (-z)", ""),
188                    ),
189             default='Y',
190             )
191
192     def execute(self, context):
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()