io_utils.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  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.
  7. #
  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.
  12. #
  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.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8-80 compliant>
  19. __all__ = (
  20. "ExportHelper",
  21. "ImportHelper",
  22. "orientation_helper",
  23. "axis_conversion",
  24. "axis_conversion_ensure",
  25. "create_derived_objects",
  26. "free_derived_objects",
  27. "unpack_list",
  28. "unpack_face_list",
  29. "path_reference",
  30. "path_reference_copy",
  31. "path_reference_mode",
  32. "unique_name"
  33. )
  34. import bpy
  35. from bpy.props import (
  36. BoolProperty,
  37. EnumProperty,
  38. StringProperty,
  39. )
  40. def _check_axis_conversion(op):
  41. if hasattr(op, "axis_forward") and hasattr(op, "axis_up"):
  42. return axis_conversion_ensure(op,
  43. "axis_forward",
  44. "axis_up",
  45. )
  46. return False
  47. class ExportHelper:
  48. filepath: StringProperty(
  49. name="File Path",
  50. description="Filepath used for exporting the file",
  51. maxlen=1024,
  52. subtype='FILE_PATH',
  53. )
  54. check_existing: BoolProperty(
  55. name="Check Existing",
  56. description="Check and warn on overwriting existing files",
  57. default=True,
  58. options={'HIDDEN'},
  59. )
  60. # subclasses can override with decorator
  61. # True == use ext, False == no ext, None == do nothing.
  62. check_extension = True
  63. def invoke(self, context, _event):
  64. import os
  65. if not self.filepath:
  66. blend_filepath = context.blend_data.filepath
  67. if not blend_filepath:
  68. blend_filepath = "untitled"
  69. else:
  70. blend_filepath = os.path.splitext(blend_filepath)[0]
  71. self.filepath = blend_filepath + self.filename_ext
  72. context.window_manager.fileselect_add(self)
  73. return {'RUNNING_MODAL'}
  74. def check(self, _context):
  75. import os
  76. change_ext = False
  77. change_axis = _check_axis_conversion(self)
  78. check_extension = self.check_extension
  79. if check_extension is not None:
  80. filepath = self.filepath
  81. if os.path.basename(filepath):
  82. filepath = bpy.path.ensure_ext(filepath,
  83. self.filename_ext
  84. if check_extension
  85. else "")
  86. if filepath != self.filepath:
  87. self.filepath = filepath
  88. change_ext = True
  89. return (change_ext or change_axis)
  90. class ImportHelper:
  91. filepath: StringProperty(
  92. name="File Path",
  93. description="Filepath used for importing the file",
  94. maxlen=1024,
  95. subtype='FILE_PATH',
  96. )
  97. def invoke(self, context, _event):
  98. context.window_manager.fileselect_add(self)
  99. return {'RUNNING_MODAL'}
  100. def check(self, _context):
  101. return _check_axis_conversion(self)
  102. def orientation_helper(axis_forward='Y', axis_up='Z'):
  103. """
  104. A decorator for import/export classes, generating properties needed by the axis conversion system and IO helpers,
  105. with specified default values (axes).
  106. """
  107. def wrapper(cls):
  108. # Without that, we may end up adding those fields to some **parent** class' __annotations__ property
  109. # (like the ImportHelper or ExportHelper ones)! See T58772.
  110. if "__annotations__" not in cls.__dict__:
  111. setattr(cls, "__annotations__", {})
  112. def _update_axis_forward(self, _context):
  113. if self.axis_forward[-1] == self.axis_up[-1]:
  114. self.axis_up = (self.axis_up[0:-1] +
  115. 'XYZ'[('XYZ'.index(self.axis_up[-1]) + 1) % 3])
  116. cls.__annotations__['axis_forward'] = EnumProperty(
  117. name="Forward",
  118. items=(
  119. ('X', "X Forward", ""),
  120. ('Y', "Y Forward", ""),
  121. ('Z', "Z Forward", ""),
  122. ('-X', "-X Forward", ""),
  123. ('-Y', "-Y Forward", ""),
  124. ('-Z', "-Z Forward", ""),
  125. ),
  126. default=axis_forward,
  127. update=_update_axis_forward,
  128. )
  129. def _update_axis_up(self, _context):
  130. if self.axis_up[-1] == self.axis_forward[-1]:
  131. self.axis_forward = (self.axis_forward[0:-1] +
  132. 'XYZ'[('XYZ'.index(self.axis_forward[-1]) + 1) % 3])
  133. cls.__annotations__['axis_up'] = EnumProperty(
  134. name="Up",
  135. items=(
  136. ('X', "X Up", ""),
  137. ('Y', "Y Up", ""),
  138. ('Z', "Z Up", ""),
  139. ('-X', "-X Up", ""),
  140. ('-Y', "-Y Up", ""),
  141. ('-Z', "-Z Up", ""),
  142. ),
  143. default=axis_up,
  144. update=_update_axis_up,
  145. )
  146. return cls
  147. return wrapper
  148. # Axis conversion function, not pretty LUT
  149. # use lookup table to convert between any axis
  150. _axis_convert_matrix = (
  151. ((-1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)),
  152. ((-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, -1.0, 0.0)),
  153. ((-1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
  154. ((-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, -1.0)),
  155. ((0.0, -1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
  156. ((0.0, 0.0, 1.0), (-1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
  157. ((0.0, 0.0, -1.0), (-1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
  158. ((0.0, 1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
  159. ((0.0, -1.0, 0.0), (0.0, 0.0, 1.0), (-1.0, 0.0, 0.0)),
  160. ((0.0, 0.0, -1.0), (0.0, -1.0, 0.0), (-1.0, 0.0, 0.0)),
  161. ((0.0, 0.0, 1.0), (0.0, 1.0, 0.0), (-1.0, 0.0, 0.0)),
  162. ((0.0, 1.0, 0.0), (0.0, 0.0, -1.0), (-1.0, 0.0, 0.0)),
  163. ((0.0, -1.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0)),
  164. ((0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
  165. ((0.0, 0.0, -1.0), (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
  166. ((0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
  167. ((0.0, -1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
  168. ((0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
  169. ((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
  170. ((0.0, 1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
  171. ((1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, -1.0)),
  172. ((1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
  173. ((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)),
  174. )
  175. # store args as a single int
  176. # (X Y Z -X -Y -Z) --> (0, 1, 2, 3, 4, 5)
  177. # each value is ((src_forward, src_up), (dst_forward, dst_up))
  178. # where all 4 values are or'd into a single value...
  179. # (i1<<0 | i1<<3 | i1<<6 | i1<<9)
  180. _axis_convert_lut = (
  181. {0x8C8, 0x4D0, 0x2E0, 0xAE8, 0x701, 0x511, 0x119, 0xB29, 0x682, 0x88A,
  182. 0x09A, 0x2A2, 0x80B, 0x413, 0x223, 0xA2B, 0x644, 0x454, 0x05C, 0xA6C,
  183. 0x745, 0x94D, 0x15D, 0x365},
  184. {0xAC8, 0x8D0, 0x4E0, 0x2E8, 0x741, 0x951, 0x159, 0x369, 0x702, 0xB0A,
  185. 0x11A, 0x522, 0xA0B, 0x813, 0x423, 0x22B, 0x684, 0x894, 0x09C, 0x2AC,
  186. 0x645, 0xA4D, 0x05D, 0x465},
  187. {0x4C8, 0x2D0, 0xAE0, 0x8E8, 0x681, 0x291, 0x099, 0x8A9, 0x642, 0x44A,
  188. 0x05A, 0xA62, 0x40B, 0x213, 0xA23, 0x82B, 0x744, 0x354, 0x15C, 0x96C,
  189. 0x705, 0x50D, 0x11D, 0xB25},
  190. {0x2C8, 0xAD0, 0x8E0, 0x4E8, 0x641, 0xA51, 0x059, 0x469, 0x742, 0x34A,
  191. 0x15A, 0x962, 0x20B, 0xA13, 0x823, 0x42B, 0x704, 0xB14, 0x11C, 0x52C,
  192. 0x685, 0x28D, 0x09D, 0x8A5},
  193. {0x708, 0xB10, 0x120, 0x528, 0x8C1, 0xAD1, 0x2D9, 0x4E9, 0x942, 0x74A,
  194. 0x35A, 0x162, 0x64B, 0xA53, 0x063, 0x46B, 0x804, 0xA14, 0x21C, 0x42C,
  195. 0x885, 0x68D, 0x29D, 0x0A5},
  196. {0xB08, 0x110, 0x520, 0x728, 0x941, 0x151, 0x359, 0x769, 0x802, 0xA0A,
  197. 0x21A, 0x422, 0xA4B, 0x053, 0x463, 0x66B, 0x884, 0x094, 0x29C, 0x6AC,
  198. 0x8C5, 0xACD, 0x2DD, 0x4E5},
  199. {0x508, 0x710, 0xB20, 0x128, 0x881, 0x691, 0x299, 0x0A9, 0x8C2, 0x4CA,
  200. 0x2DA, 0xAE2, 0x44B, 0x653, 0xA63, 0x06B, 0x944, 0x754, 0x35C, 0x16C,
  201. 0x805, 0x40D, 0x21D, 0xA25},
  202. {0x108, 0x510, 0x720, 0xB28, 0x801, 0x411, 0x219, 0xA29, 0x882, 0x08A,
  203. 0x29A, 0x6A2, 0x04B, 0x453, 0x663, 0xA6B, 0x8C4, 0x4D4, 0x2DC, 0xAEC,
  204. 0x945, 0x14D, 0x35D, 0x765},
  205. {0x748, 0x350, 0x160, 0x968, 0xAC1, 0x2D1, 0x4D9, 0x8E9, 0xA42, 0x64A,
  206. 0x45A, 0x062, 0x68B, 0x293, 0x0A3, 0x8AB, 0xA04, 0x214, 0x41C, 0x82C,
  207. 0xB05, 0x70D, 0x51D, 0x125},
  208. {0x948, 0x750, 0x360, 0x168, 0xB01, 0x711, 0x519, 0x129, 0xAC2, 0x8CA,
  209. 0x4DA, 0x2E2, 0x88B, 0x693, 0x2A3, 0x0AB, 0xA44, 0x654, 0x45C, 0x06C,
  210. 0xA05, 0x80D, 0x41D, 0x225},
  211. {0x348, 0x150, 0x960, 0x768, 0xA41, 0x051, 0x459, 0x669, 0xA02, 0x20A,
  212. 0x41A, 0x822, 0x28B, 0x093, 0x8A3, 0x6AB, 0xB04, 0x114, 0x51C, 0x72C,
  213. 0xAC5, 0x2CD, 0x4DD, 0x8E5},
  214. {0x148, 0x950, 0x760, 0x368, 0xA01, 0x811, 0x419, 0x229, 0xB02, 0x10A,
  215. 0x51A, 0x722, 0x08B, 0x893, 0x6A3, 0x2AB, 0xAC4, 0x8D4, 0x4DC, 0x2EC,
  216. 0xA45, 0x04D, 0x45D, 0x665},
  217. {0x688, 0x890, 0x0A0, 0x2A8, 0x4C1, 0x8D1, 0xAD9, 0x2E9, 0x502, 0x70A,
  218. 0xB1A, 0x122, 0x74B, 0x953, 0x163, 0x36B, 0x404, 0x814, 0xA1C, 0x22C,
  219. 0x445, 0x64D, 0xA5D, 0x065},
  220. {0x888, 0x090, 0x2A0, 0x6A8, 0x501, 0x111, 0xB19, 0x729, 0x402, 0x80A,
  221. 0xA1A, 0x222, 0x94B, 0x153, 0x363, 0x76B, 0x444, 0x054, 0xA5C, 0x66C,
  222. 0x4C5, 0x8CD, 0xADD, 0x2E5},
  223. {0x288, 0x690, 0x8A0, 0x0A8, 0x441, 0x651, 0xA59, 0x069, 0x4C2, 0x2CA,
  224. 0xADA, 0x8E2, 0x34B, 0x753, 0x963, 0x16B, 0x504, 0x714, 0xB1C, 0x12C,
  225. 0x405, 0x20D, 0xA1D, 0x825},
  226. {0x088, 0x290, 0x6A0, 0x8A8, 0x401, 0x211, 0xA19, 0x829, 0x442, 0x04A,
  227. 0xA5A, 0x662, 0x14B, 0x353, 0x763, 0x96B, 0x4C4, 0x2D4, 0xADC, 0x8EC,
  228. 0x505, 0x10D, 0xB1D, 0x725},
  229. {0x648, 0x450, 0x060, 0xA68, 0x2C1, 0x4D1, 0x8D9, 0xAE9, 0x282, 0x68A,
  230. 0x89A, 0x0A2, 0x70B, 0x513, 0x123, 0xB2B, 0x204, 0x414, 0x81C, 0xA2C,
  231. 0x345, 0x74D, 0x95D, 0x165},
  232. {0xA48, 0x650, 0x460, 0x068, 0x341, 0x751, 0x959, 0x169, 0x2C2, 0xACA,
  233. 0x8DA, 0x4E2, 0xB0B, 0x713, 0x523, 0x12B, 0x284, 0x694, 0x89C, 0x0AC,
  234. 0x205, 0xA0D, 0x81D, 0x425},
  235. {0x448, 0x050, 0xA60, 0x668, 0x281, 0x091, 0x899, 0x6A9, 0x202, 0x40A,
  236. 0x81A, 0xA22, 0x50B, 0x113, 0xB23, 0x72B, 0x344, 0x154, 0x95C, 0x76C,
  237. 0x2C5, 0x4CD, 0x8DD, 0xAE5},
  238. {0x048, 0xA50, 0x660, 0x468, 0x201, 0xA11, 0x819, 0x429, 0x342, 0x14A,
  239. 0x95A, 0x762, 0x10B, 0xB13, 0x723, 0x52B, 0x2C4, 0xAD4, 0x8DC, 0x4EC,
  240. 0x285, 0x08D, 0x89D, 0x6A5},
  241. {0x808, 0xA10, 0x220, 0x428, 0x101, 0xB11, 0x719, 0x529, 0x142, 0x94A,
  242. 0x75A, 0x362, 0x8CB, 0xAD3, 0x2E3, 0x4EB, 0x044, 0xA54, 0x65C, 0x46C,
  243. 0x085, 0x88D, 0x69D, 0x2A5},
  244. {0xA08, 0x210, 0x420, 0x828, 0x141, 0x351, 0x759, 0x969, 0x042, 0xA4A,
  245. 0x65A, 0x462, 0xACB, 0x2D3, 0x4E3, 0x8EB, 0x084, 0x294, 0x69C, 0x8AC,
  246. 0x105, 0xB0D, 0x71D, 0x525},
  247. {0x408, 0x810, 0xA20, 0x228, 0x081, 0x891, 0x699, 0x2A9, 0x102, 0x50A,
  248. 0x71A, 0xB22, 0x4CB, 0x8D3, 0xAE3, 0x2EB, 0x144, 0x954, 0x75C, 0x36C,
  249. 0x045, 0x44D, 0x65D, 0xA65},
  250. )
  251. _axis_convert_num = {'X': 0, 'Y': 1, 'Z': 2, '-X': 3, '-Y': 4, '-Z': 5}
  252. def axis_conversion(from_forward='Y', from_up='Z', to_forward='Y', to_up='Z'):
  253. """
  254. Each argument us an axis in ['X', 'Y', 'Z', '-X', '-Y', '-Z']
  255. where the first 2 are a source and the second 2 are the target.
  256. """
  257. from mathutils import Matrix
  258. from functools import reduce
  259. if from_forward == to_forward and from_up == to_up:
  260. return Matrix().to_3x3()
  261. if from_forward[-1] == from_up[-1] or to_forward[-1] == to_up[-1]:
  262. raise Exception("Invalid axis arguments passed, "
  263. "can't use up/forward on the same axis")
  264. value = reduce(int.__or__, (_axis_convert_num[a] << (i * 3)
  265. for i, a in enumerate((from_forward,
  266. from_up,
  267. to_forward,
  268. to_up,
  269. ))))
  270. for i, axis_lut in enumerate(_axis_convert_lut):
  271. if value in axis_lut:
  272. return Matrix(_axis_convert_matrix[i])
  273. assert(0)
  274. def axis_conversion_ensure(operator, forward_attr, up_attr):
  275. """
  276. Function to ensure an operator has valid axis conversion settings, intended
  277. to be used from :class:`bpy.types.Operator.check`.
  278. :arg operator: the operator to access axis attributes from.
  279. :type operator: :class:`bpy.types.Operator`
  280. :arg forward_attr: attribute storing the forward axis
  281. :type forward_attr: string
  282. :arg up_attr: attribute storing the up axis
  283. :type up_attr: string
  284. :return: True if the value was modified.
  285. :rtype: boolean
  286. """
  287. def validate(axis_forward, axis_up):
  288. if axis_forward[-1] == axis_up[-1]:
  289. axis_up = axis_up[0:-1] + 'XYZ'[('XYZ'.index(axis_up[-1]) + 1) % 3]
  290. return axis_forward, axis_up
  291. axis = getattr(operator, forward_attr), getattr(operator, up_attr)
  292. axis_new = validate(*axis)
  293. if axis != axis_new:
  294. setattr(operator, forward_attr, axis_new[0])
  295. setattr(operator, up_attr, axis_new[1])
  296. return True
  297. else:
  298. return False
  299. # return a tuple (free, object list), free is True if memory should be freed
  300. # later with free_derived_objects()
  301. def create_derived_objects(scene, ob):
  302. if ob.parent and ob.parent.instance_type in {'VERTS', 'FACES'}:
  303. return False, None
  304. if ob.instance_type != 'NONE':
  305. ob.dupli_list_create(scene)
  306. return True, [(dob.object, dob.matrix) for dob in ob.dupli_list]
  307. else:
  308. return False, [(ob, ob.matrix_world)]
  309. def free_derived_objects(ob):
  310. ob.dupli_list_clear()
  311. def unpack_list(list_of_tuples):
  312. flat_list = []
  313. flat_list_extend = flat_list.extend # a tiny bit faster
  314. for t in list_of_tuples:
  315. flat_list_extend(t)
  316. return flat_list
  317. # same as above except that it adds 0 for triangle faces
  318. def unpack_face_list(list_of_tuples):
  319. # allocate the entire list
  320. flat_ls = [0] * (len(list_of_tuples) * 4)
  321. i = 0
  322. for t in list_of_tuples:
  323. if len(t) == 3:
  324. if t[2] == 0:
  325. t = t[1], t[2], t[0]
  326. else: # assume quad
  327. if t[3] == 0 or t[2] == 0:
  328. t = t[2], t[3], t[0], t[1]
  329. flat_ls[i:i + len(t)] = t
  330. i += 4
  331. return flat_ls
  332. path_reference_mode = EnumProperty(
  333. name="Path Mode",
  334. description="Method used to reference paths",
  335. items=(
  336. ('AUTO', "Auto", "Use Relative paths with subdirectories only"),
  337. ('ABSOLUTE', "Absolute", "Always write absolute paths"),
  338. ('RELATIVE', "Relative", "Always write relative paths "
  339. "(where possible)"),
  340. ('MATCH', "Match", "Match Absolute/Relative "
  341. "setting with input path"),
  342. ('STRIP', "Strip Path", "Filename only"),
  343. ('COPY', "Copy", "Copy the file to the destination path "
  344. "(or subdirectory)"),
  345. ),
  346. default='AUTO',
  347. )
  348. def path_reference(filepath,
  349. base_src,
  350. base_dst,
  351. mode='AUTO',
  352. copy_subdir="",
  353. copy_set=None,
  354. library=None,
  355. ):
  356. """
  357. Return a filepath relative to a destination directory, for use with
  358. exporters.
  359. :arg filepath: the file path to return,
  360. supporting blenders relative '//' prefix.
  361. :type filepath: string
  362. :arg base_src: the directory the *filepath* is relative too
  363. (normally the blend file).
  364. :type base_src: string
  365. :arg base_dst: the directory the *filepath* will be referenced from
  366. (normally the export path).
  367. :type base_dst: string
  368. :arg mode: the method used get the path in
  369. ['AUTO', 'ABSOLUTE', 'RELATIVE', 'MATCH', 'STRIP', 'COPY']
  370. :type mode: string
  371. :arg copy_subdir: the subdirectory of *base_dst* to use when mode='COPY'.
  372. :type copy_subdir: string
  373. :arg copy_set: collect from/to pairs when mode='COPY',
  374. pass to *path_reference_copy* when exporting is done.
  375. :type copy_set: set
  376. :arg library: The library this path is relative to.
  377. :type library: :class:`bpy.types.Library` or None
  378. :return: the new filepath.
  379. :rtype: string
  380. """
  381. import os
  382. is_relative = filepath.startswith("//")
  383. filepath_abs = bpy.path.abspath(filepath, base_src, library)
  384. filepath_abs = os.path.normpath(filepath_abs)
  385. if mode in {'ABSOLUTE', 'RELATIVE', 'STRIP'}:
  386. pass
  387. elif mode == 'MATCH':
  388. mode = 'RELATIVE' if is_relative else 'ABSOLUTE'
  389. elif mode == 'AUTO':
  390. mode = ('RELATIVE'
  391. if bpy.path.is_subdir(filepath_abs, base_dst)
  392. else 'ABSOLUTE')
  393. elif mode == 'COPY':
  394. subdir_abs = os.path.normpath(base_dst)
  395. if copy_subdir:
  396. subdir_abs = os.path.join(subdir_abs, copy_subdir)
  397. filepath_cpy = os.path.join(subdir_abs, os.path.basename(filepath))
  398. copy_set.add((filepath_abs, filepath_cpy))
  399. filepath_abs = filepath_cpy
  400. mode = 'RELATIVE'
  401. else:
  402. raise Exception("invalid mode given %r" % mode)
  403. if mode == 'ABSOLUTE':
  404. return filepath_abs
  405. elif mode == 'RELATIVE':
  406. # can't always find the relative path
  407. # (between drive letters on windows)
  408. try:
  409. return os.path.relpath(filepath_abs, base_dst)
  410. except ValueError:
  411. return filepath_abs
  412. elif mode == 'STRIP':
  413. return os.path.basename(filepath_abs)
  414. def path_reference_copy(copy_set, report=print):
  415. """
  416. Execute copying files of path_reference
  417. :arg copy_set: set of (from, to) pairs to copy.
  418. :type copy_set: set
  419. :arg report: function used for reporting warnings, takes a string argument.
  420. :type report: function
  421. """
  422. if not copy_set:
  423. return
  424. import os
  425. import shutil
  426. for file_src, file_dst in copy_set:
  427. if not os.path.exists(file_src):
  428. report("missing %r, not copying" % file_src)
  429. elif os.path.exists(file_dst) and os.path.samefile(file_src, file_dst):
  430. pass
  431. else:
  432. dir_to = os.path.dirname(file_dst)
  433. try:
  434. os.makedirs(dir_to, exist_ok=True)
  435. except:
  436. import traceback
  437. traceback.print_exc()
  438. try:
  439. shutil.copy(file_src, file_dst)
  440. except:
  441. import traceback
  442. traceback.print_exc()
  443. def unique_name(key, name, name_dict, name_max=-1, clean_func=None, sep="."):
  444. """
  445. Helper function for storing unique names which may have special characters
  446. stripped and restricted to a maximum length.
  447. :arg key: unique item this name belongs to, name_dict[key] will be reused
  448. when available.
  449. This can be the object, mesh, material, etc instance its self.
  450. :type key: any hashable object associated with the *name*.
  451. :arg name: The name used to create a unique value in *name_dict*.
  452. :type name: string
  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
  455. function.
  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.
  461. :type sep: string
  462. """
  463. name_new = name_dict.get(key)
  464. if name_new is None:
  465. count = 1
  466. name_dict_values = name_dict.values()
  467. name_new = name_new_orig = (name if clean_func is None
  468. else clean_func(name))
  469. if name_max == -1:
  470. while name_new in name_dict_values:
  471. name_new = "%s%s%03d" % (name_new_orig, sep, count)
  472. count += 1
  473. else:
  474. name_new = name_new[:name_max]
  475. while name_new in name_dict_values:
  476. count_str = "%03d" % count
  477. name_new = "%.*s%s%s" % (name_max - (len(count_str) + 1),
  478. name_new_orig,
  479. sep,
  480. count_str,
  481. )
  482. count += 1
  483. name_dict[key] = name_new
  484. return name_new