Skip to content

Commit

Permalink
slight knee deformations for kneeling poses
Browse files Browse the repository at this point in the history
scale 1.0 for maps
  • Loading branch information
FlailingFog committed Feb 4, 2023
1 parent 657b292 commit 571662b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 64 deletions.
34 changes: 20 additions & 14 deletions extras/animationlibrary/createanimationlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@
from...importing.importbuttons import kklog, toggle_console
def main(folder):

kkbp_character = False
#delete the armature before starting to reduce console spam
if bpy.data.objects.get('Armature'):
if bpy.data.objects.get('Body'):
if bpy.data.objects['Body'].get('KKBP materials'):
kkbp_character = True
if bpy.data.objects.get('Armature') and kkbp_character:
n = bpy.data.objects['Armature'].data.name
bpy.data.objects.remove(bpy.data.objects['Armature'])
bpy.data.armatures.remove(bpy.data.armatures[n])
Expand All @@ -49,15 +53,16 @@ def main(folder):
bpy.context.view_layer.objects.active=rigify_armature
bpy.ops.object.mode_set(mode = 'OBJECT')

#disable IKs
rigify_armature.pose.bones["Right arm_parent"]["IK_FK"] = 1
rigify_armature.pose.bones["Left arm_parent"]["IK_FK"] = 1
rigify_armature.pose.bones["Right leg_parent"]["IK_FK"] = 1
rigify_armature.pose.bones["Left leg_parent"]["IK_FK"] = 1
if kkbp_character:
#disable IKs
rigify_armature.pose.bones["Right arm_parent"]["IK_FK"] = 1
rigify_armature.pose.bones["Left arm_parent"]["IK_FK"] = 1
rigify_armature.pose.bones["Right leg_parent"]["IK_FK"] = 1
rigify_armature.pose.bones["Left leg_parent"]["IK_FK"] = 1

#disable head follow
rigify_armature.pose.bones['torso']['neck_follow'] = 1.0
rigify_armature.pose.bones['torso']['head_follow'] = 1.0
#disable head follow
rigify_armature.pose.bones['torso']['neck_follow'] = 1.0
rigify_armature.pose.bones['torso']['head_follow'] = 1.0

'''#reset all bone transforms
for pb in bpy.context.selected_pose_bones_from_active_object:
Expand Down Expand Up @@ -238,11 +243,12 @@ def main(folder):
#'Left Buttock handle',
#'Right Buttock handle',
]
for action in rigify_armature.animation_data.action.fcurves:
bone_name = action.data_path[action.data_path.find('[')+2:action.data_path.find('].')-1]
if bone_name not in retargeting_list:
rigify_armature.animation_data.action.fcurves.remove(action) #remove keyframes
rigify_armature.pose.bones[bone_name].matrix_basis = mathutils.Matrix()
if kkbp_character:
for action in rigify_armature.animation_data.action.fcurves:
bone_name = action.data_path[action.data_path.find('[')+2:action.data_path.find('].')-1]
if bone_name not in retargeting_list:
rigify_armature.animation_data.action.fcurves.remove(action) #remove keyframes
rigify_armature.pose.bones[bone_name].matrix_basis = mathutils.Matrix()

#select all rigify armature bones and create pose asset
bpy.ops.object.select_all(action='DESELECT')
Expand Down
2 changes: 1 addition & 1 deletion extras/animationlibrary/createmapassetlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def better_fbx_map_import(directory):
if '.fbx' in str(item):
obj_list.append(str(item))
for obj in obj_list:
bpy.ops.better_import.fbx(filepath = obj, my_scale = 1.2)
bpy.ops.better_import.fbx(filepath = obj, my_scale = 1.0)

#if nothing was imported, skip
if len(bpy.data.objects) < 2:
Expand Down
36 changes: 0 additions & 36 deletions extras/rigifywrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,6 @@ def execute(self, context):
bpy.ops.pose.rigify_generate()

bpy.ops.kkb.rigafter('INVOKE_DEFAULT')

'''#make sure things are parented correctly and hide original armature
armature = bpy.data.objects['Armature']
for child in armature.children:
if child.name in ['Body'] or 'Outfit ' in child.name:
print(child.name)
#do for children first
for ob in child.children:
if ob.name in ['Tears', 'Gag Eyes', 'Tongue (rigged)'] or 'Outfit ' in ob.name:
print(ob.name)
hidden = ob.hide
parent = ob.parent
ob.hide = False
bpy.ops.object.select_all(action='DESELECT')
ob.parent = None
ob.select_set(True)
rig.select_set(True)
bpy.context.view_layer.objects.active=rig
bpy.ops.object.parent_set(type='ARMATURE_NAME')
ob.hide = hidden
ob.parent = parent
hidden = child.hide
child.hide = False
bpy.ops.object.select_all(action='DESELECT')
child.parent = None
child.select_set(True)
rig.select_set(True)
bpy.context.view_layer.objects.active=rig
bpy.ops.object.parent_set(type='ARMATURE_NAME')
child.hide = hidden
#find the last created armature modifier, replace it with the existing one
child.modifiers[0].object = child.modifiers[-1].object
child.modifiers.remove(child.modifiers[-1])
child.modifiers[0].name = 'Rigify Armature' '''

#make sure the new bones on the generated rig retain the KKBP outfit id entry
rig = bpy.context.active_object
Expand Down
41 changes: 28 additions & 13 deletions importing/bonedrivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def heelController(footbone, footIK, toebone):
heelIK.parent = masterbone
heelIK.tail.y *= .5


#parent footIK to heel controller
armatureData.edit_bones[footIK].parent = heelIK

Expand Down Expand Up @@ -275,13 +274,13 @@ def setup_joints():
bpy.ops.object.mode_set(mode='POSE')

#generic function to set a copy rotation modifier
def set_copy(bone, bonetarget, influence, axis = 'all', mix = 'replace'):
def set_copy(bone, bonetarget, influence, axis = 'all', mix = 'replace', space = 'LOCAL'):
constraint = bpy.data.objects['Armature'].pose.bones[bone].constraints.new("COPY_ROTATION")
constraint.target = bpy.data.objects['Armature']
constraint.subtarget = bonetarget
constraint.influence = influence
constraint.target_space = 'LOCAL'
constraint.owner_space = 'LOCAL'
constraint.target_space = space
constraint.owner_space = space

if axis == 'X':
constraint.use_y = False
Expand Down Expand Up @@ -322,8 +321,8 @@ def set_copy(bone, bonetarget, influence, axis = 'all', mix = 'replace'):
set_copy('cf_d_wrist_L', 'cf_j_hand_L', 0.33, axis = 'X', )
set_copy('cf_d_wrist_R', 'cf_j_hand_R', 0.33, axis = 'X')

#set_copy('cf_s_leg_L', 'cf_j_thigh00_L', 0.25, axis = 'X', mix = 'add')
#set_copy('cf_s_leg_R', 'cf_j_thigh00_R', 0.25, axis = 'X', mix = 'add')
set_copy('cf_d_kneeF_L', 'cf_j_leg01_L', 0.5, axis = 'antiX', mix = 'add')
set_copy('cf_d_kneeF_R', 'cf_j_leg01_R', 0.5, axis = 'antiX', mix = 'add')

set_copy('cf_d_siri_L', 'cf_j_thigh00_L', 0.33)
set_copy('cf_d_siri_R', 'cf_j_thigh00_R', 0.33)
Expand Down Expand Up @@ -360,6 +359,7 @@ def setDriver (bone, drivertype, drivertypeselect, drivertarget, drivertt, drive
#drivertypeselect is the component of the bone you want the driver to be applied to
# for location it's (0 is x component, y is 1, z is 2)
# for rotation it's (0 is w, 1 is x, etc)
# for scale it's (0 is x, 1 is y, 2 is z)
driver = bpy.data.objects['Armature'].pose.bones[bone].driver_add(drivertype, drivertypeselect)

#add driver variable
Expand All @@ -377,14 +377,13 @@ def setDriver (bone, drivertype, drivertypeselect, drivertarget, drivertt, drive

#set the transform space. can be world space too
target.transform_space = 'LOCAL_SPACE'

target.rotation_mode = 'AUTO' #or QUATERNION
target.rotation_mode = 'QUATERNION' if expresstype in ['scale', 'quat'] else 'AUTO'

#use the distance to the target bone's parent to make results consistent for different sized bones
targetbonelength = str(round((bpy.data.objects['Armature'].pose.bones[drivertarget].head - bpy.data.objects['Armature'].pose.bones[drivertarget].parent.head).length,3))

#driver expression is the rotation value of the target bone multiplied by a percentage of the driver target bone's length
if expresstype == 'move':
if expresstype in ['move', 'quat']:
driver.driver.expression = vari.name + '*' + targetbonelength + '*' + drivermult

#move but only during positive rotations
Expand All @@ -406,22 +405,38 @@ def setDriver (bone, drivertype, drivertypeselect, drivertarget, drivertt, drive
#move but exponentially
elif expresstype == 'moveexp':
driver.driver.expression = vari.name + '*' + vari.name + '*' + targetbonelength + '*' + drivermult

elif expresstype == 'scale':
driver.driver.expression = '1 + ' + vari.name + '*' + targetbonelength + '*' + drivermult

elif expresstype == 'rotation':
driver.driver.expression = vari.name + '*' + targetbonelength + '*' + drivermult

#Set the remaining joint correction drivers
#bone directions change based on the PMX vs FBX origin because the Y and Z axis for each bone are swapped
#set knee joint corrections. These go in toward the body and down toward the foot at an exponential rate
setDriver('cf_s_kneeB_R', 'location', 1, 'cf_j_leg01_R', 'ROT_X', '-0.2', expresstype = 'moveexp')
setDriver('cf_s_kneeB_R', 'location', 2, 'cf_j_leg01_R', 'ROT_X', '-0.08')

setDriver('cf_s_kneeB_L', 'location', 1, 'cf_j_leg01_L', 'ROT_X', '-0.2', expresstype = 'moveexp')
setDriver('cf_s_kneeB_L', 'location', 2, 'cf_j_leg01_L', 'ROT_X', '-0.08')

#knee tip corrections go up toward the waist and in toward the body
#knee correction to thicken the knee in a kneeling pose
setDriver('cf_s_leg01_R', 'scale', 2, 'cf_j_leg01_R', 'ROT_X', '1', expresstype = 'scale')
setDriver('cf_s_leg01_R', 'scale', 0, 'cf_j_leg01_R', 'ROT_X', '-2', expresstype = 'scale')
setDriver('cf_s_leg01_R', 'location', 2, 'cf_j_leg01_R', 'ROT_X', '0.05', expresstype='quat')
setDriver('cf_d_thigh03_R', 'location', 2, 'cf_j_leg01_R', 'ROT_X', '.015')

setDriver('cf_s_leg01_L', 'scale', 2, 'cf_j_leg01_L', 'ROT_X', '1', expresstype = 'scale')
setDriver('cf_s_leg01_L', 'scale', 0, 'cf_j_leg01_L', 'ROT_X', '-2', expresstype = 'scale')
setDriver('cf_s_leg01_L', 'location', 2, 'cf_j_leg01_L', 'ROT_X', '0.05', expresstype='quat')
setDriver('cf_d_thigh03_L', 'location', 2, 'cf_j_leg01_L', 'ROT_X', '-.015')

#knee tip corrections go up toward the waist and in toward the body, also rotate a bit
setDriver('cf_d_kneeF_R', 'location', 1, 'cf_j_leg01_R', 'ROT_X', '0.02')
setDriver('cf_d_kneeF_R', 'location', 2, 'cf_j_leg01_R', 'ROT_X', '-0.02')
setDriver('cf_d_kneeF_R', 'location', 2, 'cf_j_leg01_R', 'ROT_X', '-0.04')

setDriver('cf_d_kneeF_L', 'location', 1, 'cf_j_leg01_L', 'ROT_X', '0.02')
setDriver('cf_d_kneeF_L', 'location', 2, 'cf_j_leg01_L', 'ROT_X', '-0.02')
setDriver('cf_d_kneeF_L', 'location', 2, 'cf_j_leg01_L', 'ROT_X', '-0.04')

#butt corrections go slightly up to the spine and in to the waist
setDriver('cf_d_siri_R', 'location', 1, 'cf_j_thigh00_R', 'ROT_X', '0.02')
Expand Down

0 comments on commit 571662b

Please sign in to comment.