2009-11-30 12:31:11 +00:00
|
|
|
# ##### 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,
|
2010-02-12 13:34:04 +00:00
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-11-30 12:31:11 +00:00
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
2009-12-05 22:03:07 +00:00
|
|
|
# <pep8 compliant>
|
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
import bpy
|
2009-12-10 12:58:03 +00:00
|
|
|
from rigify_utils import copy_bone_simple, get_side_name
|
2009-12-05 22:03:07 +00:00
|
|
|
from rna_prop_ui import rna_idprop_ui_prop_get
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# not used, defined for completeness
|
|
|
|
METARIG_NAMES = tuple()
|
2009-12-02 11:55:47 +00:00
|
|
|
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-02 11:55:47 +00:00
|
|
|
def metarig_template():
|
2009-12-07 17:21:30 +00:00
|
|
|
# generated by rigify.write_meta_rig
|
2009-12-02 11:55:47 +00:00
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
2009-12-10 11:56:31 +00:00
|
|
|
obj = bpy.context.active_object
|
2009-12-02 11:55:47 +00:00
|
|
|
arm = obj.data
|
|
|
|
bone = arm.edit_bones.new('hand')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = 0.0004, -0.0629, 0.0000
|
|
|
|
bone.tail[:] = 0.0021, -0.0209, 0.0000
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = 0.0000
|
|
|
|
bone.connected = False
|
|
|
|
bone = arm.edit_bones.new('palm.03')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = -0.0000, 0.0000, 0.0000
|
|
|
|
bone.tail[:] = 0.0025, 0.0644, -0.0065
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = -3.1396
|
|
|
|
bone.connected = False
|
|
|
|
bone.parent = arm.edit_bones['hand']
|
2009-12-09 12:00:28 +00:00
|
|
|
bone = arm.edit_bones.new('palm.02')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = 0.0252, -0.0000, 0.0000
|
|
|
|
bone.tail[:] = 0.0324, 0.0627, -0.0065
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = -3.1357
|
|
|
|
bone.connected = False
|
|
|
|
bone.parent = arm.edit_bones['hand']
|
2009-12-09 12:00:28 +00:00
|
|
|
bone = arm.edit_bones.new('palm.01')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = 0.0504, 0.0000, 0.0000
|
|
|
|
bone.tail[:] = 0.0703, 0.0508, -0.0065
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = -3.1190
|
|
|
|
bone.connected = False
|
|
|
|
bone.parent = arm.edit_bones['hand']
|
2009-12-09 12:00:28 +00:00
|
|
|
bone = arm.edit_bones.new('palm.04')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = -0.0252, 0.0000, 0.0000
|
|
|
|
bone.tail[:] = -0.0286, 0.0606, -0.0065
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = 3.1386
|
|
|
|
bone.connected = False
|
|
|
|
bone.parent = arm.edit_bones['hand']
|
2009-12-09 12:00:28 +00:00
|
|
|
bone = arm.edit_bones.new('palm.05')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = -0.0504, 0.0000, 0.0000
|
|
|
|
bone.tail[:] = -0.0669, 0.0534, -0.0065
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = 3.1239
|
|
|
|
bone.connected = False
|
|
|
|
bone.parent = arm.edit_bones['hand']
|
2009-12-09 12:00:28 +00:00
|
|
|
bone = arm.edit_bones.new('thumb')
|
2009-12-17 13:17:24 +00:00
|
|
|
bone.head[:] = 0.0682, -0.0148, 0.0000
|
|
|
|
bone.tail[:] = 0.1063, 0.0242, -0.0065
|
2009-12-02 11:55:47 +00:00
|
|
|
bone.roll = -3.0929
|
|
|
|
bone.connected = False
|
|
|
|
bone.parent = arm.edit_bones['hand']
|
|
|
|
|
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2009-12-09 12:00:28 +00:00
|
|
|
pbone = obj.pose.bones['palm.01']
|
2009-12-10 13:45:16 +00:00
|
|
|
pbone['type'] = 'palm_curl'
|
2009-12-02 11:55:47 +00:00
|
|
|
|
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
def metarig_definition(obj, orig_bone_name):
|
|
|
|
'''
|
2009-12-07 17:21:30 +00:00
|
|
|
The bone given is the first in an array of siblings with a matching basename
|
2009-12-09 12:00:28 +00:00
|
|
|
sorted with pointer first, little finger last.
|
2009-12-05 19:26:28 +00:00
|
|
|
eg.
|
2009-12-09 12:00:28 +00:00
|
|
|
[pointer, middle, ring, pinky... ] # any number of fingers
|
2009-12-05 19:26:28 +00:00
|
|
|
'''
|
|
|
|
arm = obj.data
|
2009-12-08 07:11:43 +00:00
|
|
|
|
2009-12-07 17:21:30 +00:00
|
|
|
palm_bone = arm.bones[orig_bone_name]
|
|
|
|
palm_parent = palm_bone.parent
|
|
|
|
palm_base = palm_bone.basename
|
|
|
|
bone_definition = [bone.name for bone in palm_parent.children if bone.basename == palm_base]
|
|
|
|
bone_definition.sort()
|
2009-12-09 12:00:28 +00:00
|
|
|
bone_definition.reverse()
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-07 17:21:30 +00:00
|
|
|
return [palm_parent.name] + bone_definition
|
2009-12-05 19:26:28 +00:00
|
|
|
|
|
|
|
|
2010-01-02 23:43:46 +00:00
|
|
|
def deform(obj, definitions, base_names, options):
|
|
|
|
for org_bone_name in definitions[1:]:
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
|
|
|
|
# Create deform bone.
|
|
|
|
bone = copy_bone_simple(obj.data, org_bone_name, "DEF-%s" % base_names[org_bone_name], parent=True)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-02 23:43:46 +00:00
|
|
|
# Store name before leaving edit mode
|
|
|
|
bone_name = bone.name
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-02 23:43:46 +00:00
|
|
|
# Leave edit mode
|
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-02 23:43:46 +00:00
|
|
|
# Get the pose bone
|
|
|
|
bone = obj.pose.bones[bone_name]
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-02 23:43:46 +00:00
|
|
|
# Constrain to the original bone
|
|
|
|
# XXX. Todo, is this needed if the bone is connected to its parent?
|
|
|
|
con = bone.constraints.new('COPY_TRANSFORMS')
|
|
|
|
con.name = "copy_loc"
|
|
|
|
con.target = obj
|
|
|
|
con.subtarget = org_bone_name
|
|
|
|
|
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
def main(obj, bone_definition, base_names, options):
|
2009-12-10 12:58:03 +00:00
|
|
|
arm = obj.data
|
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
children = bone_definition[1:]
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# Make a copy of the pinky
|
2009-12-05 19:26:28 +00:00
|
|
|
# simply assume the pinky has the lowest name
|
2009-11-30 12:31:11 +00:00
|
|
|
pinky_ebone = arm.edit_bones[children[0]]
|
2009-12-05 19:26:28 +00:00
|
|
|
ring_ebone = arm.edit_bones[children[1]]
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-09 22:44:26 +00:00
|
|
|
# FIXME, why split the second one?
|
|
|
|
base_name = base_names[pinky_ebone.name].rsplit('.', 2)[0]
|
|
|
|
|
|
|
|
control_ebone = copy_bone_simple(arm, pinky_ebone.name, base_name + get_side_name(base_names[pinky_ebone.name]), parent=True)
|
2009-12-05 22:03:07 +00:00
|
|
|
control_name = control_ebone.name
|
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
offset = (pinky_ebone.head - ring_ebone.head)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
control_ebone.translate(offset)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-02 23:43:46 +00:00
|
|
|
deform(obj, bone_definition, base_names, options)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
arm = obj.data
|
|
|
|
control_pbone = obj.pose.bones[control_name]
|
|
|
|
pinky_pbone = obj.pose.bones[children[0]]
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
control_pbone.rotation_mode = 'YZX'
|
|
|
|
control_pbone.lock_rotation = False, True, True
|
2010-01-19 19:07:09 +00:00
|
|
|
control_pbone.lock_location = True, True, True
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
driver_fcurves = pinky_pbone.driver_add("rotation_euler")
|
2009-12-05 22:03:07 +00:00
|
|
|
|
|
|
|
|
2010-04-06 07:49:10 +00:00
|
|
|
controller_path = control_pbone.path_from_id()
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# add custom prop
|
|
|
|
control_pbone["spread"] = 0.0
|
|
|
|
prop = rna_idprop_ui_prop_get(control_pbone, "spread", create=True)
|
|
|
|
prop["soft_min"] = -1.0
|
|
|
|
prop["soft_max"] = 1.0
|
2010-01-19 19:07:09 +00:00
|
|
|
prop["min"] = -1.0
|
|
|
|
prop["max"] = 1.0
|
2009-12-05 22:03:07 +00:00
|
|
|
|
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# *****
|
|
|
|
driver = driver_fcurves[0].driver
|
|
|
|
driver.type = 'AVERAGE'
|
2009-12-05 22:03:07 +00:00
|
|
|
|
Durian Request: Drivers Recode
Highlights:
* Support for Multi-Target Variables
This was the main reason for this recode. Previously, variables could only be used to give some RNA property used as an input source to the driver a name. However, this meant that effects such as Rotational Difference couldn't be used in conjunction with other effects and/or settings to achieve the powerful results. Now, a variable can take several input targets, perform some interesting operations on them, and spit out a representative value based on that.
* New Variable Types
With the introduction of multi-target variables, there are now 3 types of variable that can be used: single property (i.e. the only type previously), Rotational Difference (angle between two bones), and Distance (distance between two objects or bones).
* New Driver Types
In addition to the existing 'Average', 'Sum', and 'Expression' types, there is now the additional options of 'Minimum' and 'Maximum'. These take the smallest/largest value that one of the variables evaluates to.
* Fix for Driver F-Curve colouring bug
Newly added drivers did not get automatically coloured in the Graph Editor properly. Was caused by inappropriate notifiers being used.
Notes:
* This commit breaks existing 2.5 files with drivers (in other words, they are lost forever).
* Rigify has been corrected to work with the new system. The PyAPI for accessing targets used for the variables could still be made nicer (using subclassing to directly access?), but that is left for later.
* Version patching for 2.49 files still needs to be put back in place.
2010-01-04 21:15:45 +00:00
|
|
|
var = driver.variables.new()
|
|
|
|
var.name = "x"
|
|
|
|
var.targets[0].id_type = 'OBJECT'
|
|
|
|
var.targets[0].id = obj
|
|
|
|
var.targets[0].data_path = controller_path + ".rotation_euler[0]"
|
2009-12-05 22:03:07 +00:00
|
|
|
|
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# *****
|
|
|
|
driver = driver_fcurves[1].driver
|
|
|
|
driver.expression = "-x/4.0"
|
2009-12-05 22:03:07 +00:00
|
|
|
|
Durian Request: Drivers Recode
Highlights:
* Support for Multi-Target Variables
This was the main reason for this recode. Previously, variables could only be used to give some RNA property used as an input source to the driver a name. However, this meant that effects such as Rotational Difference couldn't be used in conjunction with other effects and/or settings to achieve the powerful results. Now, a variable can take several input targets, perform some interesting operations on them, and spit out a representative value based on that.
* New Variable Types
With the introduction of multi-target variables, there are now 3 types of variable that can be used: single property (i.e. the only type previously), Rotational Difference (angle between two bones), and Distance (distance between two objects or bones).
* New Driver Types
In addition to the existing 'Average', 'Sum', and 'Expression' types, there is now the additional options of 'Minimum' and 'Maximum'. These take the smallest/largest value that one of the variables evaluates to.
* Fix for Driver F-Curve colouring bug
Newly added drivers did not get automatically coloured in the Graph Editor properly. Was caused by inappropriate notifiers being used.
Notes:
* This commit breaks existing 2.5 files with drivers (in other words, they are lost forever).
* Rigify has been corrected to work with the new system. The PyAPI for accessing targets used for the variables could still be made nicer (using subclassing to directly access?), but that is left for later.
* Version patching for 2.49 files still needs to be put back in place.
2010-01-04 21:15:45 +00:00
|
|
|
var = driver.variables.new()
|
|
|
|
var.name = "x"
|
|
|
|
var.targets[0].id_type = 'OBJECT'
|
|
|
|
var.targets[0].id = obj
|
|
|
|
var.targets[0].data_path = controller_path + ".rotation_euler[0]"
|
2009-12-05 22:03:07 +00:00
|
|
|
|
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# *****
|
|
|
|
driver = driver_fcurves[2].driver
|
|
|
|
driver.expression = "(1.0-cos(x))-s"
|
2009-12-11 14:16:59 +00:00
|
|
|
|
|
|
|
for fcurve in driver_fcurves:
|
|
|
|
fcurve.modifiers.remove(0) # grr dont need a modifier
|
|
|
|
|
Durian Request: Drivers Recode
Highlights:
* Support for Multi-Target Variables
This was the main reason for this recode. Previously, variables could only be used to give some RNA property used as an input source to the driver a name. However, this meant that effects such as Rotational Difference couldn't be used in conjunction with other effects and/or settings to achieve the powerful results. Now, a variable can take several input targets, perform some interesting operations on them, and spit out a representative value based on that.
* New Variable Types
With the introduction of multi-target variables, there are now 3 types of variable that can be used: single property (i.e. the only type previously), Rotational Difference (angle between two bones), and Distance (distance between two objects or bones).
* New Driver Types
In addition to the existing 'Average', 'Sum', and 'Expression' types, there is now the additional options of 'Minimum' and 'Maximum'. These take the smallest/largest value that one of the variables evaluates to.
* Fix for Driver F-Curve colouring bug
Newly added drivers did not get automatically coloured in the Graph Editor properly. Was caused by inappropriate notifiers being used.
Notes:
* This commit breaks existing 2.5 files with drivers (in other words, they are lost forever).
* Rigify has been corrected to work with the new system. The PyAPI for accessing targets used for the variables could still be made nicer (using subclassing to directly access?), but that is left for later.
* Version patching for 2.49 files still needs to be put back in place.
2010-01-04 21:15:45 +00:00
|
|
|
var = driver.variables.new()
|
|
|
|
var.name = "x"
|
|
|
|
var.targets[0].id_type = 'OBJECT'
|
|
|
|
var.targets[0].id = obj
|
|
|
|
var.targets[0].data_path = controller_path + ".rotation_euler[0]"
|
|
|
|
|
|
|
|
var = driver.variables.new()
|
|
|
|
var.name = "s"
|
|
|
|
var.targets[0].id_type = 'OBJECT'
|
|
|
|
var.targets[0].id = obj
|
|
|
|
var.targets[0].data_path = controller_path + '["spread"]'
|
2009-11-30 12:31:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
for i, child_name in enumerate(children):
|
|
|
|
child_pbone = obj.pose.bones[child_name]
|
|
|
|
child_pbone.rotation_mode = 'YZX'
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
if child_name != children[-1] and child_name != children[0]:
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# this is somewhat arbitrary but seems to look good
|
2009-12-05 22:03:07 +00:00
|
|
|
inf = i / (len(children) + 1)
|
2009-11-30 12:31:11 +00:00
|
|
|
inf = 1.0 - inf
|
|
|
|
inf = ((inf * inf) + inf) / 2.0
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# used for X/Y constraint
|
|
|
|
inf_minor = inf * inf
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
con = child_pbone.constraints.new('COPY_ROTATION')
|
|
|
|
con.name = "Copy Z Rot"
|
|
|
|
con.target = obj
|
|
|
|
con.subtarget = children[0] # also pinky_pbone
|
|
|
|
con.owner_space = con.target_space = 'LOCAL'
|
|
|
|
con.use_x, con.use_y, con.use_z = False, False, True
|
|
|
|
con.influence = inf
|
|
|
|
|
|
|
|
con = child_pbone.constraints.new('COPY_ROTATION')
|
|
|
|
con.name = "Copy XY Rot"
|
|
|
|
con.target = obj
|
|
|
|
con.subtarget = children[0] # also pinky_pbone
|
|
|
|
con.owner_space = con.target_space = 'LOCAL'
|
|
|
|
con.use_x, con.use_y, con.use_z = True, True, False
|
|
|
|
con.influence = inf_minor
|
|
|
|
|
|
|
|
|
|
|
|
child_pbone = obj.pose.bones[children[-1]]
|
|
|
|
child_pbone.rotation_mode = 'QUATERNION'
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
# fix at the end since there is some trouble with tx info not being updated otherwise
|
|
|
|
def x_direction():
|
|
|
|
# NOTE: the direction of the Z rotation depends on which side the palm is on.
|
|
|
|
# we could do a simple side-of-x test but better to work out the direction
|
|
|
|
# the hand is facing.
|
2010-04-11 14:22:27 +00:00
|
|
|
from mathutils import Vector
|
2009-12-11 16:30:27 +00:00
|
|
|
from math import degrees
|
|
|
|
child_pbone_01 = obj.pose.bones[children[0]].bone
|
|
|
|
child_pbone_02 = obj.pose.bones[children[1]].bone
|
|
|
|
|
|
|
|
rel_vec = child_pbone_01.head - child_pbone_02.head
|
2010-05-03 19:12:11 +00:00
|
|
|
x_vec = child_pbone_01.matrix.rotation_part() * Vector((1.0, 0.0, 0.0))
|
2009-12-13 12:26:19 +00:00
|
|
|
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
return degrees(rel_vec.angle(x_vec)) > 90.0
|
2009-12-11 16:30:27 +00:00
|
|
|
|
|
|
|
if x_direction(): # flip
|
|
|
|
driver.expression = "-(%s)" % driver.expression
|
|
|
|
|
2009-12-14 14:21:06 +00:00
|
|
|
|
|
|
|
# last step setup layers
|
2010-01-10 18:53:15 +00:00
|
|
|
arm.bones[control_name].layer = list(arm.bones[bone_definition[1]].layer)
|
2009-12-14 14:21:06 +00:00
|
|
|
|
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# no blending the result of this
|
|
|
|
return None
|