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

shape_key_rotdiff.py « rigify « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c30d95e666f45438c4e4c09d69c42b375aef861 (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
# ##### 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

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


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
    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'

    return fcurve




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'


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


def deform(obj, definitions, base_names, options):
    bpy.ops.object.mode_set(mode='EDIT')
    eb = obj.data.edit_bones

    bone_from = definitions[0]


    # Options
    req_options = ["to", "mesh", "shape_key"]
    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]]))

    bone_to = "ORG-" + options["to"]
    meshes = options["mesh"].replace(" ", "").split(",")
    shape_key_name = options["shape_key"]

    if "dmul" in options:
        shape_blend_fac = options["dmul"]
    else:
        shape_blend_fac = 1.0


    # Calculate the rotation difference between the bones
    rotdiff = (eb[bone_from].matrix.to_quat() * eb[bone_to].matrix.to_quat()) * 2

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

    # For every listed mesh object
    for mesh_name in meshes:
        mesh_obj = bpy.data.objects[mesh_name]

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

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

        # Get the variable, or create it if it doesn't already exist
        var_name = base_names[bone_from]
        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 = "ROTATION_DIFF"
        var.targets[0].id_type = 'OBJECT'
        var.targets[0].id = obj
        var.targets[0].bone_target = bone_from
        var.targets[1].id_type = 'OBJECT'
        var.targets[1].id = obj
        var.targets[1].bone_target = bone_to

        # Set fcurve offset, so zero is at the rest distance

        mod = fcurve.modifiers[0]
        if rotdiff > 0.00001:
            mod.coefficients[0] = -shape_blend_fac
            mod.coefficients[1] = shape_blend_fac / rotdiff

    return (None,)




def control(obj, definitions, base_names, options):
    """ options:
        mesh: name of mesh object with the shape key
        shape_key: name of shape key
        to: name of bone to measure distance from
    """
    pass




def main(obj, bone_definition, base_names, options):
    # Create control rig
    #control(obj, bone_definition, base_names, options)
    # Create deform rig
    deform(obj, bone_definition, base_names, options)

    return (None,)