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

shape_key_control.py « rigify « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd0e900a7b5208b7c7fbe5f286a7509aa4d8584f (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
# ##### 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
from rna_prop_ui import rna_idprop_ui_prop_get

#METARIG_NAMES = ("cpy",)
RIG_TYPE = "shape_key_control"


def addget_shape_key(obj, name="Key"):
    """ Fetches a shape key, or creates it if it doesn't exist
    """
    # Create a shapekey set if it doesn't already exist
    if obj.data.shape_keys is None:
        shape = obj.add_shape_key(name="Basis", from_mix=False)
        obj.active_shape_key_index = 0

    # Get the shapekey, or create it if it doesn't already exist
    if name in obj.data.shape_keys.keys:
        shape_key = obj.data.shape_keys.keys[name]
    else:
        shape_key = obj.add_shape_key(name=name, from_mix=False)

    return shape_key


def addget_shape_key_driver(obj, name="Key"):
    """ Fetches the driver for the shape key, or creates it if it doesn't
        already exist.
    """
    driver_path = 'keys["' + name + '"].value'
    fcurve = None
    driver = None
    new = False
    if obj.data.shape_keys.animation_data is not None:
        for driver_s in obj.data.shape_keys.animation_data.drivers:
            if driver_s.data_path == driver_path:
                fcurve = driver_s
    if fcurve == None:
        fcurve = obj.data.shape_keys.keys[name].driver_add("value", 0)
        fcurve.driver.type = 'AVERAGE'
        new = True

    return fcurve, new


# TODO:
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('Bone')
    #bone.head[:] = 0.0000, 0.0000, 0.0000
    #bone.tail[:] = 0.0000, 0.0000, 1.0000
    #bone.roll = 0.0000
    #bone.connected = False
    #
    #bpy.ops.object.mode_set(mode='OBJECT')
    #pbone = obj.pose.bones['Bone']
    #pbone['type'] = 'copy'
    pass


def metarig_definition(obj, orig_bone_name):
    bone = obj.data.bones[orig_bone_name]
    return [bone.name]


def main(obj, definitions, base_names, options):
    """ A rig that drives shape keys with the local transforms and/or custom
        properties of a single bone.
        A different shape can be driven by the negative value of a transform as
        well by giving a comma-separated list of two shapes.

        Required options:
            mesh:  name of mesh object(s) to add/get shapekeys to/from
                   (if multiple objects, make a comma-separated list)
        Optional options:
            loc_<x/y/z>:       name of the shape key to tie to translation of the bone
            loc_<x/y/z>_fac:   default multiplier of the bone influence on the shape key
            rot_<x/y/z>:       name of the shape key to tie to rotation of the bone
            rot_<x/y/z>_fac:   default multiplier of the bone influence on the shape key
            scale_<x/y/z>:     name of the shape key to tie to scale of the bone
            scale_<x/y/z>_fac: default multiplier of the bone influence on the shape key
            shape_key_sliders:     comma-separated list of custom properties to create sliders out of for driving shape keys
            <custom_prop>:         for each property listed in shape_key_sliders, specify a shape key for it to drive

    """

    bpy.ops.object.mode_set(mode='EDIT')
    eb = obj.data.edit_bones
    pb = obj.pose.bones

    org_bone = definitions[0]

    # Options
    req_options = ["mesh"]
    for option in req_options:
        if option not in options:
            raise RigifyError("'%s' rig type requires a '%s' option (bone: %s)" % (RIG_TYPE, option, base_names[definitions[0]]))

    meshes = options["mesh"].replace(" ", "").split(",")

    bone = copy_bone_simple(obj.data, org_bone, base_names[org_bone], parent=True).name

    bpy.ops.object.mode_set(mode='OBJECT')

    # Set rotation mode and axis locks
    pb[bone].rotation_mode = pb[org_bone].rotation_mode
    pb[bone].lock_location = tuple(pb[org_bone].lock_location)
    pb[bone].lock_rotation = tuple(pb[org_bone].lock_rotation)
    pb[bone].lock_rotation_w = pb[org_bone].lock_rotation_w
    pb[bone].lock_rotations_4d = pb[org_bone].lock_rotations_4d
    pb[bone].lock_scale = tuple(pb[org_bone].lock_scale)

    # List of rig options for specifying shape keys
    # Append '_fac' to the end for the name of the corresponding 'factor
    # default' option for that shape
    shape_key_options = ["loc_x",
                         "loc_y",
                         "loc_z",
                         "rot_x",
                         "rot_y",
                         "rot_z",
                         "scale_x",
                         "scale_y",
                         "scale_z"]

    driver_paths = {"loc_x":".location[0]",
                    "loc_y":".location[1]",
                    "loc_z":".location[2]",
                    "rot_x":".rotation_euler[0]",
                    "rot_y":".rotation_euler[1]",
                    "rot_z":".rotation_euler[2]",
                    "qrot_x":".rotation_quaternion[1]",
                    "qrot_y":".rotation_quaternion[2]",
                    "qrot_z":".rotation_quaternion[3]",
                    "scale_x":".scale[0]",
                    "scale_y":".scale[1]",
                    "scale_z":".scale[2]"}

    # Create the shape keys and drivers for transforms
    shape_info = []
    for option in shape_key_options:
        if option in options:
            shape_names = options[option].replace(" ", "").split(",")

            var_name = bone.replace(".","").replace("-","_") + "_" + option
            # Different RNA paths for euler vs quat
            if option in (shape_key_options[3:6]+shape_key_options[12:15]) \
            and pb[bone].rotation_mode == 'QUATERNION':
                var_path = driver_paths['q' + option]
            else:
                var_path = driver_paths[option]

            if (option+"_fac") in options:
                fac = options[option+"_fac"]
            else:
                fac = 1.0

            # Positive
            if shape_names[0] != "":
                # Different expressions for loc/rot/scale and positive/negative
                if option in shape_key_options[:3]:
                    # Location
                    expression = var_name + " * " + str(fac)
                elif option in shape_key_options[3:6]:
                    # Rotation
                    # Different expressions for euler vs quats
                    if pb[bone].rotation_mode == 'QUATERNION':
                        expression = "2 * asin(" + var_name + ") * " + str(fac)
                    else:
                        expression = var_name + " * " + str(fac)
                elif option in shape_key_options[6:9]:
                    # Scale
                    expression = "(1.0 - " + var_name + ") * " + str(fac) + " * -2"
                shape_name = shape_names[0]
                create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression)

            # Negative
            if shape_names[0] != "" and len(shape_names) > 1:
                # Different expressions for loc/rot/scale and positive/negative
                if option in shape_key_options[:3]:
                    # Location
                    expression = var_name + " * " + str(fac) + " * -1"
                elif option in shape_key_options[3:6]:
                    # Rotation
                    # Different expressions for euler vs quats
                    if pb[bone].rotation_mode == 'QUATERNION':
                        expression = "-2 * asin(" + var_name + ") * " + str(fac)
                    else:
                        expression = var_name + " * " + str(fac) + " * -1"
                elif option in shape_key_options[6:9]:
                    # Scale
                    expression = "(1.0 - " + var_name + ") * " + str(fac) + " * 2"
                shape_name = shape_names[1]
                create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression)

    # Create the shape keys and drivers for custom-property sliders
    if "shape_key_sliders" in options:
        # Get the slider names
        slider_names = options["shape_key_sliders"].replace(" ", "").split(",")
        if slider_names[0] != "":
            # Loop through the slider names and check if they have
            # shape keys specified for them, and if so, set them up.
            for slider_name in slider_names:
                if slider_name in options:
                    shape_names = options[slider_name].replace(" ", "").split(",")

                    # Set up the custom property on the bone
                    prop = rna_idprop_ui_prop_get(pb[bone], slider_name, create=True)
                    pb[bone][slider_name] = 0.0
                    prop["min"] = 0.0
                    prop["max"] = 1.0
                    prop["soft_min"] = 0.0
                    prop["soft_max"] = 1.0
                    if len(shape_names) > 1:
                        prop["min"] = -1.0
                        prop["soft_min"] = -1.0

                    # Add the shape drivers
                    # Positive
                    if shape_names[0] != "":
                        # Set up the variables for creating the shape key driver
                        shape_name = shape_names[0]
                        var_name = slider_name.replace(".", "_").replace("-", "_")
                        var_path = '["' + slider_name + '"]'
                        if slider_name + "_fac" in options:
                            fac = options[slider_name + "_fac"]
                        else:
                            fac = 1.0
                        expression = var_name + " * " + str(fac)
                        # Create the shape key driver
                        create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression)
                    # Negative
                    if shape_names[0] != "" and len(shape_names) > 1:
                        # Set up the variables for creating the shape key driver
                        shape_name = shape_names[1]
                        var_name = slider_name.replace(".", "_").replace("-", "_")
                        var_path = '["' + slider_name + '"]'
                        if slider_name + "_fac" in options:
                            fac = options[slider_name + "_fac"]
                        else:
                            fac = 1.0
                        expression = var_name + " * " + str(fac) + " * -1"
                        # Create the shape key driver
                        create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression)


    # Org bone copy transforms of control bone
    con = pb[org_bone].constraints.new('COPY_TRANSFORMS')
    con.target = obj
    con.subtarget = bone

    return (None,)


def create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression):
    """ Creates/gets a shape key and sets up a driver for it.

        obj = armature object
        bone = driving bone name
        meshes = list of meshes to create the shapekey/driver on
        shape_name = name of the shape key
        var_name = name of the driving variable
        var_path = path to the property on the bone to drive with
        expression = python expression for the driver
    """
    pb = obj.pose.bones
    bpy.ops.object.mode_set(mode='OBJECT')

    for mesh_name in meshes:
        mesh_obj = bpy.data.objects[mesh_name]

        # Add/get the shape key
        shape = addget_shape_key(mesh_obj, name=shape_name)

        # Add/get the shape key driver
        fcurve, a = addget_shape_key_driver(mesh_obj, name=shape_name)

        # Set up the driver
        driver = fcurve.driver
        driver.type = 'SCRIPTED'
        driver.expression = expression

        # Get the variable, or create it if it doesn't already exist
        if var_name in driver.variables:
            var = driver.variables[var_name]
        else:
            var = driver.variables.new()
            var.name = var_name

        # Set up the variable
        var.type = "SINGLE_PROP"
        var.targets[0].id_type = 'OBJECT'
        var.targets[0].id = obj
        var.targets[0].data_path = 'pose.bones["' + bone + '"]' + var_path