benchmark bvh output..

spellchecked/expanded comments in armature_symetry.py and tweaked functionality
This commit is contained in:
2006-08-04 12:48:29 +00:00
parent 4ae12081c0
commit 0c748032a6
2 changed files with 97 additions and 52 deletions

View File

@@ -12,9 +12,9 @@ __url__ = ("blender", "blenderartist")
__version__ = "1.0 2006-7-26"
__doc__ = """\
This script creates perfectly symmetrical armatures.
This script creates perfectly symmetrical armatures,
based on the best fit when comparing the mirrored locations of 2 bones.
Hidden bones are ignored, and you can optionaly only operate on selected bones
Hidden bones are ignored, and optionally only operate on selected bones.
"""
# ***** BEGIN GPL LICENSE BLOCK *****
@@ -44,6 +44,9 @@ Vector= Blender.Mathutils.Vector
def VecXFlip(vec):
'''
Return a copy of this vector x flipped.
'''
x,y,z= vec
return Vector(-x,y,z)
@@ -59,18 +62,18 @@ def editbone_mirror_diff(editbone1, editbone2):
t1= editbone1.tail
t2= editbone2.tail
# Mirror bone 2's location
# Mirror bone2's location
h2= VecXFlip(h2)
t2= VecXFlip(t2)
#return (h1-h2).length + (t1-t2).length
#return (h1-h2).length + (t1-t2).length # returns the length only
# For this function its easier to return the bones also
return ((h1-h2).length + (t1-t2).length)/2, editbone1, editbone2
def editbone_mirror_merge(editbone1, editbone2, PREF_MODE_L2R, PREF_MODE_R2L):
'''
Merge these 2 bones mirror
Merge these 2 bones to their mirrored locations
'''
h1= editbone1.head
h2= editbone2.head
@@ -79,7 +82,7 @@ def editbone_mirror_merge(editbone1, editbone2, PREF_MODE_L2R, PREF_MODE_R2L):
t2= editbone2.tail
if PREF_MODE_L2R and PREF_MODE_R2L:
# Median, flip bone 2's locations and average, then apply to editbone1, flip and apply to editbone2
# Median, flip bone2's locations and average, then apply to editbone1, flip and apply to editbone2
h2_f= VecXFlip(h2)
t2_f= VecXFlip(t2)
@@ -122,7 +125,12 @@ def editbone_mirror_merge(editbone1, editbone2, PREF_MODE_L2R, PREF_MODE_R2L):
if IS_XMIRROR_SOURCE( h1.x ):# head bone 1s negative, so copy it to h2
editbone2.head= VecXFlip(h1)
else: # assume h2.x<0 - not a big deal if were wrong, its unlikely to ever happen because the bones would both be on the same side.
else:
'''
assume h2.x<0 - not a big deal if were wrong,
its unlikely to ever happen because the bones would both be on the same side.
'''
# head bone 2s negative, so copy it to h1
editbone1.head= VecXFlip(h2)
@@ -132,49 +140,61 @@ def editbone_mirror_merge(editbone1, editbone2, PREF_MODE_L2R, PREF_MODE_R2L):
else:
editbone1.tail= VecXFlip(t2)
# Copy roll from 1 bone to another, use the head's location to deciede which side were on.
# Copy roll from 1 bone to another, use the head's location to decide which side it's on.
if IS_XMIRROR_SOURCE(editbone1.head):
editbone2.roll= -editbone1.roll
else:
editbone1.roll= -editbone2.roll
def armature_symetry(arm_ob, PREF_MAX_DIST, PREF_XMID_SNAP, PREF_XZERO_THRESH, PREF_MODE_L2R, PREF_MODE_R2L, PREF_SEL_ONLY):
def armature_symetry(\
arm_ob,\
PREF_MAX_DIST,\
PREF_XMID_SNAP,\
PREF_XZERO_THRESH,\
PREF_MODE_L2R,\
PREF_MODE_R2L,\
PREF_SEL_ONLY):
'''
Main function that does all the work,
return the number of
'''
arm_data= arm_ob.data
arm_data.makeEditable()
# Get the bones
bones= []
H= Blender.Armature.HIDDEN_EDIT
S= Blender.Armature.BONE_SELECTED
HIDDEN_EDIT= Blender.Armature.HIDDEN_EDIT
BONE_SELECTED= Blender.Armature.BONE_SELECTED
if PREF_SEL_ONLY:
for eb in arm_data.bones.values():
options= eb.options
if H not in options and S in options:
if HIDDEN_EDIT not in options and BONE_SELECTED in options:
bones.append(eb)
else:
# All non hidden bones
for eb in arm_data.bones.values():
options= eb.options
if H not in options:
if HIDDEN_EDIT not in options:
bones.append(eb)
del H
del S
del HIDDEN_EDIT # remove temp variables
del BONE_SELECTED
# Store the numder of bones we have modified for a message
tot_editbones= len(bones)
tot_editbones_modified= 0
if PREF_XMID_SNAP:
# Remove middle bones
# reverse loop so we can pop
# Remove bones that are in the middle (X Zero)
# reverse loop so we can remove items in the list.
for eb_idx in xrange(len(bones)-1, -1, -1):
edit_bone= bones[eb_idx]
#print edit_bone.options
if abs(edit_bone.head.x) + abs(edit_bone.tail.x) <= PREF_XZERO_THRESH/2:
# print 'Found Middle Bone'
# This is a center bone, clamp and remove
# This is a center bone, clamp and remove from the bone list so we dont use again.
edit_bone.tail.x= edit_bone.head.x= 0
del bones[eb_idx]
@@ -183,20 +203,19 @@ def armature_symetry(arm_ob, PREF_MAX_DIST, PREF_XMID_SNAP, PREF_XZERO_THRESH, P
bone_comparisons= []
# Compare every bone with every other bone, shouldent be too slow, though we may want to cache head/tale values
# The 2 for's only compare once
# Compare every bone with every other bone, shouldn't be too slow.
# These 2 "for" loops only compare once
for eb_idx_a in xrange(len(bones)-1, -1, -1):
edit_bone_a= bones[eb_idx_a]
for eb_idx_b in xrange(eb_idx_a-1, -1, -1):
edit_bone_b= bones[eb_idx_b]
# print 'Adding comparison', eb_idx_a, eb_idx_b
# Error float is first so we can sort it
# Error float the first value from editbone_mirror_diff() so we can sort the resulting list.
bone_comparisons.append(editbone_mirror_diff(edit_bone_a, edit_bone_b))
bone_comparisons.sort() # best matches first
# Make a dict of bone names that have been used so we dont mirror more then once
# Make a dict() of bone names that have been used so we dont mirror more then once
bone_mirrored= {}
for error, editbone1, editbone2 in bone_comparisons:
@@ -206,29 +225,26 @@ def armature_symetry(arm_ob, PREF_MAX_DIST, PREF_XMID_SNAP, PREF_XZERO_THRESH, P
break
if not bone_mirrored.has_key(editbone1.name) and not bone_mirrored.has_key(editbone2.name):
# Were not used- execute the mirror
# Were not used, execute the mirror
editbone_mirror_merge(editbone1, editbone2, PREF_MODE_L2R, PREF_MODE_R2L)
# print 'Merging bones'
# Add ourselves so we arnt touced again
bone_mirrored[editbone1.name] = None # dummy value
bone_mirrored[editbone2.name] = None # dummy value
# If both are true then we changed 2 bones
# Add ourselves so we aren't touched again
bone_mirrored[editbone1.name] = None # dummy value, would use sets in python 2.4
bone_mirrored[editbone2.name] = None
# If both options are enabled, then we have changed 2 bones
tot_editbones_modified+= PREF_MODE_L2R + PREF_MODE_R2L
arm_data.update() # out of editmode
# Print results
if PREF_SEL_ONLY:
msg= 'moved %i bones of %i selected' % (tot_editbones_modified, tot_editbones)
else:
msg= 'moved %i bones of %i visible' % (tot_editbones_modified, tot_editbones)
Blender.Draw.PupMenu(msg)
arm_data.update() # get out of armature editmode
return tot_editbones, tot_editbones_modified
def main():
# Cant be in editmode for armature.makeEditable()
'''
User interface function that gets the options and calls armature_symetry()
'''
scn= Scene.GetCurrent()
arm_ob= scn.getActiveObject()
@@ -236,17 +252,19 @@ def main():
Blender.Draw.PupMenu('No Armature object selected.')
return
Blender.Window.EditMode(0)
# Cant be in editmode for armature.makeEditable()
is_editmode= Blender.Window.EditMode()
if is_editmode: Blender.Window.EditMode(0)
Draw= Blender.Draw
# Defaults
# Defaults for the user input
PREF_XMID_SNAP= Draw.Create(1)
PREF_MAX_DIST= Draw.Create(0.4)
PREF_XZERO_THRESH= Draw.Create(0.02)
#PREF_MODE= Draw.Create(0) # THIS IS TOOO CONFUSING, HAVE 2 BUTTONS AND MAKE THE MODE FROM THEM.
PREF_MODE_L2R= Draw.Create(1)
PREF_MODE_R2L= Draw.Create(0)
PREF_SEL_ONLY= Draw.Create(0)
PREF_SEL_ONLY= Draw.Create(1)
pup_block = [\
'Left (-), Right (+)',\
@@ -254,15 +272,16 @@ def main():
('Right > Left', PREF_MODE_R2L, 'Copy from the Right to Left of the mesh. Enable Both for a mid loc.'),\
'',\
('MaxDist:', PREF_MAX_DIST, 0.0, 4.0, 'Maximum difference in mirror bones to match up pairs.'),\
('XZero limit:', PREF_XZERO_THRESH, 0.0, 2.0, 'Tolorence for locking bones into the middle (X/zero).'),\
('XZero limit:', PREF_XZERO_THRESH, 0.0, 2.0, 'Tolerance for locking bones into the middle (X/zero).'),\
('XMidSnap Bones', PREF_XMID_SNAP, 'Snap middle verts to X Zero (uses XZero limit)'),\
('Selected Only', PREF_SEL_ONLY, 'Only xmirror selected bones.'),\
]
# Popup, exit if the user doesn't click OK
if not Draw.PupBlock("X Mirror mesh tool", pup_block):
return
# Replace the variables with their button values.
PREF_XMID_SNAP= PREF_XMID_SNAP.val
PREF_MAX_DIST= PREF_MAX_DIST.val
PREF_MODE_L2R= PREF_MODE_L2R.val
@@ -274,11 +293,30 @@ def main():
if not PREF_MODE_R2L and not PREF_MODE_L2R:
PREF_MODE_R2L= PREF_MODE_L2R= True
armature_symetry(arm_ob, PREF_MAX_DIST, PREF_XMID_SNAP, PREF_XZERO_THRESH, PREF_MODE_L2R, PREF_MODE_R2L, PREF_SEL_ONLY)
tot_editbones, tot_editbones_modified = armature_symetry(\
arm_ob,\
PREF_MAX_DIST,\
PREF_XMID_SNAP,\
PREF_XZERO_THRESH,\
PREF_MODE_L2R,\
PREF_MODE_R2L,\
PREF_SEL_ONLY)
if is_editmode: Blender.Window.EditMode(1)
# Redraw all views before popup
Blender.Window.RedrawAll()
# Print results
if PREF_SEL_ONLY:
msg= 'moved %i bones of %i selected' % (tot_editbones_modified, tot_editbones)
else:
msg= 'moved %i bones of %i visible' % (tot_editbones_modified, tot_editbones)
Blender.Draw.PupMenu(msg)
# Check for __main__ so this function can be imported by other scripts without running the script.
if __name__=='__main__':
main()

View File

@@ -713,9 +713,16 @@ def load_bvh_ui(file):
return
Blender.Window.WaitCursor(1)
# Get the BVH data and act on it.
t1= Blender.sys.time()
print '\tpassing bvh...',
bvh_nodes= read_bvh(file, IMPORT_SCALE)
print '%.4f' % (Blender.sys.time()-t1)
t1= Blender.sys.time()
print '\timporting to blender...',
if IMPORT_AS_ARMATURE: bvh_node_dict2armature(bvh_nodes, IMPORT_START_FRAME)
if IMPORT_AS_EMPTIES: bvh_node_dict2objects(bvh_nodes, IMPORT_START_FRAME)
print 'Done in %.4f\n' % (Blender.sys.time()-t1)
Blender.Window.WaitCursor(0)