2023-06-15 13:09:04 +10:00
|
|
|
# SPDX-FileCopyrightText: 2010-2023 Blender Foundation
|
|
|
|
#
|
2022-02-11 09:07:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2011-05-28 07:47:58 +00:00
|
|
|
__all__ = (
|
|
|
|
"ExportHelper",
|
|
|
|
"ImportHelper",
|
2018-09-21 19:28:39 +02:00
|
|
|
"orientation_helper",
|
2011-05-28 07:47:58 +00:00
|
|
|
"axis_conversion",
|
2011-07-18 05:07:54 +00:00
|
|
|
"axis_conversion_ensure",
|
2011-05-28 07:47:58 +00:00
|
|
|
"create_derived_objects",
|
|
|
|
"unpack_list",
|
|
|
|
"unpack_face_list",
|
|
|
|
"path_reference",
|
|
|
|
"path_reference_copy",
|
|
|
|
"path_reference_mode",
|
2022-12-15 17:24:23 +11:00
|
|
|
"unique_name",
|
2018-07-03 06:27:53 +02:00
|
|
|
)
|
2011-05-28 07:47:58 +00:00
|
|
|
|
2010-09-01 13:55:41 +00:00
|
|
|
import bpy
|
2015-05-17 17:17:31 +10:00
|
|
|
from bpy.props import (
|
2017-11-29 18:00:41 +11:00
|
|
|
BoolProperty,
|
|
|
|
EnumProperty,
|
|
|
|
StringProperty,
|
|
|
|
)
|
2022-09-05 15:25:34 +02:00
|
|
|
from bpy.app.translations import pgettext_data as data_
|
2010-09-07 15:17:42 +00:00
|
|
|
|
2022-09-05 17:24:52 +02:00
|
|
|
|
2011-08-03 05:32:07 +00:00
|
|
|
def _check_axis_conversion(op):
|
|
|
|
if hasattr(op, "axis_forward") and hasattr(op, "axis_up"):
|
2019-09-16 16:16:25 +10:00
|
|
|
return axis_conversion_ensure(
|
|
|
|
op,
|
|
|
|
"axis_forward",
|
|
|
|
"axis_up",
|
|
|
|
)
|
2011-08-03 05:32:07 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2010-09-01 02:25:49 +00:00
|
|
|
class ExportHelper:
|
2018-07-11 22:18:09 +02:00
|
|
|
filepath: StringProperty(
|
2018-07-03 06:27:53 +02:00
|
|
|
name="File Path",
|
|
|
|
description="Filepath used for exporting the file",
|
|
|
|
maxlen=1024,
|
|
|
|
subtype='FILE_PATH',
|
|
|
|
)
|
2018-07-11 22:18:09 +02:00
|
|
|
check_existing: BoolProperty(
|
2018-07-03 06:27:53 +02:00
|
|
|
name="Check Existing",
|
|
|
|
description="Check and warn on overwriting existing files",
|
|
|
|
default=True,
|
|
|
|
options={'HIDDEN'},
|
|
|
|
)
|
2011-03-14 23:17:52 +00:00
|
|
|
|
2011-03-07 08:01:38 +00:00
|
|
|
# subclasses can override with decorator
|
|
|
|
# True == use ext, False == no ext, None == do nothing.
|
|
|
|
check_extension = True
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2019-05-09 13:11:36 +10:00
|
|
|
def invoke(self, context, _event):
|
2010-09-01 13:55:41 +00:00
|
|
|
import os
|
2010-09-09 18:03:57 +00:00
|
|
|
if not self.filepath:
|
2010-09-22 13:24:21 +00:00
|
|
|
blend_filepath = context.blend_data.filepath
|
|
|
|
if not blend_filepath:
|
2022-09-05 15:25:34 +02:00
|
|
|
blend_filepath = data_("untitled")
|
2010-09-22 13:24:21 +00:00
|
|
|
else:
|
|
|
|
blend_filepath = os.path.splitext(blend_filepath)[0]
|
|
|
|
|
|
|
|
self.filepath = blend_filepath + self.filename_ext
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2010-12-08 11:42:11 +00:00
|
|
|
context.window_manager.fileselect_add(self)
|
2010-09-01 13:55:41 +00:00
|
|
|
return {'RUNNING_MODAL'}
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2019-05-09 13:11:36 +10:00
|
|
|
def check(self, _context):
|
2013-01-15 04:33:08 +00:00
|
|
|
import os
|
2011-08-03 05:32:07 +00:00
|
|
|
change_ext = False
|
|
|
|
change_axis = _check_axis_conversion(self)
|
2011-03-14 23:17:52 +00:00
|
|
|
|
2011-08-03 05:32:07 +00:00
|
|
|
check_extension = self.check_extension
|
2011-03-07 08:01:38 +00:00
|
|
|
|
2011-08-03 05:32:07 +00:00
|
|
|
if check_extension is not None:
|
2013-01-15 04:33:08 +00:00
|
|
|
filepath = self.filepath
|
|
|
|
if os.path.basename(filepath):
|
2021-04-28 22:53:35 +10:00
|
|
|
if check_extension:
|
|
|
|
filepath = bpy.path.ensure_ext(
|
|
|
|
os.path.splitext(filepath)[0],
|
|
|
|
self.filename_ext,
|
|
|
|
)
|
2013-01-15 04:33:08 +00:00
|
|
|
if filepath != self.filepath:
|
|
|
|
self.filepath = filepath
|
|
|
|
change_ext = True
|
2011-03-07 08:01:38 +00:00
|
|
|
|
2011-08-03 05:32:07 +00:00
|
|
|
return (change_ext or change_axis)
|
2010-09-17 09:27:31 +00:00
|
|
|
|
2010-09-01 02:25:49 +00:00
|
|
|
|
|
|
|
class ImportHelper:
|
2018-07-11 22:18:09 +02:00
|
|
|
filepath: StringProperty(
|
2018-07-03 06:27:53 +02:00
|
|
|
name="File Path",
|
|
|
|
description="Filepath used for importing the file",
|
|
|
|
maxlen=1024,
|
|
|
|
subtype='FILE_PATH',
|
|
|
|
)
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2019-05-09 13:11:36 +10:00
|
|
|
def invoke(self, context, _event):
|
2010-12-08 11:42:11 +00:00
|
|
|
context.window_manager.fileselect_add(self)
|
2010-09-01 13:55:41 +00:00
|
|
|
return {'RUNNING_MODAL'}
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2019-05-09 13:11:36 +10:00
|
|
|
def check(self, _context):
|
2011-08-03 05:32:07 +00:00
|
|
|
return _check_axis_conversion(self)
|
|
|
|
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2018-09-21 19:28:39 +02:00
|
|
|
def orientation_helper(axis_forward='Y', axis_up='Z'):
|
2018-09-30 17:30:29 +02:00
|
|
|
"""
|
|
|
|
A decorator for import/export classes, generating properties needed by the axis conversion system and IO helpers,
|
|
|
|
with specified default values (axes).
|
|
|
|
"""
|
2018-09-21 19:28:39 +02:00
|
|
|
def wrapper(cls):
|
2018-12-05 10:57:08 +01:00
|
|
|
# Without that, we may end up adding those fields to some **parent** class' __annotations__ property
|
2023-02-12 14:37:16 +11:00
|
|
|
# (like the ImportHelper or ExportHelper ones)! See #58772.
|
2018-12-05 10:57:08 +01:00
|
|
|
if "__annotations__" not in cls.__dict__:
|
2018-12-05 20:52:47 +01:00
|
|
|
setattr(cls, "__annotations__", {})
|
2018-12-05 10:57:08 +01:00
|
|
|
|
2019-05-09 13:11:36 +10:00
|
|
|
def _update_axis_forward(self, _context):
|
2018-09-21 19:28:39 +02:00
|
|
|
if self.axis_forward[-1] == self.axis_up[-1]:
|
2019-09-16 16:16:25 +10:00
|
|
|
self.axis_up = (
|
|
|
|
self.axis_up[0:-1] +
|
|
|
|
'XYZ'[('XYZ'.index(self.axis_up[-1]) + 1) % 3]
|
|
|
|
)
|
2018-09-21 19:28:39 +02:00
|
|
|
|
2023-04-18 10:42:00 +10:00
|
|
|
cls.__annotations__["axis_forward"] = EnumProperty(
|
2018-09-21 19:28:39 +02:00
|
|
|
name="Forward",
|
|
|
|
items=(
|
|
|
|
('X', "X Forward", ""),
|
|
|
|
('Y', "Y Forward", ""),
|
|
|
|
('Z', "Z Forward", ""),
|
|
|
|
('-X', "-X Forward", ""),
|
|
|
|
('-Y', "-Y Forward", ""),
|
|
|
|
('-Z', "-Z Forward", ""),
|
|
|
|
),
|
|
|
|
default=axis_forward,
|
|
|
|
update=_update_axis_forward,
|
|
|
|
)
|
|
|
|
|
2019-05-09 13:11:36 +10:00
|
|
|
def _update_axis_up(self, _context):
|
2018-09-21 19:28:39 +02:00
|
|
|
if self.axis_up[-1] == self.axis_forward[-1]:
|
2019-09-16 16:16:25 +10:00
|
|
|
self.axis_forward = (
|
|
|
|
self.axis_forward[0:-1] +
|
|
|
|
'XYZ'[('XYZ'.index(self.axis_forward[-1]) + 1) % 3]
|
|
|
|
)
|
2018-09-21 19:28:39 +02:00
|
|
|
|
2023-04-18 10:42:00 +10:00
|
|
|
cls.__annotations__["axis_up"] = EnumProperty(
|
2018-09-21 19:28:39 +02:00
|
|
|
name="Up",
|
|
|
|
items=(
|
|
|
|
('X', "X Up", ""),
|
|
|
|
('Y', "Y Up", ""),
|
|
|
|
('Z', "Z Up", ""),
|
|
|
|
('-X', "-X Up", ""),
|
|
|
|
('-Y', "-Y Up", ""),
|
|
|
|
('-Z', "-Z Up", ""),
|
|
|
|
),
|
|
|
|
default=axis_up,
|
|
|
|
update=_update_axis_up,
|
|
|
|
)
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2011-05-07 11:25:59 +00:00
|
|
|
# Axis conversion function, not pretty LUT
|
2011-10-17 06:58:07 +00:00
|
|
|
# use lookup table to convert between any axis
|
2011-05-07 11:25:59 +00:00
|
|
|
_axis_convert_matrix = (
|
|
|
|
((-1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)),
|
|
|
|
((-1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, -1.0, 0.0)),
|
|
|
|
((-1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
|
|
|
|
((-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, -1.0)),
|
|
|
|
((0.0, -1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
|
2011-12-22 11:06:41 +00:00
|
|
|
((0.0, 0.0, 1.0), (-1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
|
2011-05-07 11:25:59 +00:00
|
|
|
((0.0, 0.0, -1.0), (-1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
|
2011-12-22 11:06:41 +00:00
|
|
|
((0.0, 1.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
|
|
|
|
((0.0, -1.0, 0.0), (0.0, 0.0, 1.0), (-1.0, 0.0, 0.0)),
|
2011-05-07 11:25:59 +00:00
|
|
|
((0.0, 0.0, -1.0), (0.0, -1.0, 0.0), (-1.0, 0.0, 0.0)),
|
|
|
|
((0.0, 0.0, 1.0), (0.0, 1.0, 0.0), (-1.0, 0.0, 0.0)),
|
|
|
|
((0.0, 1.0, 0.0), (0.0, 0.0, -1.0), (-1.0, 0.0, 0.0)),
|
2011-12-22 11:06:41 +00:00
|
|
|
((0.0, -1.0, 0.0), (0.0, 0.0, -1.0), (1.0, 0.0, 0.0)),
|
|
|
|
((0.0, 0.0, 1.0), (0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
|
|
|
|
((0.0, 0.0, -1.0), (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
|
2011-05-07 11:25:59 +00:00
|
|
|
((0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
|
2011-12-22 11:06:41 +00:00
|
|
|
((0.0, -1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
|
|
|
|
((0.0, 0.0, -1.0), (1.0, 0.0, 0.0), (0.0, -1.0, 0.0)),
|
|
|
|
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
|
2011-05-07 11:25:59 +00:00
|
|
|
((0.0, 1.0, 0.0), (1.0, 0.0, 0.0), (0.0, 0.0, -1.0)),
|
|
|
|
((1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, -1.0)),
|
|
|
|
((1.0, 0.0, 0.0), (0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
|
2011-12-22 11:06:41 +00:00
|
|
|
((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)),
|
2018-07-03 06:27:53 +02:00
|
|
|
)
|
2011-05-07 11:25:59 +00:00
|
|
|
|
|
|
|
# store args as a single int
|
|
|
|
# (X Y Z -X -Y -Z) --> (0, 1, 2, 3, 4, 5)
|
|
|
|
# each value is ((src_forward, src_up), (dst_forward, dst_up))
|
|
|
|
# where all 4 values are or'd into a single value...
|
|
|
|
# (i1<<0 | i1<<3 | i1<<6 | i1<<9)
|
|
|
|
_axis_convert_lut = (
|
2011-07-31 03:15:37 +00:00
|
|
|
{0x8C8, 0x4D0, 0x2E0, 0xAE8, 0x701, 0x511, 0x119, 0xB29, 0x682, 0x88A,
|
|
|
|
0x09A, 0x2A2, 0x80B, 0x413, 0x223, 0xA2B, 0x644, 0x454, 0x05C, 0xA6C,
|
|
|
|
0x745, 0x94D, 0x15D, 0x365},
|
|
|
|
{0xAC8, 0x8D0, 0x4E0, 0x2E8, 0x741, 0x951, 0x159, 0x369, 0x702, 0xB0A,
|
|
|
|
0x11A, 0x522, 0xA0B, 0x813, 0x423, 0x22B, 0x684, 0x894, 0x09C, 0x2AC,
|
|
|
|
0x645, 0xA4D, 0x05D, 0x465},
|
|
|
|
{0x4C8, 0x2D0, 0xAE0, 0x8E8, 0x681, 0x291, 0x099, 0x8A9, 0x642, 0x44A,
|
|
|
|
0x05A, 0xA62, 0x40B, 0x213, 0xA23, 0x82B, 0x744, 0x354, 0x15C, 0x96C,
|
|
|
|
0x705, 0x50D, 0x11D, 0xB25},
|
|
|
|
{0x2C8, 0xAD0, 0x8E0, 0x4E8, 0x641, 0xA51, 0x059, 0x469, 0x742, 0x34A,
|
|
|
|
0x15A, 0x962, 0x20B, 0xA13, 0x823, 0x42B, 0x704, 0xB14, 0x11C, 0x52C,
|
|
|
|
0x685, 0x28D, 0x09D, 0x8A5},
|
|
|
|
{0x708, 0xB10, 0x120, 0x528, 0x8C1, 0xAD1, 0x2D9, 0x4E9, 0x942, 0x74A,
|
|
|
|
0x35A, 0x162, 0x64B, 0xA53, 0x063, 0x46B, 0x804, 0xA14, 0x21C, 0x42C,
|
|
|
|
0x885, 0x68D, 0x29D, 0x0A5},
|
|
|
|
{0xB08, 0x110, 0x520, 0x728, 0x941, 0x151, 0x359, 0x769, 0x802, 0xA0A,
|
|
|
|
0x21A, 0x422, 0xA4B, 0x053, 0x463, 0x66B, 0x884, 0x094, 0x29C, 0x6AC,
|
|
|
|
0x8C5, 0xACD, 0x2DD, 0x4E5},
|
|
|
|
{0x508, 0x710, 0xB20, 0x128, 0x881, 0x691, 0x299, 0x0A9, 0x8C2, 0x4CA,
|
|
|
|
0x2DA, 0xAE2, 0x44B, 0x653, 0xA63, 0x06B, 0x944, 0x754, 0x35C, 0x16C,
|
|
|
|
0x805, 0x40D, 0x21D, 0xA25},
|
|
|
|
{0x108, 0x510, 0x720, 0xB28, 0x801, 0x411, 0x219, 0xA29, 0x882, 0x08A,
|
|
|
|
0x29A, 0x6A2, 0x04B, 0x453, 0x663, 0xA6B, 0x8C4, 0x4D4, 0x2DC, 0xAEC,
|
|
|
|
0x945, 0x14D, 0x35D, 0x765},
|
|
|
|
{0x748, 0x350, 0x160, 0x968, 0xAC1, 0x2D1, 0x4D9, 0x8E9, 0xA42, 0x64A,
|
|
|
|
0x45A, 0x062, 0x68B, 0x293, 0x0A3, 0x8AB, 0xA04, 0x214, 0x41C, 0x82C,
|
|
|
|
0xB05, 0x70D, 0x51D, 0x125},
|
|
|
|
{0x948, 0x750, 0x360, 0x168, 0xB01, 0x711, 0x519, 0x129, 0xAC2, 0x8CA,
|
|
|
|
0x4DA, 0x2E2, 0x88B, 0x693, 0x2A3, 0x0AB, 0xA44, 0x654, 0x45C, 0x06C,
|
|
|
|
0xA05, 0x80D, 0x41D, 0x225},
|
|
|
|
{0x348, 0x150, 0x960, 0x768, 0xA41, 0x051, 0x459, 0x669, 0xA02, 0x20A,
|
|
|
|
0x41A, 0x822, 0x28B, 0x093, 0x8A3, 0x6AB, 0xB04, 0x114, 0x51C, 0x72C,
|
|
|
|
0xAC5, 0x2CD, 0x4DD, 0x8E5},
|
|
|
|
{0x148, 0x950, 0x760, 0x368, 0xA01, 0x811, 0x419, 0x229, 0xB02, 0x10A,
|
|
|
|
0x51A, 0x722, 0x08B, 0x893, 0x6A3, 0x2AB, 0xAC4, 0x8D4, 0x4DC, 0x2EC,
|
|
|
|
0xA45, 0x04D, 0x45D, 0x665},
|
|
|
|
{0x688, 0x890, 0x0A0, 0x2A8, 0x4C1, 0x8D1, 0xAD9, 0x2E9, 0x502, 0x70A,
|
|
|
|
0xB1A, 0x122, 0x74B, 0x953, 0x163, 0x36B, 0x404, 0x814, 0xA1C, 0x22C,
|
|
|
|
0x445, 0x64D, 0xA5D, 0x065},
|
|
|
|
{0x888, 0x090, 0x2A0, 0x6A8, 0x501, 0x111, 0xB19, 0x729, 0x402, 0x80A,
|
|
|
|
0xA1A, 0x222, 0x94B, 0x153, 0x363, 0x76B, 0x444, 0x054, 0xA5C, 0x66C,
|
|
|
|
0x4C5, 0x8CD, 0xADD, 0x2E5},
|
|
|
|
{0x288, 0x690, 0x8A0, 0x0A8, 0x441, 0x651, 0xA59, 0x069, 0x4C2, 0x2CA,
|
|
|
|
0xADA, 0x8E2, 0x34B, 0x753, 0x963, 0x16B, 0x504, 0x714, 0xB1C, 0x12C,
|
|
|
|
0x405, 0x20D, 0xA1D, 0x825},
|
|
|
|
{0x088, 0x290, 0x6A0, 0x8A8, 0x401, 0x211, 0xA19, 0x829, 0x442, 0x04A,
|
|
|
|
0xA5A, 0x662, 0x14B, 0x353, 0x763, 0x96B, 0x4C4, 0x2D4, 0xADC, 0x8EC,
|
|
|
|
0x505, 0x10D, 0xB1D, 0x725},
|
|
|
|
{0x648, 0x450, 0x060, 0xA68, 0x2C1, 0x4D1, 0x8D9, 0xAE9, 0x282, 0x68A,
|
|
|
|
0x89A, 0x0A2, 0x70B, 0x513, 0x123, 0xB2B, 0x204, 0x414, 0x81C, 0xA2C,
|
|
|
|
0x345, 0x74D, 0x95D, 0x165},
|
|
|
|
{0xA48, 0x650, 0x460, 0x068, 0x341, 0x751, 0x959, 0x169, 0x2C2, 0xACA,
|
|
|
|
0x8DA, 0x4E2, 0xB0B, 0x713, 0x523, 0x12B, 0x284, 0x694, 0x89C, 0x0AC,
|
|
|
|
0x205, 0xA0D, 0x81D, 0x425},
|
|
|
|
{0x448, 0x050, 0xA60, 0x668, 0x281, 0x091, 0x899, 0x6A9, 0x202, 0x40A,
|
|
|
|
0x81A, 0xA22, 0x50B, 0x113, 0xB23, 0x72B, 0x344, 0x154, 0x95C, 0x76C,
|
|
|
|
0x2C5, 0x4CD, 0x8DD, 0xAE5},
|
|
|
|
{0x048, 0xA50, 0x660, 0x468, 0x201, 0xA11, 0x819, 0x429, 0x342, 0x14A,
|
|
|
|
0x95A, 0x762, 0x10B, 0xB13, 0x723, 0x52B, 0x2C4, 0xAD4, 0x8DC, 0x4EC,
|
|
|
|
0x285, 0x08D, 0x89D, 0x6A5},
|
|
|
|
{0x808, 0xA10, 0x220, 0x428, 0x101, 0xB11, 0x719, 0x529, 0x142, 0x94A,
|
|
|
|
0x75A, 0x362, 0x8CB, 0xAD3, 0x2E3, 0x4EB, 0x044, 0xA54, 0x65C, 0x46C,
|
|
|
|
0x085, 0x88D, 0x69D, 0x2A5},
|
|
|
|
{0xA08, 0x210, 0x420, 0x828, 0x141, 0x351, 0x759, 0x969, 0x042, 0xA4A,
|
|
|
|
0x65A, 0x462, 0xACB, 0x2D3, 0x4E3, 0x8EB, 0x084, 0x294, 0x69C, 0x8AC,
|
|
|
|
0x105, 0xB0D, 0x71D, 0x525},
|
|
|
|
{0x408, 0x810, 0xA20, 0x228, 0x081, 0x891, 0x699, 0x2A9, 0x102, 0x50A,
|
|
|
|
0x71A, 0xB22, 0x4CB, 0x8D3, 0xAE3, 0x2EB, 0x144, 0x954, 0x75C, 0x36C,
|
|
|
|
0x045, 0x44D, 0x65D, 0xA65},
|
2018-07-03 06:27:53 +02:00
|
|
|
)
|
2011-05-07 11:25:59 +00:00
|
|
|
|
|
|
|
_axis_convert_num = {'X': 0, 'Y': 1, 'Z': 2, '-X': 3, '-Y': 4, '-Z': 5}
|
|
|
|
|
|
|
|
|
|
|
|
def axis_conversion(from_forward='Y', from_up='Z', to_forward='Y', to_up='Z'):
|
|
|
|
"""
|
|
|
|
Each argument us an axis in ['X', 'Y', 'Z', '-X', '-Y', '-Z']
|
|
|
|
where the first 2 are a source and the second 2 are the target.
|
|
|
|
"""
|
|
|
|
from mathutils import Matrix
|
|
|
|
from functools import reduce
|
|
|
|
|
|
|
|
if from_forward == to_forward and from_up == to_up:
|
|
|
|
return Matrix().to_3x3()
|
|
|
|
|
2011-07-18 05:07:54 +00:00
|
|
|
if from_forward[-1] == from_up[-1] or to_forward[-1] == to_up[-1]:
|
2011-09-19 14:00:42 +00:00
|
|
|
raise Exception("Invalid axis arguments passed, "
|
|
|
|
"can't use up/forward on the same axis")
|
2011-07-18 05:07:54 +00:00
|
|
|
|
2011-07-29 01:24:03 +00:00
|
|
|
value = reduce(int.__or__, (_axis_convert_num[a] << (i * 3)
|
2018-07-03 06:27:53 +02:00
|
|
|
for i, a in enumerate((from_forward,
|
|
|
|
from_up,
|
|
|
|
to_forward,
|
|
|
|
to_up,
|
|
|
|
))))
|
2011-05-24 08:11:51 +00:00
|
|
|
|
2011-05-07 11:25:59 +00:00
|
|
|
for i, axis_lut in enumerate(_axis_convert_lut):
|
|
|
|
if value in axis_lut:
|
|
|
|
return Matrix(_axis_convert_matrix[i])
|
2022-09-14 16:18:59 +10:00
|
|
|
assert 0
|
2011-07-18 05:07:54 +00:00
|
|
|
|
2011-07-29 01:24:03 +00:00
|
|
|
|
2011-07-18 05:07:54 +00:00
|
|
|
def axis_conversion_ensure(operator, forward_attr, up_attr):
|
|
|
|
"""
|
|
|
|
Function to ensure an operator has valid axis conversion settings, intended
|
2011-08-26 18:48:48 +00:00
|
|
|
to be used from :class:`bpy.types.Operator.check`.
|
2011-07-18 05:07:54 +00:00
|
|
|
|
|
|
|
:arg operator: the operator to access axis attributes from.
|
2011-08-26 18:48:48 +00:00
|
|
|
:type operator: :class:`bpy.types.Operator`
|
2011-07-29 01:24:03 +00:00
|
|
|
:arg forward_attr: attribute storing the forward axis
|
2011-07-18 05:07:54 +00:00
|
|
|
:type forward_attr: string
|
2011-07-29 01:24:03 +00:00
|
|
|
:arg up_attr: attribute storing the up axis
|
2011-07-18 05:07:54 +00:00
|
|
|
:type up_attr: string
|
|
|
|
:return: True if the value was modified.
|
|
|
|
:rtype: boolean
|
|
|
|
"""
|
|
|
|
def validate(axis_forward, axis_up):
|
|
|
|
if axis_forward[-1] == axis_up[-1]:
|
|
|
|
axis_up = axis_up[0:-1] + 'XYZ'[('XYZ'.index(axis_up[-1]) + 1) % 3]
|
2011-07-29 01:24:03 +00:00
|
|
|
|
2011-07-18 05:07:54 +00:00
|
|
|
return axis_forward, axis_up
|
2011-07-29 01:24:03 +00:00
|
|
|
|
2011-07-18 05:07:54 +00:00
|
|
|
axis = getattr(operator, forward_attr), getattr(operator, up_attr)
|
|
|
|
axis_new = validate(*axis)
|
|
|
|
|
|
|
|
if axis != axis_new:
|
|
|
|
setattr(operator, forward_attr, axis_new[0])
|
|
|
|
setattr(operator, up_attr, axis_new[1])
|
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2011-05-07 11:25:59 +00:00
|
|
|
|
|
|
|
|
2021-10-11 21:28:06 +11:00
|
|
|
def create_derived_objects(depsgraph, objects):
|
|
|
|
"""
|
|
|
|
This function takes a sequence of objects, returning their instances.
|
|
|
|
|
|
|
|
:arg depsgraph: The evaluated depsgraph.
|
|
|
|
:type depsgraph: :class:`bpy.types.Depsgraph`
|
|
|
|
:arg objects: A sequencer of objects.
|
|
|
|
:type objects: sequence of :class:`bpy.types.Object`
|
|
|
|
:return: A dictionary where each key is an object from `objects`,
|
|
|
|
values are lists of (:class:`bpy.types.Object`, :class:`mathutils.Matrix`) tuples representing instances.
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
|
|
|
result = {}
|
|
|
|
for ob in objects:
|
|
|
|
ob_parent = ob.parent
|
|
|
|
if ob_parent and ob_parent.instance_type in {'VERTS', 'FACES'}:
|
|
|
|
continue
|
|
|
|
result[ob] = [] if ob.is_instancer else [(ob, ob.matrix_world.copy())]
|
|
|
|
|
|
|
|
if result:
|
|
|
|
for dup in depsgraph.object_instances:
|
|
|
|
dup_parent = dup.parent
|
|
|
|
if dup_parent is None:
|
|
|
|
continue
|
|
|
|
dup_parent_original = dup_parent.original
|
|
|
|
if not dup_parent_original.is_instancer:
|
|
|
|
# The instance has already been added (on assignment).
|
|
|
|
continue
|
|
|
|
instance_list = result.get(dup_parent_original)
|
|
|
|
if instance_list is None:
|
|
|
|
continue
|
|
|
|
instance_list.append((dup.instance_object.original, dup.matrix_world.copy()))
|
|
|
|
return result
|
2010-09-01 02:48:23 +00:00
|
|
|
|
|
|
|
|
2010-09-01 02:25:49 +00:00
|
|
|
def unpack_list(list_of_tuples):
|
|
|
|
flat_list = []
|
2011-10-17 06:58:07 +00:00
|
|
|
flat_list_extend = flat_list.extend # a tiny bit faster
|
2010-09-01 02:25:49 +00:00
|
|
|
for t in list_of_tuples:
|
|
|
|
flat_list_extend(t)
|
2010-09-01 12:11:34 +00:00
|
|
|
return flat_list
|
2010-09-01 02:25:49 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
|
2010-09-01 02:25:49 +00:00
|
|
|
# same as above except that it adds 0 for triangle faces
|
|
|
|
def unpack_face_list(list_of_tuples):
|
2015-05-17 17:17:31 +10:00
|
|
|
# allocate the entire list
|
2010-09-01 02:25:49 +00:00
|
|
|
flat_ls = [0] * (len(list_of_tuples) * 4)
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
for t in list_of_tuples:
|
|
|
|
if len(t) == 3:
|
|
|
|
if t[2] == 0:
|
|
|
|
t = t[1], t[2], t[0]
|
2011-10-17 06:58:07 +00:00
|
|
|
else: # assume quad
|
2010-09-01 02:25:49 +00:00
|
|
|
if t[3] == 0 or t[2] == 0:
|
|
|
|
t = t[2], t[3], t[0], t[1]
|
|
|
|
|
|
|
|
flat_ls[i:i + len(t)] = t
|
|
|
|
i += 4
|
2010-09-01 13:55:41 +00:00
|
|
|
return flat_ls
|
2011-04-14 08:47:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
path_reference_mode = EnumProperty(
|
2018-07-03 06:27:53 +02:00
|
|
|
name="Path Mode",
|
|
|
|
description="Method used to reference paths",
|
|
|
|
items=(
|
2022-12-08 11:26:21 -06:00
|
|
|
('AUTO', "Auto", "Use relative paths with subdirectories only"),
|
2018-07-03 06:27:53 +02:00
|
|
|
('ABSOLUTE', "Absolute", "Always write absolute paths"),
|
|
|
|
('RELATIVE', "Relative", "Always write relative paths "
|
|
|
|
"(where possible)"),
|
2022-12-08 11:26:21 -06:00
|
|
|
('MATCH', "Match", "Match absolute/relative "
|
2018-07-03 06:27:53 +02:00
|
|
|
"setting with input path"),
|
|
|
|
('STRIP', "Strip Path", "Filename only"),
|
|
|
|
('COPY', "Copy", "Copy the file to the destination path "
|
|
|
|
"(or subdirectory)"),
|
|
|
|
),
|
|
|
|
default='AUTO',
|
|
|
|
)
|
2011-04-14 08:47:47 +00:00
|
|
|
|
|
|
|
|
2019-09-16 16:16:25 +10:00
|
|
|
def path_reference(
|
|
|
|
filepath,
|
|
|
|
base_src,
|
|
|
|
base_dst,
|
|
|
|
mode='AUTO',
|
|
|
|
copy_subdir="",
|
|
|
|
copy_set=None,
|
|
|
|
library=None,
|
|
|
|
):
|
2011-04-14 08:47:47 +00:00
|
|
|
"""
|
|
|
|
Return a filepath relative to a destination directory, for use with
|
|
|
|
exporters.
|
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg filepath: the file path to return,
|
|
|
|
supporting blenders relative '//' prefix.
|
2011-04-14 08:47:47 +00:00
|
|
|
:type filepath: string
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg base_src: the directory the *filepath* is relative too
|
|
|
|
(normally the blend file).
|
2011-04-14 08:47:47 +00:00
|
|
|
:type base_src: string
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg base_dst: the directory the *filepath* will be referenced from
|
|
|
|
(normally the export path).
|
2011-04-14 08:47:47 +00:00
|
|
|
:type base_dst: string
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg mode: the method used get the path in
|
|
|
|
['AUTO', 'ABSOLUTE', 'RELATIVE', 'MATCH', 'STRIP', 'COPY']
|
2011-04-14 08:47:47 +00:00
|
|
|
:type mode: string
|
|
|
|
:arg copy_subdir: the subdirectory of *base_dst* to use when mode='COPY'.
|
|
|
|
:type copy_subdir: string
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg copy_set: collect from/to pairs when mode='COPY',
|
2011-10-17 06:58:07 +00:00
|
|
|
pass to *path_reference_copy* when exporting is done.
|
2011-04-14 08:47:47 +00:00
|
|
|
:type copy_set: set
|
2011-10-11 04:36:27 +00:00
|
|
|
:arg library: The library this path is relative to.
|
|
|
|
:type library: :class:`bpy.types.Library` or None
|
2011-04-14 08:47:47 +00:00
|
|
|
:return: the new filepath.
|
|
|
|
:rtype: string
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
is_relative = filepath.startswith("//")
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
filepath_abs = bpy.path.abspath(filepath, start=base_src, library=library)
|
2011-10-11 04:36:27 +00:00
|
|
|
filepath_abs = os.path.normpath(filepath_abs)
|
2011-04-14 08:47:47 +00:00
|
|
|
|
2011-08-08 05:21:37 +00:00
|
|
|
if mode in {'ABSOLUTE', 'RELATIVE', 'STRIP'}:
|
2011-04-14 08:47:47 +00:00
|
|
|
pass
|
|
|
|
elif mode == 'MATCH':
|
|
|
|
mode = 'RELATIVE' if is_relative else 'ABSOLUTE'
|
|
|
|
elif mode == 'AUTO':
|
2011-07-31 03:15:37 +00:00
|
|
|
mode = ('RELATIVE'
|
2011-10-11 04:36:27 +00:00
|
|
|
if bpy.path.is_subdir(filepath_abs, base_dst)
|
2011-07-31 03:15:37 +00:00
|
|
|
else 'ABSOLUTE')
|
2011-04-14 08:47:47 +00:00
|
|
|
elif mode == 'COPY':
|
2011-10-11 04:36:27 +00:00
|
|
|
subdir_abs = os.path.normpath(base_dst)
|
2011-04-14 08:47:47 +00:00
|
|
|
if copy_subdir:
|
2011-10-11 04:36:27 +00:00
|
|
|
subdir_abs = os.path.join(subdir_abs, copy_subdir)
|
2011-04-14 08:47:47 +00:00
|
|
|
|
2019-12-02 18:09:47 +01:00
|
|
|
filepath_cpy = os.path.join(subdir_abs, os.path.basename(filepath_abs))
|
2011-04-14 08:47:47 +00:00
|
|
|
|
|
|
|
copy_set.add((filepath_abs, filepath_cpy))
|
|
|
|
|
|
|
|
filepath_abs = filepath_cpy
|
|
|
|
mode = 'RELATIVE'
|
|
|
|
else:
|
2011-07-10 17:26:15 +00:00
|
|
|
raise Exception("invalid mode given %r" % mode)
|
2011-04-14 08:47:47 +00:00
|
|
|
|
|
|
|
if mode == 'ABSOLUTE':
|
|
|
|
return filepath_abs
|
|
|
|
elif mode == 'RELATIVE':
|
2014-02-13 08:51:33 +11:00
|
|
|
# can't always find the relative path
|
|
|
|
# (between drive letters on windows)
|
|
|
|
try:
|
2013-08-18 15:17:33 +00:00
|
|
|
return os.path.relpath(filepath_abs, base_dst)
|
|
|
|
except ValueError:
|
|
|
|
return filepath_abs
|
2011-04-14 08:47:47 +00:00
|
|
|
elif mode == 'STRIP':
|
|
|
|
return os.path.basename(filepath_abs)
|
|
|
|
|
|
|
|
|
|
|
|
def path_reference_copy(copy_set, report=print):
|
|
|
|
"""
|
|
|
|
Execute copying files of path_reference
|
2011-05-07 11:25:59 +00:00
|
|
|
|
2011-04-14 08:47:47 +00:00
|
|
|
:arg copy_set: set of (from, to) pairs to copy.
|
|
|
|
:type copy_set: set
|
|
|
|
:arg report: function used for reporting warnings, takes a string argument.
|
|
|
|
:type report: function
|
|
|
|
"""
|
|
|
|
if not copy_set:
|
|
|
|
return
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
for file_src, file_dst in copy_set:
|
|
|
|
if not os.path.exists(file_src):
|
|
|
|
report("missing %r, not copying" % file_src)
|
|
|
|
elif os.path.exists(file_dst) and os.path.samefile(file_src, file_dst):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
dir_to = os.path.dirname(file_dst)
|
2013-04-10 12:16:27 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs(dir_to, exist_ok=True)
|
|
|
|
except:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
try:
|
|
|
|
shutil.copy(file_src, file_dst)
|
|
|
|
except:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2011-06-02 15:21:47 +00:00
|
|
|
|
|
|
|
|
2011-08-15 04:58:19 +00:00
|
|
|
def unique_name(key, name, name_dict, name_max=-1, clean_func=None, sep="."):
|
2011-06-02 15:21:47 +00:00
|
|
|
"""
|
|
|
|
Helper function for storing unique names which may have special characters
|
|
|
|
stripped and restricted to a maximum length.
|
|
|
|
|
|
|
|
:arg key: unique item this name belongs to, name_dict[key] will be reused
|
|
|
|
when available.
|
2022-01-23 22:34:56 -06:00
|
|
|
This can be the object, mesh, material, etc instance itself.
|
2011-10-17 06:58:07 +00:00
|
|
|
:type key: any hashable object associated with the *name*.
|
2011-06-02 15:21:47 +00:00
|
|
|
:arg name: The name used to create a unique value in *name_dict*.
|
|
|
|
:type name: string
|
|
|
|
:arg name_dict: This is used to cache namespace to ensure no collisions
|
|
|
|
occur, this should be an empty dict initially and only modified by this
|
|
|
|
function.
|
|
|
|
:type name_dict: dict
|
|
|
|
:arg clean_func: Function to call on *name* before creating a unique value.
|
|
|
|
:type clean_func: function
|
2011-08-15 04:58:19 +00:00
|
|
|
:arg sep: Separator to use when between the name and a number when a
|
|
|
|
duplicate name is found.
|
|
|
|
:type sep: string
|
2011-06-02 15:21:47 +00:00
|
|
|
"""
|
|
|
|
name_new = name_dict.get(key)
|
|
|
|
if name_new is None:
|
|
|
|
count = 1
|
|
|
|
name_dict_values = name_dict.values()
|
2019-09-16 16:16:25 +10:00
|
|
|
name_new = name_new_orig = (
|
|
|
|
name if clean_func is None
|
|
|
|
else clean_func(name)
|
|
|
|
)
|
2011-06-02 15:21:47 +00:00
|
|
|
|
|
|
|
if name_max == -1:
|
|
|
|
while name_new in name_dict_values:
|
2019-09-16 16:16:25 +10:00
|
|
|
name_new = "%s%s%03d" % (
|
|
|
|
name_new_orig,
|
|
|
|
sep,
|
|
|
|
count,
|
|
|
|
)
|
2011-06-02 15:21:47 +00:00
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
name_new = name_new[:name_max]
|
|
|
|
while name_new in name_dict_values:
|
|
|
|
count_str = "%03d" % count
|
2019-09-16 16:16:25 +10:00
|
|
|
name_new = "%.*s%s%s" % (
|
|
|
|
name_max - (len(count_str) + 1),
|
|
|
|
name_new_orig,
|
|
|
|
sep,
|
|
|
|
count_str,
|
|
|
|
)
|
2011-06-02 15:21:47 +00:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
name_dict[key] = name_new
|
|
|
|
|
|
|
|
return name_new
|