Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pose_usage.py « pose_library - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc496d9c98c4bb84db2348d529b619360c2cee8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

"""
Pose Library - usage functions.
"""

from typing import Set
import re

from bpy.types import (
    Action,
    Object,
)


def select_bones(arm_object: Object, action: Action, *, select: bool, flipped: bool) -> None:
    pose_bone_re = re.compile(r'pose.bones\["([^"]+)"\]')
    pose = arm_object.pose

    seen_bone_names: Set[str] = set()

    for fcurve in action.fcurves:
        data_path: str = fcurve.data_path
        match = pose_bone_re.match(data_path)
        if not match:
            continue

        bone_name = match.group(1)

        if bone_name in seen_bone_names:
            continue
        seen_bone_names.add(bone_name)

        if flipped:
            bone_name = flip_side_name(bone_name)

        try:
            pose_bone = pose.bones[bone_name]
        except KeyError:
            continue

        pose_bone.bone.select = select


_FLIP_SEPARATORS = set(". -_")

# These are single-character replacements, others are handled differently.
_FLIP_REPLACEMENTS = {
    "l": "r",
    "L": "R",
    "r": "l",
    "R": "L",
}


def flip_side_name(to_flip: str) -> str:
    """Flip left and right indicators in the name.

    Basically a Python implementation of BLI_string_flip_side_name.

    >>> flip_side_name('bone_L.004')
    'bone_R.004'
    >>> flip_side_name('left_bone')
    'right_bone'
    >>> flip_side_name('Left_bone')
    'Right_bone'
    >>> flip_side_name('LEFT_bone')
    'RIGHT_bone'
    >>> flip_side_name('some.bone-RIGHT.004')
    'some.bone-LEFT.004'
    >>> flip_side_name('some.bone-right.004')
    'some.bone-left.004'
    >>> flip_side_name('some.bone-Right.004')
    'some.bone-Left.004'
    >>> flip_side_name('some.bone-LEFT.004')
    'some.bone-RIGHT.004'
    >>> flip_side_name('some.bone-left.004')
    'some.bone-right.004'
    >>> flip_side_name('some.bone-Left.004')
    'some.bone-Right.004'
    >>> flip_side_name('.004')
    '.004'
    >>> flip_side_name('L.004')
    'R.004'
    """
    import string

    if len(to_flip) < 3:
        # we don't flip names like .R or .L
        return to_flip

    # We first check the case with a .### extension, let's find the last period.
    number = ""
    replace = to_flip
    if to_flip[-1] in string.digits:
        try:
            index = to_flip.rindex(".")
        except ValueError:
            pass
        else:
            if to_flip[index + 1] in string.digits:
                # TODO(Sybren): this doesn't handle "bone.1abc2" correctly.
                number = to_flip[index:]
                replace = to_flip[:index]

    if not replace:
        # Nothing left after the number, so no flips necessary.
        return replace + number

    if len(replace) == 1:
        replace = _FLIP_REPLACEMENTS.get(replace, replace)
        return replace + number

    # First case; separator . - _ with extensions r R l L.
    if replace[-2] in _FLIP_SEPARATORS and replace[-1] in _FLIP_REPLACEMENTS:
        replace = replace[:-1] + _FLIP_REPLACEMENTS[replace[-1]]
        return replace + number

    # Second case; beginning with r R l L, with separator after it.
    if replace[1] in _FLIP_SEPARATORS and replace[0] in _FLIP_REPLACEMENTS:
        replace = _FLIP_REPLACEMENTS[replace[0]] + replace[1:]
        return replace + number

    lower = replace.lower()
    prefix = suffix = ""
    if lower.startswith("right"):
        bit = replace[0:2]
        if bit == "Ri":
            prefix = "Left"
        elif bit == "RI":
            prefix = "LEFT"
        else:
            prefix = "left"
        replace = replace[5:]
    elif lower.startswith("left"):
        bit = replace[0:2]
        if bit == "Le":
            prefix = "Right"
        elif bit == "LE":
            prefix = "RIGHT"
        else:
            prefix = "right"
        replace = replace[4:]
    elif lower.endswith("right"):
        bit = replace[-5:-3]
        if bit == "Ri":
            suffix = "Left"
        elif bit == "RI":
            suffix = "LEFT"
        else:
            suffix = "left"
        replace = replace[:-5]
    elif lower.endswith("left"):
        bit = replace[-4:-2]
        if bit == "Le":
            suffix = "Right"
        elif bit == "LE":
            suffix = "RIGHT"
        else:
            suffix = "right"
        replace = replace[:-4]

    return prefix + replace + suffix + number


if __name__ == '__main__':
    import doctest

    print(f"Test result: {doctest.testmod()}")