1#====================== BEGIN GPL LICENSE BLOCK ======================
2#
3#  This program is free software; you can redistribute it and/or
4#  modify it under the terms of the GNU General Public License
5#  as published by the Free Software Foundation; either version 2
6#  of the License, or (at your option) any later version.
7#
8#  This program is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#  GNU General Public License for more details.
12#
13#  You should have received a copy of the GNU General Public License
14#  along with this program; if not, write to the Free Software Foundation,
15#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16#
17#======================= END GPL LICENSE BLOCK ========================
18
19# <pep8 compliant>
20
21import bpy
22
23from .. import limb_common
24
25from ....utils import MetarigError
26from ....utils import connected_children_names
27from ....utils import create_widget
28from ....utils import strip_org
29from ....utils import get_layers
30
31
32class Rig:
33    """ An FK arm rig, with hinge switch.
34
35    """
36    def __init__(self, obj, bone, params):
37        """ Gather and validate data about the rig.
38            Store any data or references to data that will be needed later on.
39            In particular, store references to bones that will be needed, and
40            store names of bones that will be needed.
41            Do NOT change any data in the scene.  This is a gathering phase only.
42
43        """
44        self.obj = obj
45
46        # Get the chain of 3 connected bones
47        self.org_bones = [bone] + connected_children_names(self.obj, bone)[:2]
48
49        if len(self.org_bones) != 3:
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
53        if "layers" in params:
54            layers = get_layers(params["layers"])
55        else:
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)
62
63    def generate(self):
64        """ Generate the rig.
65            Do NOT modify any of the original bones, except for adding constraints.
66            The main armature should be selected and active before this is called.
67
68        """
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
75        ob = create_widget(self.obj, hand)
76        if ob is not None:
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)]
78            edges = [(1, 2), (0, 3), (0, 4), (3, 5), (4, 6), (1, 6), (5, 7), (2, 7)]
79            mesh = ob.data
80            mesh.from_pydata(verts, edges, [])
81            mesh.update()
82
83            mod = ob.modifiers.new("subsurf", 'SUBSURF')
84            mod.levels = 2
85
86        return [uarm, farm, hand]
87