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

finger_curl.py « rigify « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fcf57f81d3eb3807b840074f4f1f8f77ac67a9e4 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# ##### 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 rigify import RigifyError
from rigify_utils import copy_bone_simple, get_side_name
from rna_prop_ui import rna_idprop_ui_prop_get

METARIG_NAMES = "finger_01", "finger_02", "finger_03"


def metarig_template():
    # generated by rigify.write_meta_rig
    bpy.ops.object.mode_set(mode='EDIT')
    obj = bpy.context.active_object
    arm = obj.data
    bone = arm.edit_bones.new('finger.01')
    bone.head[:] = 0.0000, 0.0000, 0.0000
    bone.tail[:] = 0.0353, -0.0184, -0.0053
    bone.roll = -2.8722
    bone.connected = False
    bone = arm.edit_bones.new('finger.02')
    bone.head[:] = 0.0353, -0.0184, -0.0053
    bone.tail[:] = 0.0702, -0.0364, -0.0146
    bone.roll = -2.7099
    bone.connected = True
    bone.parent = arm.edit_bones['finger.01']
    bone = arm.edit_bones.new('finger.03')
    bone.head[:] = 0.0702, -0.0364, -0.0146
    bone.tail[:] = 0.0903, -0.0461, -0.0298
    bone.roll = -2.1709
    bone.connected = True
    bone.parent = arm.edit_bones['finger.02']

    bpy.ops.object.mode_set(mode='OBJECT')
    pbone = obj.pose.bones['finger.01']
    pbone['type'] = 'finger_curl'


def metarig_definition(obj, orig_bone_name):
    '''
    The bone given is the first in a chain
    Expects a chain of at least 2 children.
    eg.
        finger -> finger_01 -> finger_02
    '''

    bone_definition = []

    orig_bone = obj.data.bones[orig_bone_name]

    bone_definition.append(orig_bone.name)

    bone = orig_bone
    chain = 0
    while chain < 2: # first 2 bones only have 1 child
        children = bone.children

        if len(children) != 1:
            raise RigifyError("expected the chain to have 2 children from bone '%s' without a fork" % orig_bone_name)
        bone = children[0]
        bone_definition.append(bone.name) # finger_02, finger_03
        chain += 1

    if len(bone_definition) != len(METARIG_NAMES):
        raise RigifyError("internal problem, expected %d bones" % len(METARIG_NAMES))

    return bone_definition


def deform(obj, definitions, base_names, options):
    """ Creates the deform rig.
    """
    bpy.ops.object.mode_set(mode='EDIT')

    # Create base digit bones: two bones, each half of the base digit.
    f1a = copy_bone_simple(obj.data, definitions[0], "DEF-%s.01" % base_names[definitions[0]], parent=True)
    f1b = copy_bone_simple(obj.data, definitions[0], "DEF-%s.02" % base_names[definitions[0]], parent=True)
    f1a.connected = False
    f1b.connected = False
    f1b.parent = f1a
    center = f1a.center
    f1a.tail = center
    f1b.head = center

    # Create the other deform bones.
    f2 = copy_bone_simple(obj.data, definitions[1], "DEF-%s" % base_names[definitions[1]], parent=True)
    f3 = copy_bone_simple(obj.data, definitions[2], "DEF-%s" % base_names[definitions[2]], parent=True)

    # Store names before leaving edit mode
    f1a_name = f1a.name
    f1b_name = f1b.name
    f2_name = f2.name
    f3_name = f3.name

    # Leave edit mode
    bpy.ops.object.mode_set(mode='OBJECT')

    # Get the pose bones
    f1a = obj.pose.bones[f1a_name]
    f1b = obj.pose.bones[f1b_name]
    f2 = obj.pose.bones[f2_name]
    f3 = obj.pose.bones[f3_name]

    # Constrain the base digit's bones
    con = f1a.constraints.new('DAMPED_TRACK')
    con.name = "trackto"
    con.target = obj
    con.subtarget = definitions[1]

    con = f1a.constraints.new('COPY_SCALE')
    con.name = "copy_scale"
    con.target = obj
    con.subtarget = definitions[0]

    con = f1b.constraints.new('COPY_ROTATION')
    con.name = "copy_rot"
    con.target = obj
    con.subtarget = definitions[0]

    # Constrain the other digit's bones
    con = f2.constraints.new('COPY_TRANSFORMS')
    con.name = "copy_transforms"
    con.target = obj
    con.subtarget = definitions[1]

    con = f3.constraints.new('COPY_TRANSFORMS')
    con.name = "copy_transforms"
    con.target = obj
    con.subtarget = definitions[2]


def main(obj, bone_definition, base_names, options):
    # *** EDITMODE
    bpy.ops.object.mode_set(mode='EDIT')

    # get assosiated data
    arm = obj.data
    bb = obj.data.bones
    eb = obj.data.edit_bones
    pb = obj.pose.bones

    org_f1 = bone_definition[0] # Original finger bone 01
    org_f2 = bone_definition[1] # Original finger bone 02
    org_f3 = bone_definition[2] # Original finger bone 03

    # Check options
    if "bend_ratio" in options:
        bend_ratio = options["bend_ratio"]
    else:
        bend_ratio = 0.4

    yes = [1, 1.0, True, "True", "true", "Yes", "yes"]
    make_hinge = False
    if ("hinge" in options) and (eb[org_f1].parent is not None):
        if options["hinge"] in yes:
            make_hinge = True


    # Needed if its a new armature with no keys
    obj.animation_data_create()

    # Create the control bone
    base_name = base_names[bone_definition[0]].split(".", 1)[0]
    tot_len = eb[org_f1].length + eb[org_f2].length + eb[org_f3].length
    control = copy_bone_simple(arm, bone_definition[0], base_name + get_side_name(base_names[bone_definition[0]]), parent=True).name
    eb[control].connected = eb[org_f1].connected
    eb[control].parent = eb[org_f1].parent
    eb[control].length = tot_len

    # Create secondary control bones
    f1 = copy_bone_simple(arm, bone_definition[0], base_names[bone_definition[0]]).name
    f2 = copy_bone_simple(arm, bone_definition[1], base_names[bone_definition[1]]).name
    f3 = copy_bone_simple(arm, bone_definition[2], base_names[bone_definition[2]]).name

    # Create driver bones
    df1 = copy_bone_simple(arm, bone_definition[0], "MCH-" + base_names[bone_definition[0]]).name
    eb[df1].length /= 2
    df2 = copy_bone_simple(arm, bone_definition[1], "MCH-" + base_names[bone_definition[1]]).name
    eb[df2].length /= 2
    df3 = copy_bone_simple(arm, bone_definition[2], "MCH-" + base_names[bone_definition[2]]).name
    eb[df3].length /= 2

    # Set parents of the bones, interleaving the driver bones with the secondary control bones
    eb[f3].connected = False
    eb[df3].connected = False
    eb[f2].connected = False
    eb[df2].connected = False
    eb[f1].connected = False
    eb[df1].connected = eb[org_f1].connected

    eb[f3].parent = eb[df3]
    eb[df3].parent = eb[f2]
    eb[f2].parent = eb[df2]
    eb[df2].parent = eb[f1]
    eb[f1].parent = eb[df1]
    eb[df1].parent = eb[org_f1].parent

    # Set up bones for hinge
    if make_hinge:
        socket = copy_bone_simple(arm, org_f1, "MCH-socket_" + control, parent=True).name
        hinge = copy_bone_simple(arm, eb[org_f1].parent.name, "MCH-hinge_" + control).name

        eb[control].connected = False
        eb[control].parent = eb[hinge]

    # Create the deform rig while we're still in edit mode
    deform(obj, bone_definition, base_names, options)


    # *** POSEMODE
    bpy.ops.object.mode_set(mode='OBJECT')

    # Set rotation modes and axis locks
    pb[control].rotation_mode = obj.pose.bones[bone_definition[0]].rotation_mode
    pb[control].lock_location = True, True, True
    pb[control].lock_scale = True, False, True
    pb[f1].rotation_mode = 'YZX'
    pb[f2].rotation_mode = 'YZX'
    pb[f3].rotation_mode = 'YZX'
    pb[f1].lock_location = True, True, True
    pb[f2].lock_location = True, True, True
    pb[f3].lock_location = True, True, True
    pb[df2].rotation_mode = 'YZX'
    pb[df3].rotation_mode = 'YZX'

    # Add the bend_ratio property to the control bone
    pb[control]["bend_ratio"] = bend_ratio
    prop = rna_idprop_ui_prop_get(pb[control], "bend_ratio", create=True)
    prop["soft_min"] = 0.0
    prop["soft_max"] = 1.0

    # Add hinge property to the control bone
    if make_hinge:
        pb[control]["hinge"] = 0.0
        prop = rna_idprop_ui_prop_get(pb[control], "hinge", create=True)
        prop["soft_min"] = 0.0
        prop["soft_max"] = 1.0

    # Constraints
    con = pb[df1].constraints.new('COPY_LOCATION')
    con.target = obj
    con.subtarget = control

    con = pb[df1].constraints.new('COPY_ROTATION')
    con.target = obj
    con.subtarget = control

    con = pb[org_f1].constraints.new('COPY_TRANSFORMS')
    con.target = obj
    con.subtarget = f1

    con = pb[org_f2].constraints.new('COPY_TRANSFORMS')
    con.target = obj
    con.subtarget = f2

    con = pb[org_f3].constraints.new('COPY_TRANSFORMS')
    con.target = obj
    con.subtarget = f3

    if make_hinge:
        con = pb[hinge].constraints.new('COPY_TRANSFORMS')
        con.target = obj
        con.subtarget = bb[org_f1].parent.name

        hinge_driver_path = pb[control].path_to_id() + '["hinge"]'

        fcurve = con.driver_add("influence", 0)
        driver = fcurve.driver
        var = driver.variables.new()
        driver.type = 'AVERAGE'
        var.name = "var"
        var.targets[0].id_type = 'OBJECT'
        var.targets[0].id = obj
        var.targets[0].data_path = hinge_driver_path

        mod = fcurve.modifiers[0]
        mod.poly_order = 1
        mod.coefficients[0] = 1.0
        mod.coefficients[1] = -1.0

        con = pb[control].constraints.new('COPY_LOCATION')
        con.target = obj
        con.subtarget = socket

    # Create the drivers for the driver bones (control bone scale rotates driver bones)
    controller_path = pb[control].path_to_id() # 'pose.bones["%s"]' % control_bone_name

    i = 0
    for bone in [df2, df3]:

        # XXX - todo, any number
        if i == 2:
            break

        pbone = pb[bone]

        pbone.rotation_mode = 'YZX'
        fcurve_driver = pbone.driver_add("rotation_euler", 0)

        #obj.driver_add('pose.bones["%s"].scale', 1)
        #obj.animation_data.drivers[-1] # XXX, WATCH THIS
        driver = fcurve_driver.driver

        # scale target
        var = driver.variables.new()
        var.name = "scale"
        var.targets[0].id_type = 'OBJECT'
        var.targets[0].id = obj
        var.targets[0].data_path = controller_path + '.scale[1]'

        # bend target
        var = driver.variables.new()
        var.name = "br"
        var.targets[0].id_type = 'OBJECT'
        var.targets[0].id = obj
        var.targets[0].data_path = controller_path + '["bend_ratio"]'

        # XXX - todo, any number
        if i == 0:
            driver.expression = '(-scale+1.0)*pi*2.0*(1.0-br)'
        elif i == 1:
            driver.expression = '(-scale+1.0)*pi*2.0*br'

        i += 1

    # Last step setup layers
    if "ex_layer" in options:
        layer = [n == options["ex_layer"] for n in range(0, 32)]
    else:
        layer = list(arm.bones[bone_definition[0]].layer)
    for bone_name in [f1, f2, f3]:
        arm.bones[bone_name].layer = layer

    layer = list(arm.bones[bone_definition[0]].layer)
    bb[control].layer = layer

    # no blending the result of this
    return None