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

glue.py « skin « rigs « rigify - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f9400cd69d596ecfdac77d497a4f44a1dc16c76 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# ====================== 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 ========================

# <pep8 compliant>

import bpy

from ...utils.naming import make_derived_name
from ...utils.widgets_basic import create_cube_widget
from ...utils.mechanism import move_all_constraints

from ...base_rig import stage
from ...base_generate import SubstitutionRig

from .skin_nodes import ControlQueryNode
from .skin_rigs import BaseSkinRig

from ..basic.raw_copy import RelinkConstraintsMixin

from .basic_chain import Rig as BasicChainRig


class Rig(SubstitutionRig):
    """Skin rig component that injects constraints into a control generated by other rigs."""

    def substitute(self):
        # Deformation is implemented by inheriting from the chain rig, so
        # enabling it requires switching between two different classes.
        if self.params.skin_glue_head_mode == 'BRIDGE':
            return [self.instantiate_rig(BridgeGlueRig, self.base_bone)]
        else:
            return [self.instantiate_rig(SimpleGlueRig, self.base_bone)]


def add_parameters(params):
    SimpleGlueRig.add_parameters(params)
    BridgeGlueRig.add_parameters(params)


def parameters_ui(layout, params):
    if params.skin_glue_head_mode == 'BRIDGE':
        BridgeGlueRig.parameters_ui(layout, params)
    else:
        SimpleGlueRig.parameters_ui(layout, params)


class BaseGlueRig(BaseSkinRig, RelinkConstraintsMixin):
    """Base class for the glue rigs."""

    def initialize(self):
        super().initialize()

        self.glue_head_mode = self.params.skin_glue_head_mode

        self.glue_use_tail = self.params.relink_constraints and self.params.skin_glue_use_tail
        self.relink_unmarked_constraints = self.glue_use_tail

    ####################################################
    # QUERY NODES

    @stage.initialize
    def init_glue_nodes(self):
        bone = self.get_bone(self.base_bone)

        self.head_constraint_node = ControlQueryNode(
            self, self.base_bone, point=bone.head
        )

        if self.glue_use_tail:
            self.tail_position_node = PositionQueryNode(
                self, self.base_bone, point=bone.tail,
                needs_reparent=self.params.skin_glue_tail_reparent,
            )

    ####################################################
    # GLUE CONSTRAINTS

    def rig_glue_constraints(self):
        org = self.base_bone
        ctrl = self.head_constraint_node.control_bone

        self.relink_bone_constraints(org)

        # Add the built-in constraint
        if self.glue_use_tail:
            target = self.tail_position_node.output_bone
            add_mode = self.params.skin_glue_add_constraint
            inf = self.params.skin_glue_add_constraint_influence

            if add_mode == 'COPY_LOCATION':
                self.make_constraint(
                    ctrl, 'COPY_LOCATION', target, insert_index=0,
                    owner_space='LOCAL', target_space='LOCAL',
                    use_offset=True, influence=inf
                )
            elif add_mode == 'COPY_LOCATION_OWNER':
                self.make_constraint(
                    ctrl, 'COPY_LOCATION', target, insert_index=0,
                    owner_space='LOCAL', target_space='LOCAL_OWNER_ORIENT',
                    use_offset=True, influence=inf
                )

        move_all_constraints(self.obj, org, ctrl)

    def find_relink_target(self, spec, old_target):
        if self.glue_use_tail and (spec == 'TARGET' or spec == '' == old_target):
            return self.tail_position_node.output_bone

        return super().find_relink_target(spec, old_target)

    ####################################################
    # SETTINGS

    @classmethod
    def add_parameters(self, params):
        params.skin_glue_head_mode = bpy.props.EnumProperty(
            name='Glue Mode',
            items=[('CHILD', 'Child Of Control',
                    "The glue bone becomes a child of the control bone"),
                   ('MIRROR', 'Mirror Of Control',
                    "The glue bone becomes a sibling of the control bone with Copy Transforms"),
                   ('REPARENT', 'Mirror With Parents',
                    "The glue bone keeps its parent, but uses Copy Transforms to group both local and parent induced motion of the control into local space"),
                   ('BRIDGE', 'Deformation Bridge',
                    "Other than adding glue constraints to the control, the rig acts as a one segment basic deform chain")],
            default='CHILD',
            description="Specifies how the glue bone is rigged to the control at the bone head location",
        )

        params.skin_glue_use_tail = bpy.props.BoolProperty(
            name='Use Tail Target',
            default=False,
            description='Find the control at the bone tail location and use it to relink TARGET or any constraints without an assigned subtarget or relink spec'
        )

        params.skin_glue_tail_reparent = bpy.props.BoolProperty(
            name='Target Local With Parents',
            default=False,
            description='Include transformations induced by target parents into target local space'
        )

        params.skin_glue_add_constraint = bpy.props.EnumProperty(
            name='Add Constraint',
            items=[('NONE', 'No New Constraint',
                    "Don't add new constraints"),
                   ('COPY_LOCATION', 'Copy Location (Local)',
                    "Add a constraint to copy Local Location with Offset. If the owner and target control " +
                    "rest orientations are different, the global movement direction will change accordingly"),
                   ('COPY_LOCATION_OWNER', 'Copy Location (Local, Owner Orientation)',
                    "Add a constraint to copy Local Location (Owner Orientation) with Offset. Even if the owner and " +
                    "target controls have different rest orientations, the global movement direction would be the same")],
            default='NONE',
            description="Add one of the common constraints linking the control to the tail target",
        )

        params.skin_glue_add_constraint_influence = bpy.props.FloatProperty(
            name="Influence",
            default=1.0, min=0, max=1,
            description="Influence of the added constraint",
        )

        self.add_relink_constraints_params(params)

        super().add_parameters(params)

    @classmethod
    def parameters_ui(self, layout, params):
        layout.prop(params, "skin_glue_head_mode")
        layout.prop(params, "relink_constraints")

        if params.relink_constraints:
            col = layout.column()
            col.prop(params, "skin_glue_use_tail")

            col2 = col.column()
            col2.active = params.skin_glue_use_tail
            col2.prop(params, "skin_glue_tail_reparent")

            col = layout.column()
            col.active = params.skin_glue_use_tail
            col.prop(params, "skin_glue_add_constraint", text="Add")

            col3 = col.column()
            col3.active = params.skin_glue_add_constraint != 'NONE'
            col3.prop(params, "skin_glue_add_constraint_influence", slider=True)

        layout.label(text="All constraints are moved to the control bone.", icon='INFO')

        super().parameters_ui(layout, params)


class SimpleGlueRig(BaseGlueRig):
    """Normal glue rig that only does glue."""

    def find_org_bones(self, bone):
        return bone.name

    ####################################################
    # QUERY NODES

    @stage.initialize
    def init_glue_nodes(self):
        super().init_glue_nodes()

        bone = self.get_bone(self.base_bone)

        self.head_position_node = PositionQueryNode(
            self, self.base_bone, point=bone.head,
            rig_org=self.glue_head_mode != 'CHILD',
            needs_reparent=self.glue_head_mode == 'REPARENT',
        )

    ##############################
    # ORG chain

    @stage.parent_bones
    def parent_org_bone(self):
        if self.glue_head_mode == 'CHILD':
            self.set_bone_parent(self.bones.org, self.head_position_node.output_bone)

    @stage.rig_bones
    def rig_org_bone(self):
        # This executes before head_position_node owned a by generator plugin
        self.rig_glue_constraints()


class BridgeGlueRig(BaseGlueRig, BasicChainRig):
    """Glue rig that also behaves like a deformation chain rig."""

    def find_org_bones(self, bone):
        # Still only bind to one bone
        return [bone.name]

    # Assign lowest priority
    chain_priority = -20

    # Orientation is irrelevant since controls should be merged into others
    use_skin_control_orientation_bone = False

    ####################################################
    # QUERY NODES

    @stage.prepare_bones
    def prepare_glue_nodes(self):
        # Verify that all nodes of the chain have been merged into others
        for node in self.control_nodes:
            if node.is_master_node:
                self.raise_error('glue control {} was not merged', node.name)

    ##############################
    # ORG chain

    @stage.rig_bones
    def rig_org_chain(self):
        # Move the user constraints away before the chain adds new ones
        self.rig_glue_constraints()

        super().rig_org_chain()


class PositionQueryNode(ControlQueryNode):
    """Finds the position of the highest layer control and rig reparent and/or org bone"""

    def __init__(self, rig, org, *, point=None, needs_reparent=False, rig_org=False):
        super().__init__(rig, org, point=point, find_highest_layer=True)

        self.needs_reparent = needs_reparent
        self.rig_org = rig_org

    @property
    def output_bone(self):
        if self.rig_org:
            return self.org
        elif self.needs_reparent:
            return self.reparent_bone
        else:
            return self.control_bone

    def initialize(self):
        if self.needs_reparent:
            self.build_parent(reparent=not self.rig_org)

    def parent_bones(self):
        if self.rig_org:
            if self.needs_reparent:
                parent = self.node_parent.output_bone
            else:
                parent = self.get_bone_parent(self.control_bone)

            self.set_bone_parent(self.org, parent, inherit_scale='AVERAGE')

    def apply_bones(self):
        if self.rig_org:
            self.get_bone(self.org).matrix = self.merged_master.matrix

    def rig_bones(self):
        if self.rig_org:
            self.make_constraint(self.org, 'COPY_TRANSFORMS', self.control_bone)


def create_sample(obj):
    from rigify.rigs.basic.super_copy import create_sample as inner
    obj.pose.bones[inner(obj)["Bone"]].rigify_type = 'skin.glue'