1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
25 "axis_conversion_ensure",
26 "create_derived_objects",
27 "free_derived_objects",
31 "path_reference_copy",
32 "path_reference_mode",
37 from bpy.props import StringProperty, BoolProperty, EnumProperty
40 def _check_axis_conversion(op):
41 if hasattr(op, "axis_forward") and hasattr(op, "axis_up"):
42 return axis_conversion_ensure(op,
50 filepath = StringProperty(
52 description="Filepath used for exporting the file",
56 check_existing = BoolProperty(
57 name="Check Existing",
58 description="Check and warn on overwriting existing files",
63 # subclasses can override with decorator
64 # True == use ext, False == no ext, None == do nothing.
65 check_extension = True
67 def invoke(self, context, event):
70 blend_filepath = context.blend_data.filepath
71 if not blend_filepath:
72 blend_filepath = "untitled"
74 blend_filepath = os.path.splitext(blend_filepath)[0]
76 self.filepath = blend_filepath + self.filename_ext
78 context.window_manager.fileselect_add(self)
79 return {'RUNNING_MODAL'}
81 def check(self, context):
83 change_axis = _check_axis_conversion(self)
85 check_extension = self.check_extension
87 if check_extension is not None:
88 filepath = bpy.path.ensure_ext(self.filepath,
93 if filepath != self.filepath:
94 self.filepath = filepath
97 return (change_ext or change_axis)
101 filepath = StringProperty(
103 description="Filepath used for importing the file",
108 def invoke(self, context, event):
109 context.window_manager.fileselect_add(self)
110 return {'RUNNING_MODAL'}
112 def check(self, context):
113 return _check_axis_conversion(self)
116 # Axis conversion function, not pretty LUT
117 # use lookup tabes to convert between any axis
118 _axis_convert_matrix = (
119 ((-1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)),
120 ((-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, -1.0, 0.0)),
121 ((-1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
122 ((-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, -1.0)),
123 ((0.0, -1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
124 ((0.0, -1.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0)),
125 ((0.0, -1.0, 0.0), (0.0, 0.0, 1.0), (-1.0, 0.0, 0.0)),
126 ((0.0, -1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
127 ((0.0, 0.0, -1.0), (-1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
128 ((0.0, 0.0, -1.0), (0.0, -1.0, 0.0), (-1.0, 0.0, 0.0)),
129 ((0.0, 0.0, -1.0), (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
130 ((0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
131 ((0.0, 0.0, 1.0), (-1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
132 ((0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
133 ((0.0, 0.0, 1.0), (0.0, 1.0, 0.0), (-1.0, 0.0, 0.0)),
134 ((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
135 ((0.0, 1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
136 ((0.0, 1.0, 0.0), (0.0, 0.0, -1.0), (-1.0, 0.0, 0.0)),
137 ((0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
138 ((0.0, 1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
139 ((1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, -1.0)),
140 ((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)),
141 ((1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
144 # store args as a single int
145 # (X Y Z -X -Y -Z) --> (0, 1, 2, 3, 4, 5)
146 # each value is ((src_forward, src_up), (dst_forward, dst_up))
147 # where all 4 values are or'd into a single value...
148 # (i1<<0 | i1<<3 | i1<<6 | i1<<9)
149 _axis_convert_lut = (
150 {0x8C8, 0x4D0, 0x2E0, 0xAE8, 0x701, 0x511, 0x119, 0xB29, 0x682, 0x88A,
151 0x09A, 0x2A2, 0x80B, 0x413, 0x223, 0xA2B, 0x644, 0x454, 0x05C, 0xA6C,
152 0x745, 0x94D, 0x15D, 0x365},
153 {0xAC8, 0x8D0, 0x4E0, 0x2E8, 0x741, 0x951, 0x159, 0x369, 0x702, 0xB0A,
154 0x11A, 0x522, 0xA0B, 0x813, 0x423, 0x22B, 0x684, 0x894, 0x09C, 0x2AC,
155 0x645, 0xA4D, 0x05D, 0x465},
156 {0x4C8, 0x2D0, 0xAE0, 0x8E8, 0x681, 0x291, 0x099, 0x8A9, 0x642, 0x44A,
157 0x05A, 0xA62, 0x40B, 0x213, 0xA23, 0x82B, 0x744, 0x354, 0x15C, 0x96C,
158 0x705, 0x50D, 0x11D, 0xB25},
159 {0x2C8, 0xAD0, 0x8E0, 0x4E8, 0x641, 0xA51, 0x059, 0x469, 0x742, 0x34A,
160 0x15A, 0x962, 0x20B, 0xA13, 0x823, 0x42B, 0x704, 0xB14, 0x11C, 0x52C,
161 0x685, 0x28D, 0x09D, 0x8A5},
162 {0x708, 0xB10, 0x120, 0x528, 0x8C1, 0xAD1, 0x2D9, 0x4E9, 0x942, 0x74A,
163 0x35A, 0x162, 0x64B, 0xA53, 0x063, 0x46B, 0x804, 0xA14, 0x21C, 0x42C,
164 0x885, 0x68D, 0x29D, 0x0A5},
165 {0xB08, 0x110, 0x520, 0x728, 0x941, 0x151, 0x359, 0x769, 0x802, 0xA0A,
166 0x21A, 0x422, 0xA4B, 0x053, 0x463, 0x66B, 0x884, 0x094, 0x29C, 0x6AC,
167 0x8C5, 0xACD, 0x2DD, 0x4E5},
168 {0x508, 0x710, 0xB20, 0x128, 0x881, 0x691, 0x299, 0x0A9, 0x8C2, 0x4CA,
169 0x2DA, 0xAE2, 0x44B, 0x653, 0xA63, 0x06B, 0x944, 0x754, 0x35C, 0x16C,
170 0x805, 0x40D, 0x21D, 0xA25},
171 {0x108, 0x510, 0x720, 0xB28, 0x801, 0x411, 0x219, 0xA29, 0x882, 0x08A,
172 0x29A, 0x6A2, 0x04B, 0x453, 0x663, 0xA6B, 0x8C4, 0x4D4, 0x2DC, 0xAEC,
173 0x945, 0x14D, 0x35D, 0x765},
174 {0x748, 0x350, 0x160, 0x968, 0xAC1, 0x2D1, 0x4D9, 0x8E9, 0xA42, 0x64A,
175 0x45A, 0x062, 0x68B, 0x293, 0x0A3, 0x8AB, 0xA04, 0x214, 0x41C, 0x82C,
176 0xB05, 0x70D, 0x51D, 0x125},
177 {0x948, 0x750, 0x360, 0x168, 0xB01, 0x711, 0x519, 0x129, 0xAC2, 0x8CA,
178 0x4DA, 0x2E2, 0x88B, 0x693, 0x2A3, 0x0AB, 0xA44, 0x654, 0x45C, 0x06C,
179 0xA05, 0x80D, 0x41D, 0x225},
180 {0x348, 0x150, 0x960, 0x768, 0xA41, 0x051, 0x459, 0x669, 0xA02, 0x20A,
181 0x41A, 0x822, 0x28B, 0x093, 0x8A3, 0x6AB, 0xB04, 0x114, 0x51C, 0x72C,
182 0xAC5, 0x2CD, 0x4DD, 0x8E5},
183 {0x148, 0x950, 0x760, 0x368, 0xA01, 0x811, 0x419, 0x229, 0xB02, 0x10A,
184 0x51A, 0x722, 0x08B, 0x893, 0x6A3, 0x2AB, 0xAC4, 0x8D4, 0x4DC, 0x2EC,
185 0xA45, 0x04D, 0x45D, 0x665},
186 {0x688, 0x890, 0x0A0, 0x2A8, 0x4C1, 0x8D1, 0xAD9, 0x2E9, 0x502, 0x70A,
187 0xB1A, 0x122, 0x74B, 0x953, 0x163, 0x36B, 0x404, 0x814, 0xA1C, 0x22C,
188 0x445, 0x64D, 0xA5D, 0x065},
189 {0x888, 0x090, 0x2A0, 0x6A8, 0x501, 0x111, 0xB19, 0x729, 0x402, 0x80A,
190 0xA1A, 0x222, 0x94B, 0x153, 0x363, 0x76B, 0x444, 0x054, 0xA5C, 0x66C,
191 0x4C5, 0x8CD, 0xADD, 0x2E5},
192 {0x288, 0x690, 0x8A0, 0x0A8, 0x441, 0x651, 0xA59, 0x069, 0x4C2, 0x2CA,
193 0xADA, 0x8E2, 0x34B, 0x753, 0x963, 0x16B, 0x504, 0x714, 0xB1C, 0x12C,
194 0x405, 0x20D, 0xA1D, 0x825},
195 {0x088, 0x290, 0x6A0, 0x8A8, 0x401, 0x211, 0xA19, 0x829, 0x442, 0x04A,
196 0xA5A, 0x662, 0x14B, 0x353, 0x763, 0x96B, 0x4C4, 0x2D4, 0xADC, 0x8EC,
197 0x505, 0x10D, 0xB1D, 0x725},
198 {0x648, 0x450, 0x060, 0xA68, 0x2C1, 0x4D1, 0x8D9, 0xAE9, 0x282, 0x68A,
199 0x89A, 0x0A2, 0x70B, 0x513, 0x123, 0xB2B, 0x204, 0x414, 0x81C, 0xA2C,
200 0x345, 0x74D, 0x95D, 0x165},
201 {0xA48, 0x650, 0x460, 0x068, 0x341, 0x751, 0x959, 0x169, 0x2C2, 0xACA,
202 0x8DA, 0x4E2, 0xB0B, 0x713, 0x523, 0x12B, 0x284, 0x694, 0x89C, 0x0AC,
203 0x205, 0xA0D, 0x81D, 0x425},
204 {0x448, 0x050, 0xA60, 0x668, 0x281, 0x091, 0x899, 0x6A9, 0x202, 0x40A,
205 0x81A, 0xA22, 0x50B, 0x113, 0xB23, 0x72B, 0x344, 0x154, 0x95C, 0x76C,
206 0x2C5, 0x4CD, 0x8DD, 0xAE5},
207 {0x048, 0xA50, 0x660, 0x468, 0x201, 0xA11, 0x819, 0x429, 0x342, 0x14A,
208 0x95A, 0x762, 0x10B, 0xB13, 0x723, 0x52B, 0x2C4, 0xAD4, 0x8DC, 0x4EC,
209 0x285, 0x08D, 0x89D, 0x6A5},
210 {0x808, 0xA10, 0x220, 0x428, 0x101, 0xB11, 0x719, 0x529, 0x142, 0x94A,
211 0x75A, 0x362, 0x8CB, 0xAD3, 0x2E3, 0x4EB, 0x044, 0xA54, 0x65C, 0x46C,
212 0x085, 0x88D, 0x69D, 0x2A5},
213 {0xA08, 0x210, 0x420, 0x828, 0x141, 0x351, 0x759, 0x969, 0x042, 0xA4A,
214 0x65A, 0x462, 0xACB, 0x2D3, 0x4E3, 0x8EB, 0x084, 0x294, 0x69C, 0x8AC,
215 0x105, 0xB0D, 0x71D, 0x525},
216 {0x408, 0x810, 0xA20, 0x228, 0x081, 0x891, 0x699, 0x2A9, 0x102, 0x50A,
217 0x71A, 0xB22, 0x4CB, 0x8D3, 0xAE3, 0x2EB, 0x144, 0x954, 0x75C, 0x36C,
218 0x045, 0x44D, 0x65D, 0xA65},
221 _axis_convert_num = {'X': 0, 'Y': 1, 'Z': 2, '-X': 3, '-Y': 4, '-Z': 5}
224 def axis_conversion(from_forward='Y', from_up='Z', to_forward='Y', to_up='Z'):
226 Each argument us an axis in ['X', 'Y', 'Z', '-X', '-Y', '-Z']
227 where the first 2 are a source and the second 2 are the target.
229 from mathutils import Matrix
230 from functools import reduce
232 if from_forward == to_forward and from_up == to_up:
233 return Matrix().to_3x3()
235 if from_forward[-1] == from_up[-1] or to_forward[-1] == to_up[-1]:
236 raise Exception("invalid axis arguments passed, "
237 "can't use up/forward on the same axis.")
239 value = reduce(int.__or__, (_axis_convert_num[a] << (i * 3)
240 for i, a in enumerate((from_forward,
246 for i, axis_lut in enumerate(_axis_convert_lut):
247 if value in axis_lut:
248 return Matrix(_axis_convert_matrix[i])
252 def axis_conversion_ensure(operator, forward_attr, up_attr):
254 Function to ensure an operator has valid axis conversion settings, intended
255 to be used from :class:`bpy.types.Operator.check`.
257 :arg operator: the operator to access axis attributes from.
258 :type operator: :class:`bpy.types.Operator`
259 :arg forward_attr: attribute storing the forward axis
260 :type forward_attr: string
261 :arg up_attr: attribute storing the up axis
262 :type up_attr: string
263 :return: True if the value was modified.
266 def validate(axis_forward, axis_up):
267 if axis_forward[-1] == axis_up[-1]:
268 axis_up = axis_up[0:-1] + 'XYZ'[('XYZ'.index(axis_up[-1]) + 1) % 3]
270 return axis_forward, axis_up
274 axis = getattr(operator, forward_attr), getattr(operator, up_attr)
275 axis_new = validate(*axis)
278 setattr(operator, forward_attr, axis_new[0])
279 setattr(operator, up_attr, axis_new[1])
286 # return a tuple (free, object list), free is True if memory should be freed
287 # later with free_derived_objects()
288 def create_derived_objects(scene, ob):
289 if ob.parent and ob.parent.dupli_type in {'VERTS', 'FACES'}:
292 if ob.dupli_type != 'NONE':
293 ob.dupli_list_create(scene)
294 return True, [(dob.object, dob.matrix) for dob in ob.dupli_list]
296 return False, [(ob, ob.matrix_world)]
299 def free_derived_objects(ob):
300 ob.dupli_list_clear()
303 def unpack_list(list_of_tuples):
305 flat_list_extend = flat_list.extend # a tich faster
306 for t in list_of_tuples:
311 # same as above except that it adds 0 for triangle faces
312 def unpack_face_list(list_of_tuples):
313 #allocate the entire list
314 flat_ls = [0] * (len(list_of_tuples) * 4)
317 for t in list_of_tuples:
322 if t[3] == 0 or t[2] == 0:
323 t = t[2], t[3], t[0], t[1]
325 flat_ls[i:i + len(t)] = t
330 path_reference_mode = EnumProperty(
332 description="Method used to reference paths",
333 items=(('AUTO', "Auto", "Use Relative paths with subdirectories only"),
334 ('ABSOLUTE', "Absolute", "Always write absolute paths"),
335 ('RELATIVE', "Relative", "Always write relative patsh "
337 ('MATCH', "Match", "Match Absolute/Relative "
338 "setting with input path"),
339 ('STRIP', "Strip Path", "Filename only"),
340 ('COPY', "Copy", "copy the file to the destination path "
341 "(or subdirectory)"),
347 def path_reference(filepath,
355 Return a filepath relative to a destination directory, for use with
358 :arg filepath: the file path to return,
359 supporting blenders relative '//' prefix.
360 :type filepath: string
361 :arg base_src: the directory the *filepath* is relative too
362 (normally the blend file).
363 :type base_src: string
364 :arg base_dst: the directory the *filepath* will be referenced from
365 (normally the export path).
366 :type base_dst: string
367 :arg mode: the method used get the path in
368 ['AUTO', 'ABSOLUTE', 'RELATIVE', 'MATCH', 'STRIP', 'COPY']
370 :arg copy_subdir: the subdirectory of *base_dst* to use when mode='COPY'.
371 :type copy_subdir: string
372 :arg copy_set: collect from/to pairs when mode='COPY',
373 pass to *path_reference_copy* when exportign is done.
375 :return: the new filepath.
379 is_relative = filepath.startswith("//")
380 filepath_abs = os.path.normpath(bpy.path.abspath(filepath, base_src))
382 if mode in {'ABSOLUTE', 'RELATIVE', 'STRIP'}:
384 elif mode == 'MATCH':
385 mode = 'RELATIVE' if is_relative else 'ABSOLUTE'
388 if bpy.path.is_subdir(filepath, base_dst)
392 subdir_abs = os.path.join(os.path.normpath(base_dst), copy_subdir)
394 subdir_abs = os.path.normpath(base_dst)
396 filepath_cpy = os.path.join(subdir_abs, os.path.basename(filepath))
398 copy_set.add((filepath_abs, filepath_cpy))
400 filepath_abs = filepath_cpy
403 raise Exception("invalid mode given %r" % mode)
405 if mode == 'ABSOLUTE':
407 elif mode == 'RELATIVE':
408 return os.path.relpath(filepath_abs, base_dst)
409 elif mode == 'STRIP':
410 return os.path.basename(filepath_abs)
413 def path_reference_copy(copy_set, report=print):
415 Execute copying files of path_reference
417 :arg copy_set: set of (from, to) pairs to copy.
419 :arg report: function used for reporting warnings, takes a string argument.
420 :type report: function
428 for file_src, file_dst in copy_set:
429 if not os.path.exists(file_src):
430 report("missing %r, not copying" % file_src)
431 elif os.path.exists(file_dst) and os.path.samefile(file_src, file_dst):
434 dir_to = os.path.dirname(file_dst)
436 if not os.path.isdir(dir_to):
439 shutil.copy(file_src, file_dst)
442 def unique_name(key, name, name_dict, name_max=-1, clean_func=None, sep="."):
444 Helper function for storing unique names which may have special characters
445 stripped and restricted to a maximum length.
447 :arg key: unique item this name belongs to, name_dict[key] will be reused
449 This can be the object, mesh, material, etc instance its self.
450 :type key: any hashable object assosiated with the *name*.
451 :arg name: The name used to create a unique value in *name_dict*.
453 :arg name_dict: This is used to cache namespace to ensure no collisions
454 occur, this should be an empty dict initially and only modified by this
456 :type name_dict: dict
457 :arg clean_func: Function to call on *name* before creating a unique value.
458 :type clean_func: function
459 :arg sep: Separator to use when between the name and a number when a
460 duplicate name is found.
463 name_new = name_dict.get(key)
466 name_dict_values = name_dict.values()
467 name_new = name_new_orig = (name if clean_func is None
468 else clean_func(name))
471 while name_new in name_dict_values:
472 name_new = "%s%s%03d" % (name_new_orig, sep, count)
475 name_new = name_new[:name_max]
476 while name_new in name_dict_values:
477 count_str = "%03d" % count
478 name_new = "%.*s%s%s" % (name_max - (len(count_str) + 1),
485 name_dict[key] = name_new