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

« back to all changes in this revision

Viewing changes to release/scripts/addons/rigify/rigs/biped/arm/fk.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 rigify.utils import MetarigError
23
 
from rigify.utils import copy_bone
24
 
from rigify.utils import connected_children_names
25
 
from rigify.utils import strip_org, make_mechanism_name
26
 
from rigify.utils import get_layers
27
 
from rigify.utils import create_widget, create_limb_widget
28
 
from rna_prop_ui import rna_idprop_ui_prop_get
 
22
 
 
23
from .. import limb_common
 
24
 
 
25
from ....utils import MetarigError
 
26
from ....utils import connected_children_names
 
27
from ....utils import create_widget
 
28
from ....utils import strip_org
 
29
from ....utils import get_layers
29
30
 
30
31
 
31
32
class Rig:
41
42
 
42
43
        """
43
44
        self.obj = obj
44
 
        self.params = params
45
45
 
46
46
        # Get the chain of 3 connected bones
47
47
        self.org_bones = [bone] + connected_children_names(self.obj, bone)[:2]
48
48
 
49
49
        if len(self.org_bones) != 3:
50
 
            raise MetarigError("RIGIFY ERROR: Bone '%s': input to rig type must be a chain of 3 bones" % (strip_org(bone)))
51
 
 
52
 
        # Get (optional) parent
53
 
        if self.obj.data.bones[bone].parent is None:
54
 
            self.org_parent = None
55
 
        else:
56
 
            self.org_parent = self.obj.data.bones[bone].parent.name
57
 
 
58
 
        # Get the rig parameters
 
50
            raise MetarigError("RIGIFY ERROR: Bone '%s': input to rig type must be a chain of at least 3 bones" % (strip_org(bone)))
 
51
 
 
52
        # Get params
59
53
        if "layers" in params:
60
 
            self.layers = get_layers(params["layers"])
 
54
            layers = get_layers(params["layers"])
61
55
        else:
62
 
            self.layers = None
63
 
 
64
 
        self.primary_rotation_axis = params.primary_rotation_axis
 
56
            layers = None
 
57
 
 
58
        primary_rotation_axis = params.primary_rotation_axis
 
59
 
 
60
        # Arm is based on common limb
 
61
        self.fk_limb = limb_common.FKLimb(obj, self.org_bones[0], self.org_bones[1], self.org_bones[2], primary_rotation_axis, layers)
65
62
 
66
63
    def generate(self):
67
64
        """ Generate the rig.
69
66
            The main armature should be selected and active before this is called.
70
67
 
71
68
        """
72
 
        bpy.ops.object.mode_set(mode='EDIT')
73
 
 
74
 
        # Create the control bones
75
 
        uarm = copy_bone(self.obj, self.org_bones[0], strip_org(self.org_bones[0]))
76
 
        farm = copy_bone(self.obj, self.org_bones[1], strip_org(self.org_bones[1]))
77
 
        hand = copy_bone(self.obj, self.org_bones[2], strip_org(self.org_bones[2]))
78
 
 
79
 
        # Create the hinge bones
80
 
        if self.org_parent != None:
81
 
            hinge = copy_bone(self.obj, self.org_parent, make_mechanism_name(uarm + ".hinge"))
82
 
            socket1 = copy_bone(self.obj, uarm, make_mechanism_name(uarm + ".socket1"))
83
 
            socket2 = copy_bone(self.obj, uarm, make_mechanism_name(uarm + ".socket2"))
84
 
 
85
 
        # Get edit bones
86
 
        eb = self.obj.data.edit_bones
87
 
 
88
 
        uarm_e = eb[uarm]
89
 
        farm_e = eb[farm]
90
 
        hand_e = eb[hand]
91
 
 
92
 
        if self.org_parent != None:
93
 
            hinge_e = eb[hinge]
94
 
            socket1_e = eb[socket1]
95
 
            socket2_e = eb[socket2]
96
 
 
97
 
        # Parenting
98
 
        farm_e.parent = uarm_e
99
 
        hand_e.parent = farm_e
100
 
 
101
 
        if self.org_parent != None:
102
 
            hinge_e.use_connect = False
103
 
            socket1_e.use_connect = False
104
 
            socket2_e.use_connect = False
105
 
 
106
 
            uarm_e.parent = hinge_e
107
 
            hinge_e.parent = socket2_e
108
 
            socket2_e.parent = None
109
 
 
110
 
        # Positioning
111
 
        if self.org_parent != None:
112
 
            center = (hinge_e.head + hinge_e.tail) / 2
113
 
            hinge_e.head = center
114
 
            socket1_e.length /= 4
115
 
            socket2_e.length /= 3
116
 
 
117
 
        # Object mode, get pose bones
118
 
        bpy.ops.object.mode_set(mode='OBJECT')
119
 
        pb = self.obj.pose.bones
120
 
 
121
 
        uarm_p = pb[uarm]
122
 
        farm_p = pb[farm]
123
 
        hand_p = pb[hand]
124
 
        if self.org_parent != None:
125
 
            hinge_p = pb[hinge]
126
 
 
127
 
        if self.org_parent != None:
128
 
            # socket1_p = pb[socket1]  # UNUSED
129
 
            socket2_p = pb[socket2]
130
 
 
131
 
        # Set the elbow to only bend on the x-axis.
132
 
        farm_p.rotation_mode = 'XYZ'
133
 
        if 'X' in self.primary_rotation_axis:
134
 
            farm_p.lock_rotation = (False, True, True)
135
 
        elif 'Y' in self.primary_rotation_axis:
136
 
            farm_p.lock_rotation = (True, False, True)
137
 
        else:
138
 
            farm_p.lock_rotation = (True, True, False)
139
 
 
140
 
        # Hinge transforms are locked, for auto-ik
141
 
        if self.org_parent != None:
142
 
            hinge_p.lock_location = True, True, True
143
 
            hinge_p.lock_rotation = True, True, True
144
 
            hinge_p.lock_rotation_w = True
145
 
            hinge_p.lock_scale = True, True, True
146
 
 
147
 
        # Set up custom properties
148
 
        if self.org_parent != None:
149
 
            prop = rna_idprop_ui_prop_get(uarm_p, "isolate", create=True)
150
 
            uarm_p["isolate"] = 0.0
151
 
            prop["soft_min"] = prop["min"] = 0.0
152
 
            prop["soft_max"] = prop["max"] = 1.0
153
 
 
154
 
        # Hinge constraints / drivers
155
 
        if self.org_parent != None:
156
 
            con = socket2_p.constraints.new('COPY_LOCATION')
157
 
            con.name = "copy_location"
158
 
            con.target = self.obj
159
 
            con.subtarget = socket1
160
 
 
161
 
            con = socket2_p.constraints.new('COPY_TRANSFORMS')
162
 
            con.name = "isolate_off"
163
 
            con.target = self.obj
164
 
            con.subtarget = socket1
165
 
 
166
 
            # Driver
167
 
            fcurve = con.driver_add("influence")
168
 
            driver = fcurve.driver
169
 
            var = driver.variables.new()
170
 
            driver.type = 'AVERAGE'
171
 
            var.name = "var"
172
 
            var.targets[0].id_type = 'OBJECT'
173
 
            var.targets[0].id = self.obj
174
 
            var.targets[0].data_path = uarm_p.path_from_id() + '["isolate"]'
175
 
            mod = fcurve.modifiers[0]
176
 
            mod.poly_order = 1
177
 
            mod.coefficients[0] = 1.0
178
 
            mod.coefficients[1] = -1.0
179
 
 
180
 
        # Constrain org bones to controls
181
 
        con = pb[self.org_bones[0]].constraints.new('COPY_TRANSFORMS')
182
 
        con.name = "fk"
183
 
        con.target = self.obj
184
 
        con.subtarget = uarm
185
 
 
186
 
        con = pb[self.org_bones[1]].constraints.new('COPY_TRANSFORMS')
187
 
        con.name = "fk"
188
 
        con.target = self.obj
189
 
        con.subtarget = farm
190
 
 
191
 
        con = pb[self.org_bones[2]].constraints.new('COPY_TRANSFORMS')
192
 
        con.name = "fk"
193
 
        con.target = self.obj
194
 
        con.subtarget = hand
195
 
 
196
 
        # Set layers if specified
197
 
        if self.layers:
198
 
            uarm_p.bone.layers = self.layers
199
 
            farm_p.bone.layers = self.layers
200
 
            hand_p.bone.layers = self.layers
201
 
 
202
 
        # Create control widgets
203
 
        create_limb_widget(self.obj, uarm)
204
 
        create_limb_widget(self.obj, farm)
205
 
 
 
69
        bone_list = self.fk_limb.generate()
 
70
        uarm = bone_list[0]
 
71
        farm = bone_list[1]
 
72
        hand = bone_list[2]
 
73
 
 
74
        # Create hand widget
206
75
        ob = create_widget(self.obj, hand)
207
76
        if ob != None:
208
77
            verts = [(0.7, 1.5, 0.0), (0.7, -0.25, 0.0), (-0.7, -0.25, 0.0), (-0.7, 1.5, 0.0), (0.7, 0.723, 0.0), (-0.7, 0.723, 0.0), (0.7, 0.0, 0.0), (-0.7, 0.0, 0.0)]