~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/addons/rigify/rigs/biped/arm/deform.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# <pep8 compliant>
20
20
 
21
21
import bpy
22
 
from math import acos
23
 
from mathutils import Vector, Matrix
24
 
from rigify.utils import MetarigError
25
 
from rigify.utils import copy_bone, put_bone
26
 
from rigify.utils import connected_children_names
27
 
from rigify.utils import strip_org, make_mechanism_name, make_deformer_name
28
 
 
29
 
 
30
 
def align_roll(obj, bone1, bone2):
31
 
    bone1_e = obj.data.edit_bones[bone1]
32
 
    bone2_e = obj.data.edit_bones[bone2]
33
 
 
34
 
    bone1_e.roll = 0.0
35
 
 
36
 
    # Get the directions the bones are pointing in, as vectors
37
 
    y1 = bone1_e.y_axis
38
 
    x1 = bone1_e.x_axis
39
 
    y2 = bone2_e.y_axis
40
 
    x2 = bone2_e.x_axis
41
 
 
42
 
    # Get the shortest axis to rotate bone1 on to point in the same direction as bone2
43
 
    axis = y1.cross(y2)
44
 
    axis.normalize()
45
 
 
46
 
    # Angle to rotate on that shortest axis
47
 
    angle = y1.angle(y2)
48
 
 
49
 
    # Create rotation matrix to make bone1 point in the same direction as bone2
50
 
    rot_mat = Matrix.Rotation(angle, 3, axis)
51
 
 
52
 
    # Roll factor
53
 
    x3 = rot_mat * x1
54
 
    dot = x2 * x3
55
 
    if dot > 1.0:
56
 
        dot = 1.0
57
 
    elif dot < -1.0:
58
 
        dot = -1.0
59
 
    roll = acos(dot)
60
 
 
61
 
    # Set the roll
62
 
    bone1_e.roll = roll
63
 
 
64
 
    # Check if we rolled in the right direction
65
 
    x3 = rot_mat * bone1_e.x_axis
66
 
    check = x2 * x3
67
 
 
68
 
    # If not, reverse
69
 
    if check < 0.9999:
70
 
        bone1_e.roll = -roll
 
22
 
 
23
from .. import limb_common
 
24
 
 
25
from ....utils import MetarigError
 
26
from ....utils import connected_children_names
 
27
from ....utils import strip_org
71
28
 
72
29
 
73
30
class Rig:
75
32
 
76
33
    """
77
34
    def __init__(self, obj, bone, params):
78
 
        """ Gather and validate data about the rig.
79
 
            Store any data or references to data that will be needed later on.
80
 
            In particular, store references to bones that will be needed, and
81
 
            store names of bones that will be needed.
82
 
            Do NOT change any data in the scene.  This is a gathering phase only.
83
 
 
84
 
        """
85
35
        self.obj = obj
86
36
        self.params = params
87
37
 
92
42
            raise MetarigError("RIGIFY ERROR: Bone '%s': input to rig type must be a chain of 3 bones" % (strip_org(bone)))
93
43
 
94
44
        # Get rig parameters
95
 
        self.use_upper_arm_twist = params.use_upper_arm_twist
96
 
        self.use_forearm_twist = params.use_forearm_twist
 
45
        if params.separate_hose_layers:
 
46
            layers = list(params.hose_layers)
 
47
        else:
 
48
            layers = None
 
49
        use_complex_rig = params.use_complex_arm
 
50
        elbow_base_name = params.elbow_base_name
 
51
        primary_rotation_axis = params.primary_rotation_axis
 
52
 
 
53
        # Based on common limb
 
54
        self.rubber_hose_limb = limb_common.RubberHoseLimb(obj, self.org_bones[0], self.org_bones[1], self.org_bones[2], use_complex_rig, elbow_base_name, primary_rotation_axis, layers)
97
55
 
98
56
    def generate(self):
99
 
        """ Generate the rig.
100
 
            Do NOT modify any of the original bones, except for adding constraints.
101
 
            The main armature should be selected and active before this is called.
102
 
 
103
 
        """
104
 
        bpy.ops.object.mode_set(mode='EDIT')
105
 
 
106
 
        # Create upper arm bones
107
 
        if self.use_upper_arm_twist:
108
 
            uarm1 = copy_bone(self.obj, self.org_bones[0], make_deformer_name(strip_org(self.org_bones[0] + ".01")))
109
 
            uarm2 = copy_bone(self.obj, self.org_bones[0], make_deformer_name(strip_org(self.org_bones[0] + ".02")))
110
 
            utip = copy_bone(self.obj, self.org_bones[0], make_mechanism_name(strip_org(self.org_bones[0] + ".tip")))
111
 
        else:
112
 
            uarm = copy_bone(self.obj, self.org_bones[0], make_deformer_name(strip_org(self.org_bones[0])))
113
 
 
114
 
        # Create forearm bones
115
 
        if self.use_forearm_twist:
116
 
            farm1 = copy_bone(self.obj, self.org_bones[1], make_deformer_name(strip_org(self.org_bones[1] + ".01")))
117
 
            farm2 = copy_bone(self.obj, self.org_bones[1], make_deformer_name(strip_org(self.org_bones[1] + ".02")))
118
 
            ftip = copy_bone(self.obj, self.org_bones[1], make_mechanism_name(strip_org(self.org_bones[1] + ".tip")))
119
 
        else:
120
 
            farm = copy_bone(self.obj, self.org_bones[1], make_deformer_name(strip_org(self.org_bones[1])))
121
 
 
122
 
        # Create hand bone
123
 
        hand = copy_bone(self.obj, self.org_bones[2], make_deformer_name(strip_org(self.org_bones[2])))
124
 
 
125
 
        # Get edit bones
126
 
        eb = self.obj.data.edit_bones
127
 
 
128
 
        org_uarm_e = eb[self.org_bones[0]]
129
 
        if self.use_upper_arm_twist:
130
 
            uarm1_e = eb[uarm1]
131
 
            uarm2_e = eb[uarm2]
132
 
            utip_e = eb[utip]
133
 
        else:
134
 
            uarm_e = eb[uarm]
135
 
 
136
 
        org_farm_e = eb[self.org_bones[1]]
137
 
        if self.use_forearm_twist:
138
 
            farm1_e = eb[farm1]
139
 
            farm2_e = eb[farm2]
140
 
            ftip_e = eb[ftip]
141
 
        else:
142
 
            farm_e = eb[farm]
143
 
 
144
 
        org_hand_e = eb[self.org_bones[2]]
145
 
        hand_e = eb[hand]
146
 
 
147
 
        # Parent and position upper arm bones
148
 
        if self.use_upper_arm_twist:
149
 
            uarm1_e.use_connect = False
150
 
            uarm2_e.use_connect = False
151
 
            utip_e.use_connect = False
152
 
 
153
 
            uarm1_e.parent = org_uarm_e.parent
154
 
            uarm2_e.parent = org_uarm_e
155
 
            utip_e.parent = org_uarm_e
156
 
 
157
 
            center = Vector((org_uarm_e.head + org_uarm_e.tail) / 2)
158
 
 
159
 
            uarm1_e.tail = center
160
 
            uarm2_e.head = center
161
 
            put_bone(self.obj, utip, org_uarm_e.tail)
162
 
            utip_e.length = org_uarm_e.length / 8
163
 
        else:
164
 
            uarm_e.use_connect = False
165
 
            uarm_e.parent = org_uarm_e
166
 
 
167
 
        # Parent and position forearm bones
168
 
        if self.use_forearm_twist:
169
 
            farm1_e.use_connect = False
170
 
            farm2_e.use_connect = False
171
 
            ftip_e.use_connect = False
172
 
 
173
 
            farm1_e.parent = org_farm_e
174
 
            farm2_e.parent = org_farm_e
175
 
            ftip_e.parent = org_farm_e
176
 
 
177
 
            center = Vector((org_farm_e.head + org_farm_e.tail) / 2)
178
 
 
179
 
            farm1_e.tail = center
180
 
            farm2_e.head = center
181
 
            put_bone(self.obj, ftip, org_farm_e.tail)
182
 
            ftip_e.length = org_farm_e.length / 8
183
 
 
184
 
            # Align roll of farm2 with hand
185
 
            align_roll(self.obj, farm2, hand)
186
 
        else:
187
 
            farm_e.use_connect = False
188
 
            farm_e.parent = org_farm_e
189
 
 
190
 
        # Parent hand
191
 
        hand_e.use_connect = False
192
 
        hand_e.parent = org_hand_e
193
 
 
194
 
        # Object mode, get pose bones
195
 
        bpy.ops.object.mode_set(mode='OBJECT')
196
 
        pb = self.obj.pose.bones
197
 
 
198
 
        if self.use_upper_arm_twist:
199
 
            uarm1_p = pb[uarm1]
200
 
        if self.use_forearm_twist:
201
 
            farm2_p = pb[farm2]
202
 
        # hand_p = pb[hand]  # UNUSED
203
 
 
204
 
        # Upper arm constraints
205
 
        if self.use_upper_arm_twist:
206
 
            con = uarm1_p.constraints.new('COPY_LOCATION')
207
 
            con.name = "copy_location"
208
 
            con.target = self.obj
209
 
            con.subtarget = self.org_bones[0]
210
 
 
211
 
            con = uarm1_p.constraints.new('COPY_SCALE')
212
 
            con.name = "copy_scale"
213
 
            con.target = self.obj
214
 
            con.subtarget = self.org_bones[0]
215
 
 
216
 
            con = uarm1_p.constraints.new('DAMPED_TRACK')
217
 
            con.name = "track_to"
218
 
            con.target = self.obj
219
 
            con.subtarget = utip
220
 
 
221
 
        # Forearm constraints
222
 
        if self.use_forearm_twist:
223
 
            con = farm2_p.constraints.new('COPY_ROTATION')
224
 
            con.name = "copy_rotation"
225
 
            con.target = self.obj
226
 
            con.subtarget = hand
227
 
 
228
 
            con = farm2_p.constraints.new('DAMPED_TRACK')
229
 
            con.name = "track_to"
230
 
            con.target = self.obj
231
 
            con.subtarget = ftip
 
57
        bone_list = self.rubber_hose_limb.generate()
 
58
        return bone_list