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
|
2010-04-11 14:22:27 +00:00
|
|
|
from mathutils import Vector
|
2009-11-30 12:31:11 +00:00
|
|
|
|
|
|
|
# TODO, have these in a more general module
|
2009-12-05 22:03:07 +00:00
|
|
|
from rna_prop_ui import rna_idprop_ui_prop_get
|
2009-12-10 23:24:31 +00:00
|
|
|
SPECIAL_TYPES = "root",
|
2009-12-14 14:21:06 +00:00
|
|
|
LAYER_TYPES = "main", "extra", "ik", "fk"
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2010-03-06 01:40:29 +00:00
|
|
|
ORG_LAYERS = [n == 31 for n in range(0, 32)]
|
|
|
|
MCH_LAYERS = [n == 30 for n in range(0, 32)]
|
|
|
|
DEF_LAYERS = [n == 29 for n in range(0, 32)]
|
|
|
|
ROOT_LAYERS = [n == 28 for n in range(0, 32)]
|
2010-01-02 23:43:46 +00:00
|
|
|
|
2010-01-10 18:53:15 +00:00
|
|
|
ORG_PREFIX = "ORG-"
|
|
|
|
MCH_PREFIX = "MCH-"
|
|
|
|
DEF_PREFIX = "DEF-"
|
|
|
|
|
|
|
|
WGT_PREFIX = "WGT-"
|
2010-01-02 23:43:46 +00:00
|
|
|
|
|
|
|
|
2009-12-10 18:28:22 +00:00
|
|
|
class RigifyError(Exception):
|
|
|
|
"""Exception raised for errors in the metarig.
|
|
|
|
"""
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 18:28:22 +00:00
|
|
|
def __init__(self, message):
|
|
|
|
self.message = message
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 18:28:22 +00:00
|
|
|
def __str__(self):
|
|
|
|
return repr(self.message)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 12:58:03 +00:00
|
|
|
def submodule_func_from_type(bone_type):
|
|
|
|
type_pair = bone_type.split(".")
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 12:58:03 +00:00
|
|
|
# 'leg.ik' will look for an ik function in the leg module
|
|
|
|
# 'leg' will look up leg.main
|
|
|
|
if len(type_pair) == 1:
|
|
|
|
type_pair = type_pair[0], "main"
|
2009-12-02 19:13:54 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
type_name, func_name = type_pair
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 12:58:03 +00:00
|
|
|
# from rigify import leg
|
2009-12-10 23:24:31 +00:00
|
|
|
try:
|
2009-12-11 16:30:27 +00:00
|
|
|
submod = __import__(name="%s.%s" % (__package__, type_name), fromlist=[type_name])
|
2009-12-10 23:24:31 +00:00
|
|
|
except ImportError:
|
2009-12-11 16:30:27 +00:00
|
|
|
raise RigifyError("python module for type '%s' not found" % type_name)
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
reload(submod)
|
2009-12-11 16:30:27 +00:00
|
|
|
return type_name, submod, getattr(submod, func_name)
|
2009-12-11 14:16:59 +00:00
|
|
|
|
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
def get_submodule_types():
|
2009-12-11 14:16:59 +00:00
|
|
|
import os
|
|
|
|
submodules = []
|
|
|
|
files = os.listdir(os.path.dirname(__file__))
|
|
|
|
for f in files:
|
|
|
|
if not f.startswith("_") and f.endswith(".py"):
|
|
|
|
submodules.append(f[:-3])
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-11 14:16:59 +00:00
|
|
|
return sorted(submodules)
|
|
|
|
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
def get_bone_type_options(pbone, type_name):
|
|
|
|
options = {}
|
|
|
|
bone_name = pbone.name
|
|
|
|
for key, value in pbone.items():
|
2010-01-10 18:53:15 +00:00
|
|
|
key_pair = key.rsplit(".")
|
|
|
|
# get all bone properties
|
|
|
|
""""
|
2009-12-11 16:30:27 +00:00
|
|
|
if key_pair[0] == type_name:
|
|
|
|
if len(key_pair) != 2:
|
|
|
|
raise RigifyError("option error for bone '%s', property name was not a pair '%s'" % (bone_name, key_pair))
|
|
|
|
options[key_pair[1]] = value
|
2010-01-10 18:53:15 +00:00
|
|
|
"""
|
|
|
|
options[key] = value
|
2009-12-11 16:30:27 +00:00
|
|
|
|
|
|
|
return options
|
2009-12-09 22:44:26 +00:00
|
|
|
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-14 14:21:06 +00:00
|
|
|
def get_layer_dict(options):
|
|
|
|
'''
|
|
|
|
Extracts layer info from a bone options dict
|
|
|
|
defaulting to the layer index if not set.
|
|
|
|
'''
|
|
|
|
layer_default = [False] * 32
|
|
|
|
result = {}
|
|
|
|
for i, layer_type in enumerate(LAYER_TYPES):
|
|
|
|
# no matter if its not defined
|
|
|
|
layer_index = options.get("layer_" + layer_type, i + 2)
|
|
|
|
layer = layer_default[:]
|
|
|
|
layer[layer_index-1] = True
|
|
|
|
result[layer_type] = layer
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
def validate_rig(context, obj):
|
2009-12-11 14:16:59 +00:00
|
|
|
'''
|
|
|
|
Makes no changes
|
|
|
|
only runs the metarig definitions and reports errors
|
|
|
|
'''
|
2009-12-10 22:23:09 +00:00
|
|
|
type_found = False
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
for pbone in obj.pose.bones:
|
|
|
|
bone_name = pbone.name
|
|
|
|
bone_type = pbone.get("type", "")
|
|
|
|
|
|
|
|
if bone_type:
|
|
|
|
bone_type_list = [bt for bt in bone_type.replace(",", " ").split()]
|
|
|
|
else:
|
|
|
|
bone_type_list = []
|
|
|
|
|
2009-12-10 12:58:03 +00:00
|
|
|
for bone_type in bone_type_list:
|
2009-12-10 23:24:31 +00:00
|
|
|
if bone_type.split(".")[0] in SPECIAL_TYPES:
|
|
|
|
continue
|
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
type_name, submod, type_func = submodule_func_from_type(bone_type)
|
2009-12-10 11:56:31 +00:00
|
|
|
reload(submod)
|
|
|
|
submod.metarig_definition(obj, bone_name)
|
2009-12-10 22:23:09 +00:00
|
|
|
type_found = True
|
2009-12-11 16:30:27 +00:00
|
|
|
|
|
|
|
get_bone_type_options(pbone, bone_type)
|
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
# missing, - check for duplicate root bone.
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 22:23:09 +00:00
|
|
|
if not type_found:
|
|
|
|
raise RigifyError("This rig has no 'type' properties defined on any pose bones, nothing to do")
|
2009-12-10 11:56:31 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-08 14:02:06 +00:00
|
|
|
def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
|
2009-12-11 14:16:59 +00:00
|
|
|
'''
|
2009-12-13 13:59:16 +00:00
|
|
|
Main function for generating
|
2009-12-11 14:16:59 +00:00
|
|
|
'''
|
2009-12-05 19:26:28 +00:00
|
|
|
from collections import OrderedDict
|
2009-12-10 14:32:37 +00:00
|
|
|
import rigify_utils
|
|
|
|
reload(rigify_utils)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
print("Begin...")
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 22:23:09 +00:00
|
|
|
# Not needed but catches any errors before duplicating
|
2009-12-10 23:24:31 +00:00
|
|
|
validate_rig(context, obj_orig)
|
2009-12-05 19:26:28 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
global_undo = context.user_preferences.edit.global_undo
|
|
|
|
context.user_preferences.edit.global_undo = False
|
2009-12-10 11:56:31 +00:00
|
|
|
mode_orig = context.mode
|
2009-12-10 22:23:09 +00:00
|
|
|
rest_backup = obj_orig.data.pose_position
|
|
|
|
obj_orig.data.pose_position = 'REST'
|
2009-12-13 13:59:16 +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-08 14:02:06 +00:00
|
|
|
scene = context.scene
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Check if the generated rig already exists, so we can
|
|
|
|
# regenerate in the same object. If not, create a new
|
|
|
|
# object to generate the rig in.
|
|
|
|
print("Fetch rig.")
|
|
|
|
try:
|
|
|
|
name = obj_orig["rig_object_name"]
|
|
|
|
except KeyError:
|
|
|
|
name = "rig"
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
try:
|
|
|
|
obj = scene.objects[name]
|
|
|
|
except KeyError:
|
2010-02-22 00:07:46 +00:00
|
|
|
obj = bpy.data.objects.new(name, bpy.data.armatures.new(name))
|
2010-01-25 14:19:12 +00:00
|
|
|
scene.objects.link(obj)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
obj.data.pose_position = 'POSE'
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Get rid of anim data in case the rig already existed
|
|
|
|
print("Clear rig animation data.")
|
|
|
|
obj.animation_data_clear()
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Select generated rig object
|
2010-07-15 16:56:04 +00:00
|
|
|
obj_orig.select = False
|
|
|
|
obj.select = True
|
2009-12-05 19:26:28 +00:00
|
|
|
scene.objects.active = obj
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Remove all bones from the generated rig armature.
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
for bone in obj.data.edit_bones:
|
|
|
|
obj.data.edit_bones.remove(bone)
|
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Create temporary duplicates for merging
|
|
|
|
temp_rig_1 = obj_orig.copy()
|
|
|
|
temp_rig_1.data = obj_orig.data.copy()
|
|
|
|
scene.objects.link(temp_rig_1)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
temp_rig_2 = obj_orig.copy()
|
|
|
|
temp_rig_2.data = obj.data
|
|
|
|
scene.objects.link(temp_rig_2)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Select the temp rigs for merging
|
|
|
|
for objt in scene.objects:
|
2010-07-15 16:56:04 +00:00
|
|
|
objt.select = False # deselect all objects
|
|
|
|
temp_rig_1.select = True
|
|
|
|
temp_rig_2.select = True
|
2010-01-25 14:19:12 +00:00
|
|
|
scene.objects.active = temp_rig_2
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Merge the temporary rigs
|
|
|
|
bpy.ops.object.join(context)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Delete the second temp rig
|
|
|
|
bpy.ops.object.delete()
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Select the generated rig
|
|
|
|
for objt in scene.objects:
|
2010-07-15 16:56:04 +00:00
|
|
|
objt.select = False # deselect all objects
|
|
|
|
obj.select = True
|
2010-01-25 14:19:12 +00:00
|
|
|
scene.objects.active = obj
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-26 11:17:41 +00:00
|
|
|
# Copy over the pose_bone properties
|
2010-01-25 14:19:12 +00:00
|
|
|
for bone in obj_orig.pose.bones:
|
2010-01-26 11:17:41 +00:00
|
|
|
bone_gen = obj.pose.bones[bone.name]
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-26 11:17:41 +00:00
|
|
|
# Rotation mode and transform locks
|
2010-04-04 14:52:15 +00:00
|
|
|
bone_gen.rotation_mode = bone.rotation_mode
|
|
|
|
bone_gen.lock_rotation = tuple(bone.lock_rotation)
|
|
|
|
bone_gen.lock_rotation_w = bone.lock_rotation_w
|
2010-01-26 11:17:41 +00:00
|
|
|
bone_gen.lock_rotations_4d = bone.lock_rotations_4d
|
2010-04-04 14:52:15 +00:00
|
|
|
bone_gen.lock_location = tuple(bone.lock_location)
|
|
|
|
bone_gen.lock_scale = tuple(bone.lock_scale)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-26 11:17:41 +00:00
|
|
|
# Custom properties
|
2010-01-25 14:19:12 +00:00
|
|
|
for prop in bone.keys():
|
2010-01-26 11:17:41 +00:00
|
|
|
bone_gen[prop] = bone[prop]
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-26 11:17:41 +00:00
|
|
|
# Copy over bone properties
|
|
|
|
for bone in obj_orig.data.bones:
|
|
|
|
bone_gen = obj.data.bones[bone.name]
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-26 11:17:41 +00:00
|
|
|
# B-bone stuff
|
|
|
|
bone_gen.bbone_segments = bone.bbone_segments
|
|
|
|
bone_gen.bbone_in = bone.bbone_in
|
|
|
|
bone_gen.bbone_out = bone.bbone_out
|
2010-01-31 14:30:21 +00:00
|
|
|
|
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
# Create proxy deformation rig
|
|
|
|
# TODO: remove this
|
2009-12-08 14:02:06 +00:00
|
|
|
if META_DEF:
|
|
|
|
obj_def = obj_orig.copy()
|
|
|
|
obj_def.data = obj_orig.data.copy()
|
|
|
|
scene.objects.link(obj_def)
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
scene.update()
|
|
|
|
print("On to the real work.")
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
arm = obj.data
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2010-01-10 18:53:15 +00:00
|
|
|
# prepend the ORG prefix to the bones, and create the base_names mapping
|
2009-12-05 19:26:28 +00:00
|
|
|
base_names = {}
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
for bone in arm.edit_bones:
|
|
|
|
bone_name = bone.name
|
2010-01-10 18:53:15 +00:00
|
|
|
bone.name = ORG_PREFIX + bone_name
|
|
|
|
base_names[bone.name] = bone_name
|
|
|
|
|
|
|
|
# create root_bone
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
edit_bone = obj.data.edit_bones.new("root")
|
|
|
|
root_bone = edit_bone.name
|
|
|
|
edit_bone.head = (0.0, 0.0, 0.0)
|
|
|
|
edit_bone.tail = (0.0, 1.0, 0.0)
|
|
|
|
edit_bone.roll = 0.0
|
|
|
|
edit_bone.layer = ROOT_LAYERS
|
2009-12-05 19:26:28 +00:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# key: bone name
|
|
|
|
# value: {type:definition, ...}
|
|
|
|
# where type is the submodule name - leg, arm etc
|
2009-12-05 22:03:07 +00:00
|
|
|
# and definition is a list of bone names
|
2009-12-05 19:26:28 +00:00
|
|
|
bone_definitions = {}
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# key: bone name
|
|
|
|
# value: [functions, ...]
|
|
|
|
# each function is from the module. eg leg.ik, arm.main
|
|
|
|
bone_typeinfos = {}
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
# key: bone name
|
|
|
|
# value: [new_bone_name, ...]
|
|
|
|
# where each bone with a 'type' stores a list of bones that it created
|
|
|
|
# ...needed so we can override the root parent
|
|
|
|
bone_genesis = {}
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# inspect all bones and assign their definitions before modifying
|
|
|
|
for pbone in obj.pose.bones:
|
|
|
|
bone_name = pbone.name
|
2009-12-09 12:36:13 +00:00
|
|
|
bone_type = pbone.get("type", "")
|
|
|
|
if bone_type:
|
|
|
|
bone_type_list = [bt for bt in bone_type.replace(",", " ").split()]
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-09 12:36:13 +00:00
|
|
|
# not essential but means running autorig again wont do anything
|
|
|
|
del pbone["type"]
|
|
|
|
else:
|
|
|
|
bone_type_list = []
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
for bone_type in bone_type_list:
|
2009-12-11 16:30:27 +00:00
|
|
|
type_name, submod, type_func = submodule_func_from_type(bone_type)
|
2009-12-05 19:26:28 +00:00
|
|
|
reload(submod)
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
bone_def_dict = bone_definitions.setdefault(bone_name, {})
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# Only calculate bone definitions once
|
2009-12-11 16:30:27 +00:00
|
|
|
if type_name not in bone_def_dict:
|
|
|
|
bone_def_dict[type_name] = submod.metarig_definition(obj, bone_name)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
bone_typeinfo = bone_typeinfos.setdefault(bone_name, [])
|
2009-12-11 16:30:27 +00:00
|
|
|
bone_typeinfo.append((type_name, type_func))
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-07 17:21:30 +00:00
|
|
|
# sort bones, not needed but gives more pradictable execution which may be useful in rare cases
|
|
|
|
bones_sorted = obj.pose.bones.values()
|
|
|
|
bones_sorted.sort(key=lambda pbone: pbone.name) # first sort by names
|
2009-12-13 12:26:19 +00:00
|
|
|
bones_sorted.sort(key=lambda pbone: len(pbone.parent_recursive)) # parents before children
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-07 17:21:30 +00:00
|
|
|
# now we have all the info about bones we can start operating on them
|
|
|
|
# for pbone in obj.pose.bones:
|
|
|
|
for pbone in bones_sorted:
|
2009-12-05 19:26:28 +00:00
|
|
|
bone_name = pbone.name
|
2010-01-25 14:19:12 +00:00
|
|
|
print(bone_name)
|
2009-12-05 19:26:28 +00:00
|
|
|
if bone_name not in bone_typeinfos:
|
|
|
|
continue
|
2009-12-08 07:11:43 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
bone_def_dict = bone_definitions[bone_name]
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
# Only blend results from the same submodule, eg.
|
|
|
|
# leg.ik and arm.fk could not be blended.
|
|
|
|
results = OrderedDict()
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-13 16:20:18 +00:00
|
|
|
bone_names_pre = {bone.name for bone in arm.bones}
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
for type_name, type_func in bone_typeinfos[bone_name]:
|
2010-01-25 14:19:12 +00:00
|
|
|
print(" " + type_name)
|
2009-12-05 19:26:28 +00:00
|
|
|
# this bones definition of the current typeinfo
|
2009-12-11 16:30:27 +00:00
|
|
|
definition = bone_def_dict[type_name]
|
|
|
|
options = get_bone_type_options(pbone, type_name)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
2009-12-11 16:30:27 +00:00
|
|
|
ret = type_func(obj, definition, base_names, options)
|
2009-12-05 19:26:28 +00:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-05 22:03:07 +00:00
|
|
|
if ret:
|
2009-12-11 16:30:27 +00:00
|
|
|
result_submod = results.setdefault(type_name, [])
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
if result_submod and len(result_submod[-1]) != len(ret):
|
2009-12-15 18:53:16 +00:00
|
|
|
raise RigifyError("bone lists not compatible: %s, %s" % (result_submod[-1], ret))
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
result_submod.append(ret)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
for result_submod in results.values():
|
|
|
|
# blend 2 chains
|
2009-12-11 16:30:27 +00:00
|
|
|
definition = bone_def_dict[type_name]
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
if len(result_submod) == 2:
|
2009-12-09 12:36:13 +00:00
|
|
|
blend_bone_list(obj, definition, result_submod[0], result_submod[1], target_bone=bone_name)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
|
2009-12-13 16:20:18 +00:00
|
|
|
bone_names_post = {bone.name for bone in arm.bones}
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
# Store which bones were created from this one
|
|
|
|
bone_genesis[bone_name] = list(bone_names_post - bone_names_pre)
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
# need a reverse lookup on bone_genesis so as to know immediately
|
|
|
|
# where a bone comes from
|
|
|
|
bone_genesis_reverse = {}
|
2010-01-10 18:53:15 +00:00
|
|
|
'''
|
2009-12-10 14:32:37 +00:00
|
|
|
for bone_name, bone_children in bone_genesis.items():
|
|
|
|
for bone_child_name in bone_children:
|
|
|
|
bone_genesis_reverse[bone_child_name] = bone_name
|
2010-01-10 18:53:15 +00:00
|
|
|
'''
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 14:32:37 +00:00
|
|
|
|
2009-12-09 15:13:03 +00:00
|
|
|
if root_bone:
|
|
|
|
# assign all new parentless bones to this
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-09 15:13:03 +00:00
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
root_ebone = arm.edit_bones[root_bone]
|
|
|
|
for ebone in arm.edit_bones:
|
2009-12-10 14:32:37 +00:00
|
|
|
bone_name = ebone.name
|
2010-01-10 18:53:15 +00:00
|
|
|
if ebone.parent is None:
|
|
|
|
ebone.parent = root_ebone
|
|
|
|
'''
|
2009-12-10 14:32:37 +00:00
|
|
|
if ebone.parent is None and bone_name not in base_names:
|
|
|
|
# check for override
|
|
|
|
bone_creator = bone_genesis_reverse[bone_name]
|
|
|
|
pbone_creator = obj.pose.bones[bone_creator]
|
|
|
|
root_bone_override = pbone_creator.get("root", "")
|
|
|
|
|
|
|
|
if root_bone_override:
|
|
|
|
root_ebone_tmp = arm.edit_bones[root_bone_override]
|
|
|
|
else:
|
|
|
|
root_ebone_tmp = root_ebone
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-09 15:13:03 +00:00
|
|
|
ebone.connected = False
|
2009-12-10 23:24:31 +00:00
|
|
|
ebone.parent = root_ebone_tmp
|
2010-01-10 18:53:15 +00:00
|
|
|
'''
|
2009-12-10 14:32:37 +00:00
|
|
|
|
2009-12-09 15:13:03 +00:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-08 14:02:06 +00:00
|
|
|
|
|
|
|
if META_DEF:
|
|
|
|
# for pbone in obj_def.pose.bones:
|
|
|
|
for bone_name, bone_name_new in base_names.items():
|
|
|
|
#pbone_from = bone_name
|
|
|
|
pbone = obj_def.pose.bones[bone_name_new]
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-08 14:02:06 +00:00
|
|
|
con = pbone.constraints.new('COPY_ROTATION')
|
|
|
|
con.target = obj
|
|
|
|
con.subtarget = bone_name
|
|
|
|
|
|
|
|
if not pbone.bone.connected:
|
|
|
|
con = pbone.constraints.new('COPY_LOCATION')
|
|
|
|
con.target = obj
|
|
|
|
con.subtarget = bone_name
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 22:23:09 +00:00
|
|
|
# would be 'REST' from when copied
|
|
|
|
obj_def.data.pose_position = 'POSE'
|
|
|
|
|
2009-12-15 18:53:16 +00:00
|
|
|
# todo - make a more generic system?
|
|
|
|
layer_tot = [False] * 32
|
|
|
|
layer_last = layer_tot[:]
|
|
|
|
layer_last[31] = True
|
|
|
|
layer_second_last = layer_tot[:]
|
|
|
|
layer_second_last[30] = True
|
|
|
|
|
|
|
|
for bone_name, bone in arm.bones.items():
|
2010-01-02 23:43:46 +00:00
|
|
|
bone.deform = False # Non DEF bones shouldn't deform
|
2010-01-10 18:53:15 +00:00
|
|
|
if bone_name.startswith(ORG_PREFIX):
|
2010-01-02 23:43:46 +00:00
|
|
|
bone.layer = ORG_LAYERS
|
2010-01-10 18:53:15 +00:00
|
|
|
elif bone_name.startswith(MCH_PREFIX): # XXX fixme
|
2010-01-02 23:43:46 +00:00
|
|
|
bone.layer = MCH_LAYERS
|
2010-01-10 18:53:15 +00:00
|
|
|
elif bone_name.startswith(DEF_PREFIX): # XXX fixme
|
2010-01-02 23:43:46 +00:00
|
|
|
bone.layer = DEF_LAYERS
|
|
|
|
bone.deform = True
|
2010-01-10 18:53:15 +00:00
|
|
|
else:
|
|
|
|
# Assign bone appearance if there is a widget for it
|
2010-03-06 01:40:29 +00:00
|
|
|
obj.pose.bones[bone_name].custom_shape = context.scene.objects.get(WGT_PREFIX + bone_name)
|
2009-12-15 18:53:16 +00:00
|
|
|
|
|
|
|
layer_tot[:] = [max(lay) for lay in zip(layer_tot, bone.layer)]
|
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
# Only for demo'ing
|
2010-03-06 01:40:29 +00:00
|
|
|
layer_show = [a and not (b or c or d) for a, b, c, d in zip(layer_tot, ORG_LAYERS, MCH_LAYERS, DEF_LAYERS)]
|
2010-01-10 18:53:15 +00:00
|
|
|
arm.layer = layer_show
|
2009-12-15 18:53:16 +00:00
|
|
|
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2010-07-15 16:56:04 +00:00
|
|
|
# obj.hide = True
|
2009-12-05 19:26:28 +00:00
|
|
|
obj.data.draw_axes = False
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
bpy.ops.object.mode_set(mode=mode_orig)
|
2009-12-10 22:23:09 +00:00
|
|
|
obj_orig.data.pose_position = rest_backup
|
|
|
|
obj.data.pose_position = 'POSE'
|
2010-01-25 14:19:12 +00:00
|
|
|
obj_orig.data.pose_position = 'POSE'
|
2009-12-10 11:56:31 +00:00
|
|
|
context.user_preferences.edit.global_undo = global_undo
|
2010-01-31 14:30:21 +00:00
|
|
|
|
2010-01-25 14:19:12 +00:00
|
|
|
print("Done.\n")
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-05 19:26:28 +00:00
|
|
|
return obj
|
2009-11-30 12:31:11 +00:00
|
|
|
|
2009-12-02 11:55:47 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
def generate_test(context, metarig_type="", GENERATE_FINAL=True):
|
2009-12-02 15:37:07 +00:00
|
|
|
import os
|
|
|
|
new_objects = []
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-02 15:37:07 +00:00
|
|
|
scene = context.scene
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-02 15:37:07 +00:00
|
|
|
def create_empty_armature(name):
|
2010-01-09 23:44:01 +00:00
|
|
|
armature = bpy.data.armatures.new(name)
|
2010-02-22 00:07:46 +00:00
|
|
|
obj_new = bpy.data.objects.new(name, armature)
|
2009-12-05 19:26:28 +00:00
|
|
|
scene.objects.link(obj_new)
|
|
|
|
scene.objects.active = obj_new
|
2009-12-10 11:56:31 +00:00
|
|
|
for obj in scene.objects:
|
2010-07-15 16:56:04 +00:00
|
|
|
obj.select = False
|
|
|
|
obj_new.select = True
|
2009-12-02 15:37:07 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
for module_name in get_submodule_types():
|
2009-12-10 12:58:03 +00:00
|
|
|
if (metarig_type and module_name != metarig_type):
|
2009-12-10 11:56:31 +00:00
|
|
|
continue
|
2009-12-11 16:30:27 +00:00
|
|
|
|
|
|
|
# XXX workaround!, problem with updating the pose matrix.
|
2009-12-13 13:59:16 +00:00
|
|
|
if module_name == "delta":
|
2009-12-11 16:30:27 +00:00
|
|
|
continue
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-11 16:30:27 +00:00
|
|
|
type_name, submodule, func = submodule_func_from_type(module_name)
|
2009-12-02 15:37:07 +00:00
|
|
|
|
|
|
|
metarig_template = getattr(submodule, "metarig_template", None)
|
|
|
|
|
|
|
|
if metarig_template:
|
|
|
|
create_empty_armature("meta_" + module_name) # sets active
|
|
|
|
metarig_template()
|
2009-12-10 11:56:31 +00:00
|
|
|
obj = context.active_object
|
|
|
|
obj.location = scene.cursor_location
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
if GENERATE_FINAL:
|
|
|
|
obj_new = generate_rig(context, obj)
|
|
|
|
new_objects.append((obj, obj_new))
|
|
|
|
else:
|
|
|
|
new_objects.append((obj, None))
|
2009-12-02 15:37:07 +00:00
|
|
|
else:
|
2010-01-21 19:43:27 +00:00
|
|
|
print("note: rig type '%s' has no metarig_template(), can't test this" % module_name)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-02 15:37:07 +00:00
|
|
|
return new_objects
|
|
|
|
|
2009-12-04 16:35:31 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
def generate_test_all(context, GRAPH=False):
|
2009-12-02 15:37:07 +00:00
|
|
|
import rigify
|
2009-12-10 14:32:37 +00:00
|
|
|
import rigify_utils
|
2009-12-02 15:37:07 +00:00
|
|
|
import graphviz_export
|
|
|
|
import os
|
|
|
|
reload(rigify)
|
2009-12-10 14:32:37 +00:00
|
|
|
reload(rigify_utils)
|
2009-12-02 15:37:07 +00:00
|
|
|
reload(graphviz_export)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-02 15:37:07 +00:00
|
|
|
new_objects = rigify.generate_test(context)
|
2009-12-13 13:59:16 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
if GRAPH:
|
2010-06-02 17:58:28 +00:00
|
|
|
base_name = os.path.splitext(bpy.data.filepath)[0]
|
2009-12-10 11:56:31 +00:00
|
|
|
for obj, obj_new in new_objects:
|
|
|
|
for obj in (obj, obj_new):
|
|
|
|
fn = base_name + "-" + bpy.utils.clean_name(obj.name)
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
path_dot = fn + ".dot"
|
|
|
|
path_png = fn + ".png"
|
|
|
|
saved = graphviz_export.graph_armature(obj, path_dot, CONSTRAINTS=True, DRIVERS=True)
|
2009-12-02 15:37:07 +00:00
|
|
|
|
2009-12-10 11:56:31 +00:00
|
|
|
#if saved:
|
|
|
|
# os.system("dot -Tpng %s > %s; eog %s" % (path_dot, path_png, path_png))
|
2009-12-04 16:35:31 +00:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
for obj, obj_new in new_objects:
|
|
|
|
obj.data.drawtype = 'STICK'
|
|
|
|
obj.location[1] += i
|
|
|
|
obj_new.location[1] += i
|
2010-07-15 16:56:04 +00:00
|
|
|
obj_new.select = False
|
|
|
|
obj.select = True
|
2009-12-04 16:35:31 +00:00
|
|
|
i += 4
|
2009-12-05 22:03:07 +00:00
|
|
|
|
2009-12-02 15:37:07 +00:00
|
|
|
|
2009-11-30 12:31:11 +00:00
|
|
|
if __name__ == "__main__":
|
2009-12-10 11:56:31 +00:00
|
|
|
generate_rig(bpy.context, bpy.context.active_object)
|