[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [25284] trunk/blender/release/scripts/ modules: move generic functions out of rigify's __init__. py into rigify_utils.py since rigify its self does not use them, only some of the metarig types.
Campbell Barton
ideasman42 at gmail.com
Thu Dec 10 13:58:03 CET 2009
Revision: 25284
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=25284
Author: campbellbarton
Date: 2009-12-10 13:58:03 +0100 (Thu, 10 Dec 2009)
Log Message:
-----------
move generic functions out of rigify's __init__.py into rigify_utils.py since rigify its self does not use them, only some of the metarig types.
Modified Paths:
--------------
trunk/blender/release/scripts/modules/rigify/__init__.py
trunk/blender/release/scripts/modules/rigify/arm.py
trunk/blender/release/scripts/modules/rigify/delta.py
trunk/blender/release/scripts/modules/rigify/finger.py
trunk/blender/release/scripts/modules/rigify/leg.py
trunk/blender/release/scripts/modules/rigify/neck.py
trunk/blender/release/scripts/modules/rigify/palm.py
trunk/blender/release/scripts/modules/rigify/spine.py
Added Paths:
-----------
trunk/blender/release/scripts/modules/rigify_utils.py
Modified: trunk/blender/release/scripts/modules/rigify/__init__.py
===================================================================
--- trunk/blender/release/scripts/modules/rigify/__init__.py 2009-12-10 11:56:31 UTC (rev 25283)
+++ trunk/blender/release/scripts/modules/rigify/__init__.py 2009-12-10 12:58:03 UTC (rev 25284)
@@ -24,390 +24,25 @@
# TODO, have these in a more general module
from rna_prop_ui import rna_idprop_ui_prop_get
-empty_layer = [False] * 32
+EMPTY_LAYER = [False] * 32
DELIMITER = '-._'
-def auto_class(slots, name="ContainerClass", class_dict=None):
+def submodule_func_from_type(bone_type):
+ type_pair = bone_type.split(".")
- if class_dict:
- class_dict = class_dict.copy()
- else:
- class_dict = {}
+ # '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"
- class_dict["__slots__"] = tuple(slots)
+ submod_name, func_name = type_pair
- return type(name, (object,), class_dict)
-
-
-def auto_class_instance(slots, name="ContainerClass", class_dict=None):
- return auto_class(slots, name, class_dict)()
-
-
-def _bone_class_instance_update(self):
- ''' Re-Assigns bones from the blender data
- '''
- arm = self.obj.data
- bbones = arm.bones
- pbones = self.obj.pose.bones
- ebones = arm.edit_bones
-
- for member in self.attr_names:
- name = getattr(self, member, None)
- if name is not None:
- setattr(self, member + "_b", bbones.get(name, None))
- setattr(self, member + "_p", pbones.get(name, None))
- setattr(self, member + "_e", ebones.get(name, None))
-
-
-def _bone_class_instance_rename(self, attr, new_name):
- ''' Rename bones, editmode only
- '''
-
- if self.obj.mode != 'EDIT':
- raise Exception("Only rename in editmode supported")
-
- ebone = getattr(self, attr + "_e")
- ebone.name = new_name
-
- # we may not get what is asked for so get the name from the editbone
- setattr(self, attr, ebone.name)
-
-
-def _bone_class_instance_copy(self, from_fmt="%s", to_fmt="%s", exclude_attrs=(), base_names=None):
- from_name_ls = []
- new_name_ls = []
- new_slot_ls = []
-
- for attr in self.attr_names:
-
- if attr in exclude_attrs:
- continue
-
- bone_name_orig = getattr(self, attr)
- ebone = getattr(self, attr + "_e")
- # orig_names[attr] = bone_name_orig
-
- # insert formatting
- if from_fmt != "%s":
- bone_name = from_fmt % bone_name_orig
- ebone.name = bone_name
- bone_name = ebone.name # cant be sure we get what we ask for
- else:
- bone_name = bone_name_orig
-
- setattr(self, attr, bone_name)
-
- new_slot_ls.append(attr)
- from_name_ls.append(bone_name)
- if base_names:
- bone_name_orig = base_names[bone_name_orig]
- new_name_ls.append(to_fmt % bone_name_orig)
-
- new_bones = copy_bone_simple_list(self.obj.data, from_name_ls, new_name_ls, True)
- new_bc = bone_class_instance(self.obj, new_slot_ls)
-
- for i, attr in enumerate(new_slot_ls):
- ebone = new_bones[i]
- setattr(new_bc, attr + "_e", ebone)
- setattr(new_bc, attr, ebone.name)
-
- return new_bc
-
-
-def _bone_class_instance_names(self):
- return [getattr(self, attr) for attr in self.attr_names]
-
-
-def _bone_class_instance_blend(self, from_bc, to_bc, target_bone=None, target_prop="blend"):
- '''
- Use for blending bone chains.
-
- blend_target = (bone_name, bone_property)
- default to the last bone, blend prop
-
- XXX - toggles editmode, need to re-validate all editbones :(
- '''
-
- if self.attr_names != from_bc.attr_names or self.attr_names != to_bc.attr_names:
- raise Exception("can only blend between matching chains")
-
- apply_bones = [getattr(self, attr) for attr in self.attr_names]
- from_bones = [getattr(from_bc, attr) for attr in from_bc.attr_names]
- to_bones = [getattr(to_bc, attr) for attr in to_bc.attr_names]
-
- blend_bone_list(self.obj, apply_bones, from_bones, to_bones, target_bone, target_prop)
-
-
-def bone_class_instance(obj, slots, name="BoneContainer"):
+ # from rigify import leg
+ submod = __import__(name="%s.%s" % (__package__, submod_name), fromlist=[submod_name])
- if len(slots) != len(set(slots)):
- raise Exception("duplicate entries found %s" % attr_names)
-
- attr_names = tuple(slots) # dont modify the original
- slots = list(slots) # dont modify the original
- for i in range(len(slots)):
- member = slots[i]
- slots.append(member + "_b") # bone bone
- slots.append(member + "_p") # pose bone
- slots.append(member + "_e") # edit bone
-
- class_dict = { \
- "obj": obj, \
- "attr_names": attr_names, \
- "update": _bone_class_instance_update, \
- "rename": _bone_class_instance_rename, \
- "names": _bone_class_instance_names, \
- "copy": _bone_class_instance_copy, \
- "blend": _bone_class_instance_blend, \
- }
-
- instance = auto_class_instance(slots, name, class_dict)
- return instance
-
-
-def get_bone_data(obj, bone_name):
- arm = obj.data
- pbone = obj.pose.bones[bone_name]
- if obj.mode == 'EDIT':
- bone = arm.edit_bones[bone_name]
- else:
- bone = arm.bones[bone_name]
-
- return arm, pbone, bone
-
-
-def copy_bone_simple(arm, from_bone, name, parent=False):
- ebone = arm.edit_bones[from_bone]
- ebone_new = arm.edit_bones.new(name)
-
- if parent:
- ebone_new.connected = ebone.connected
- ebone_new.parent = ebone.parent
-
- ebone_new.head = ebone.head
- ebone_new.tail = ebone.tail
- ebone_new.roll = ebone.roll
- return ebone_new
-
-
-def copy_bone_simple_list(arm, from_bones, to_bones, parent=False):
-
- if len(from_bones) != len(to_bones):
- raise Exception("bone list sizes must match")
-
- copy_bones = [copy_bone_simple(arm, bone_name, to_bones[i], True) for i, bone_name in enumerate(from_bones)]
-
- # now we need to re-parent
- for ebone in copy_bones:
- parent = ebone.parent
- if parent:
- try:
- i = from_bones.index(parent.name)
- except:
- i = -1
-
- if i == -1:
- ebone.parent = None
- else:
- ebone.parent = copy_bones[i]
-
- return copy_bones
-
-
-def blend_bone_list(obj, apply_bones, from_bones, to_bones, target_bone=None, target_prop="blend"):
-
- if obj.mode == 'EDIT':
- raise Exception("blending cant be called in editmode")
-
- if len(apply_bones) != len(from_bones):
- raise Exception("lists differ in length (from -> apply): \n\t%s\n\t%s" % (from_bones, apply_bones))
- if len(apply_bones) != len(to_bones):
- raise Exception("lists differ in length (to -> apply): \n\t%s\n\t%s" % (to_bones, apply_bones))
-
- # setup the blend property
- if target_bone is None:
- target_bone = apply_bones[-1] # default to the last bone
-
- prop_pbone = obj.pose.bones[target_bone]
- if prop_pbone.get(target_bone, None) is None:
- prop = rna_idprop_ui_prop_get(prop_pbone, target_prop, create=True)
- prop_pbone[target_prop] = 0.5
- prop["soft_min"] = 0.0
- prop["soft_max"] = 1.0
-
- driver_path = prop_pbone.path_to_id() + ('["%s"]' % target_prop)
-
- def blend_target(driver):
- tar = driver.targets.new()
- tar.name = target_bone
- tar.id_type = 'OBJECT'
- tar.id = obj
- tar.rna_path = driver_path
-
- def blend_location(new_pbone, from_bone_name, to_bone_name):
- con = new_pbone.constraints.new('COPY_LOCATION')
- con.target = obj
- con.subtarget = from_bone_name
-
- con = new_pbone.constraints.new('COPY_LOCATION')
- con.target = obj
- con.subtarget = to_bone_name
-
- fcurve = con.driver_add("influence", 0)
- driver = fcurve.driver
- driver.type = 'AVERAGE'
- fcurve.modifiers.remove(0) # grr dont need a modifier
-
- blend_target(driver)
-
- def blend_rotation(new_pbone, from_bone_name, to_bone_name):
- con = new_pbone.constraints.new('COPY_ROTATION')
- con.target = obj
- con.subtarget = from_bone_name
-
- con = new_pbone.constraints.new('COPY_ROTATION')
- con.target = obj
- con.subtarget = to_bone_name
-
- fcurve = con.driver_add("influence", 0)
- driver = fcurve.driver
- driver.type = 'AVERAGE'
- fcurve.modifiers.remove(0) # grr dont need a modifier
-
- blend_target(driver)
-
- for i, new_bone_name in enumerate(apply_bones):
- from_bone_name = from_bones[i]
- to_bone_name = to_bones[i]
-
- # allow skipping some bones by having None in the list
- if None in (new_bone_name, from_bone_name, to_bone_name):
- continue
-
- new_pbone = obj.pose.bones[new_bone_name]
-
- # if the bone is connected or its location is totally locked then dont add location blending.
- if not (new_pbone.bone.connected or (False not in new_pbone.lock_location)):
- blend_location(new_pbone, from_bone_name, to_bone_name)
-
- if not (False not in new_pbone.lock_rotation): # TODO. 4D chech?
- blend_rotation(new_pbone, from_bone_name, to_bone_name)
-
-
-def get_side_name(name):
- '''
- Returns the last part of a string (typically a bone's name) indicating
- whether it is a a left or right (or center, or whatever) bone.
- Returns an empty string if nothing is found.
- '''
- if name[-2] in DELIMITER:
- return name[-2:]
- else:
- return ""
-
-def get_base_name(name):
- '''
- Returns the part of a string (typically a bone's name) corresponding to it's
- base name (no sidedness, no ORG prefix).
- '''
- if name[-2] in DELIMITER:
- return name[:-2]
- else:
- return name
+ return submod, getattr(submod, func_name)
-def add_stretch_to(obj, from_name, to_name, name):
- '''
- Adds a bone that stretches from one to another
- '''
-
- mode_orig = obj.mode
- bpy.ops.object.mode_set(mode='EDIT')
-
- arm = obj.data
- stretch_ebone = arm.edit_bones.new(name)
- stretch_name = stretch_ebone.name
- del name
-
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list