2022-04-21 17:44:35 +02:00
|
|
|
from doctest import SKIP
|
2021-04-08 19:25:05 +02:00
|
|
|
import bpy, re
|
2021-04-06 18:30:25 +02:00
|
|
|
from . import fn
|
2021-04-08 19:25:05 +02:00
|
|
|
from time import time
|
2022-04-07 14:36:47 +02:00
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
## step 3
|
|
|
|
# - Bake cycle modifier keys -chained with- step the animation path
|
|
|
|
# - Pin the feet (separated ops)
|
|
|
|
|
2022-04-21 17:44:35 +02:00
|
|
|
def bake_cycle(on_selection=True, end=None):
|
2021-04-06 18:30:25 +02:00
|
|
|
print(fn.helper())
|
2022-04-21 17:44:35 +02:00
|
|
|
end = end or bpy.context.scene.frame_end
|
2022-04-25 18:12:24 +02:00
|
|
|
|
2022-04-21 18:42:38 +02:00
|
|
|
print('end: ', end)
|
2021-04-08 19:25:05 +02:00
|
|
|
debug = fn.get_addon_prefs().debug
|
2021-04-06 18:30:25 +02:00
|
|
|
obj = bpy.context.object
|
|
|
|
if obj.type != 'ARMATURE':
|
|
|
|
print('ERROR', 'active is not an armature type')
|
|
|
|
return
|
|
|
|
|
2022-04-12 11:21:00 +02:00
|
|
|
act = fn.set_baked_action(obj)
|
2021-04-06 18:30:25 +02:00
|
|
|
if not act:
|
|
|
|
return
|
2021-04-08 19:25:05 +02:00
|
|
|
|
2022-04-25 18:12:24 +02:00
|
|
|
if debug: print('action:', act.name)
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
# obj.animation_data.action = act
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
ct_fcu = len(act.fcurves)
|
2021-04-06 18:30:25 +02:00
|
|
|
ct = 0
|
2021-04-08 19:25:05 +02:00
|
|
|
ct_no_cycle = 0
|
|
|
|
|
2022-04-25 18:12:24 +02:00
|
|
|
# all_keys = [k.co.x for fc in act.fcurves if not '.offset' in fc.data_path for k in fc.keyframe_points]
|
|
|
|
all_keys = fn.get_x_pos_of_visible_keys(obj, act)
|
|
|
|
first = min(all_keys) # int(min(all_keys))
|
|
|
|
last = max(all_keys) # int(max(all_keys))
|
2022-04-21 18:42:38 +02:00
|
|
|
offset = last - first
|
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
for fcu in act.fcurves:
|
|
|
|
|
|
|
|
## if a curve is not cycled don't touch
|
|
|
|
if not [m for m in fcu.modifiers if m.type == 'CYCLES']:
|
2021-04-08 19:25:05 +02:00
|
|
|
ct_no_cycle += 1
|
2021-04-06 18:30:25 +02:00
|
|
|
continue
|
2021-04-08 19:25:05 +02:00
|
|
|
|
|
|
|
if debug: print(fcu.data_path, 'has cycle')
|
2021-04-06 18:30:25 +02:00
|
|
|
#-# only on location :
|
|
|
|
# if not fcu.data_path.endswith('.location'):
|
|
|
|
# continue
|
|
|
|
|
|
|
|
# prop = fcu.data_path.split('.')[-1]
|
|
|
|
|
|
|
|
b_name = fcu.data_path.split('"')[1]
|
2021-04-08 19:25:05 +02:00
|
|
|
if debug: print(b_name, 'has cycle')
|
2021-04-06 18:30:25 +02:00
|
|
|
pb = obj.pose.bones.get(b_name)
|
|
|
|
if not pb:
|
|
|
|
print(f'{b_name} is invalid')
|
|
|
|
continue
|
|
|
|
|
|
|
|
#-# limit on selection if passed
|
|
|
|
if on_selection and not pb.bone.select:
|
|
|
|
continue
|
2021-04-08 19:25:05 +02:00
|
|
|
|
|
|
|
if debug: print(b_name, 'seems ok')
|
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
#-# only on selected and visible curve
|
|
|
|
# if not fcu.select or fcu.hide:
|
|
|
|
# continue
|
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
fcu_kfs = []
|
|
|
|
for k in fcu.keyframe_points:
|
|
|
|
k_dic = {}
|
|
|
|
# k_dic['k'] = k
|
|
|
|
k_dic['co'] = k.co
|
|
|
|
k_dic['interpolation'] = k.interpolation
|
|
|
|
k_dic['type'] = k.type
|
|
|
|
fcu_kfs.append(k_dic)
|
|
|
|
|
2022-04-21 18:42:38 +02:00
|
|
|
current_offset = offset
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
keys_num = len(fcu_kfs)
|
2022-04-08 19:35:20 +02:00
|
|
|
if debug >= 2: print(keys_num)
|
2021-04-08 19:25:05 +02:00
|
|
|
|
|
|
|
if keys_num <= 1:
|
2022-04-08 19:35:20 +02:00
|
|
|
if debug >= 2: print(b_name, f'{keys_num} key')
|
2021-04-08 19:25:05 +02:00
|
|
|
continue
|
|
|
|
|
2022-04-21 18:42:38 +02:00
|
|
|
## delete last after computing offset IF cycle have first frame repeatead as last !
|
|
|
|
|
|
|
|
# fcu_kfs_without_last = fcu_kfs.copy()
|
|
|
|
# fcu_kfs_without_last.pop()
|
|
|
|
|
|
|
|
# last_kf = fcu_kfs.pop() (or just iterate with slicing [:-1])
|
2021-04-08 19:25:05 +02:00
|
|
|
|
2022-04-08 19:35:20 +02:00
|
|
|
if debug >= 2: print('keys', len(fcu_kfs))
|
2022-04-21 17:44:35 +02:00
|
|
|
|
2022-04-21 18:42:38 +02:00
|
|
|
iterations = int( ((end - last) // offset) + 1 )
|
2022-04-08 19:35:20 +02:00
|
|
|
if debug >= 2: print('iterations: ', iterations)
|
2022-04-21 18:42:38 +02:00
|
|
|
|
|
|
|
for i in range(iterations):
|
|
|
|
# if i == iterations - 1: # last
|
|
|
|
# kfs = fcu_kfs
|
|
|
|
# else:
|
|
|
|
# kfs = fcu_kfs_without_last
|
|
|
|
# for kf in kfs:
|
|
|
|
|
|
|
|
for count, kf in enumerate(fcu_kfs):
|
|
|
|
if count == keys_num and i < iterations - 1:
|
|
|
|
# last key of fcurves, to use only if on last iteration
|
|
|
|
continue
|
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
# create a new key, adding offset to keys
|
|
|
|
fcu.keyframe_points.add(1)
|
|
|
|
new = fcu.keyframe_points[-1]
|
|
|
|
for att, val in kf.items():
|
|
|
|
if att == 'co':
|
|
|
|
new.co = (val[0] + current_offset, val[1])
|
|
|
|
else:
|
|
|
|
setattr(new, att, val)
|
|
|
|
current_offset += offset
|
2021-04-08 19:25:05 +02:00
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
ct += 1
|
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
if ct_fcu == ct_no_cycle: # skipped because no cycle exists
|
2022-04-21 17:44:35 +02:00
|
|
|
re_baked = re.compile(r'_baked\.?\d{0,3}$')
|
|
|
|
if re_baked.search(act.name):
|
2021-04-08 19:25:05 +02:00
|
|
|
# is an autogenerated one
|
2022-04-21 17:44:35 +02:00
|
|
|
org_action_name = re_baked.sub('', act.name)
|
2021-04-08 19:25:05 +02:00
|
|
|
org_action = bpy.data.actions.get(org_action_name)
|
|
|
|
if not org_action:
|
2022-04-25 18:12:24 +02:00
|
|
|
return ('ERROR', 'No fcurve with anim cycle found (on baked action)')
|
2021-04-08 19:25:05 +02:00
|
|
|
obj.animation_data.action = org_action
|
2022-04-26 11:04:44 +02:00
|
|
|
return ('ERROR', 'No fcurve with cyclic modifier found (used to determine what to bake)')
|
2021-04-08 19:25:05 +02:00
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
if not ct:
|
2022-04-25 18:12:24 +02:00
|
|
|
return ('ERROR', 'No fcurve treated (! action duplicated to _baked !)')
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-01 12:12:05 +02:00
|
|
|
# cleaning update
|
|
|
|
fn.update_action(act)
|
2021-04-06 18:30:25 +02:00
|
|
|
print('end of anim cycle keys baking')
|
|
|
|
# C.scene.frame_current = org_frame
|
2022-04-21 17:44:35 +02:00
|
|
|
# detect last key in contact
|
2021-04-06 18:30:25 +02:00
|
|
|
|
|
|
|
def step_path():
|
2022-04-01 12:12:05 +02:00
|
|
|
'''Step the path anim of the curve to constant'''
|
2021-04-06 18:30:25 +02:00
|
|
|
print(fn.helper())
|
|
|
|
|
2022-04-21 17:44:35 +02:00
|
|
|
ob = bpy.context.object
|
|
|
|
if ob.type != 'ARMATURE':
|
|
|
|
return ('ERROR', 'active is not an armature type')
|
|
|
|
|
|
|
|
act = fn.get_obj_action(ob)
|
|
|
|
if not act:
|
|
|
|
return
|
|
|
|
|
|
|
|
# CHANGE - removed int from frame
|
|
|
|
# keyframes = [int(k.co[0]) for fcu in act.fcurves for k in fcu.keyframe_points]
|
|
|
|
keyframes = [k.co[0] for fcu in act.fcurves for k in fcu.keyframe_points]
|
|
|
|
keyframes = list(set(keyframes))
|
|
|
|
|
|
|
|
offset_fc = None
|
|
|
|
for fc in act.fcurves:
|
|
|
|
if all(x in fc.data_path for x in ('pose.bones', 'constraints', 'offset')):
|
|
|
|
offset_fc = fc
|
|
|
|
|
|
|
|
if not offset_fc:
|
|
|
|
return ('ERROR', f'no offset animation in action {act.name}')
|
|
|
|
|
|
|
|
data_path = offset_fc.data_path
|
|
|
|
const = ob.path_resolve(data_path.rsplit('.', 1)[0])
|
|
|
|
|
|
|
|
timevalues = [offset_fc.evaluate(kf) for kf in keyframes]
|
|
|
|
for kf, value in zip(keyframes, timevalues):
|
|
|
|
## or use t_fcu.keyframe_points.add(len(kf))
|
|
|
|
|
|
|
|
|
|
|
|
const.offset = value
|
|
|
|
const.keyframe_insert('offset', frame=kf, options={'INSERTKEY_AVAILABLE'})
|
|
|
|
# ``INSERTKEY_NEEDED````INSERTKEY_AVAILABLE`` (only available channels)
|
|
|
|
|
|
|
|
## set all to constant
|
|
|
|
for k in offset_fc.keyframe_points:
|
|
|
|
k.interpolation = 'CONSTANT'
|
|
|
|
|
|
|
|
# cleaning update (might not be needed here)
|
|
|
|
fn.update_action(act)
|
|
|
|
print('end of step_anim')
|
|
|
|
|
2022-04-20 12:02:19 +02:00
|
|
|
class AW_OT_bake_cycle_and_step(bpy.types.Operator):
|
|
|
|
bl_idname = "autowalk.bake_cycle_and_step"
|
2022-04-21 17:44:35 +02:00
|
|
|
bl_label = "Bake keys"
|
|
|
|
bl_description = "Bake the keys to a new baked animation\
|
|
|
|
\nStep path animation according to those key (if not in Linear)\
|
2022-04-13 18:38:03 +02:00
|
|
|
\n(duplicate to a new 'baked' action)"
|
2021-04-06 18:30:25 +02:00
|
|
|
bl_options = {"REGISTER", "UNDO"}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return context.object and context.object.type == 'ARMATURE'
|
|
|
|
|
2022-04-21 17:44:35 +02:00
|
|
|
end_frame : bpy.props.IntProperty(name='End Frame',options={'SKIP_SAVE'})
|
|
|
|
|
|
|
|
def invoke(self,context,event):
|
|
|
|
self.end_frame = context.scene.frame_end
|
2022-04-25 18:12:24 +02:00
|
|
|
act = fn.get_obj_action(context.object)
|
|
|
|
if not act:
|
|
|
|
self.report({'ERROR'}, 'No Animation set on active object')
|
|
|
|
return {"CANCELLED"}
|
|
|
|
|
|
|
|
act = fn.get_origin_action(act)
|
|
|
|
# all_keys = [k.co.x for fc in act.fcurves if not '.offset' in fc.data_path for k in fc.keyframe_points]
|
|
|
|
all_keys = fn.get_x_pos_of_visible_keys(context.object, act) # no offset and only visible bone layers
|
|
|
|
self.first = min(all_keys) # int(min(all_keys))
|
|
|
|
self.last = max(all_keys) # int(max(all_keys))
|
|
|
|
self.offset = self.last - self.first
|
2022-04-21 17:44:35 +02:00
|
|
|
# return self.execute(context) # uncomment only this to skip pop-up and keep scene.end
|
|
|
|
return context.window_manager.invoke_props_dialog(self) # width=400
|
|
|
|
|
|
|
|
def draw(self, context):
|
|
|
|
layout = self.layout
|
|
|
|
layout.use_property_split = True
|
|
|
|
layout.label(text='End of cycle duplication')
|
|
|
|
layout.prop(self, 'end_frame', text='End Frame')
|
2022-04-25 18:12:24 +02:00
|
|
|
iteration = ((self.end_frame - self.first) // self.offset) + 1
|
|
|
|
real_end_cycle = iteration * self.offset + self.first
|
|
|
|
layout.label(text=f'Cycle will stop at frame: {real_end_cycle}')
|
2022-04-21 17:44:35 +02:00
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
def execute(self, context):
|
|
|
|
|
2022-04-21 18:42:38 +02:00
|
|
|
err = bake_cycle(context.scene.anim_cycle_settings.expand_on_selected_bones, end=self.end_frame)
|
2021-04-06 18:30:25 +02:00
|
|
|
if err:
|
|
|
|
self.report({err[0]}, err[1])
|
|
|
|
if err[0] == 'ERROR':
|
|
|
|
return {"CANCELLED"}
|
|
|
|
|
2022-04-11 19:56:38 +02:00
|
|
|
## all followup is not needed when animating on one
|
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
if not context.scene.anim_cycle_settings.linear:
|
|
|
|
# CHAINED ACTION : step the path of the curve path
|
|
|
|
err = step_path()
|
|
|
|
if err:
|
|
|
|
self.report({err[0]}, err[1])
|
|
|
|
if err[0] == 'ERROR':
|
|
|
|
return {"CANCELLED"}
|
|
|
|
else:
|
|
|
|
# Delete points in curve action between first and last and go LINEAR
|
2022-04-11 19:56:38 +02:00
|
|
|
# curve = context.scene.anim_cycle_settings.path_to_follow
|
|
|
|
# if curve:
|
|
|
|
# act = fn.get_obj_action(curve.data)
|
|
|
|
act = fn.get_obj_action(context.object)
|
|
|
|
if act:
|
|
|
|
timef = next((fc for fc in act.fcurves if fc.data_path.endswith('.offset') and 'constraint' in fc.data_path), None)
|
|
|
|
if timef:
|
|
|
|
keys_ct = len(timef.keyframe_points)
|
|
|
|
if keys_ct > 2:
|
|
|
|
for k in reversed(timef.keyframe_points[1:-2]):
|
|
|
|
timef.keyframe_points.remove(k)
|
|
|
|
for k in timef.keyframe_points:
|
|
|
|
k.interpolation = 'LINEAR'
|
|
|
|
print(f'Anim path to linear : Deleted all keys ({keys_ct - 2}) on anim path except first and last')
|
2022-04-01 12:12:05 +02:00
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
# CHAINED ACTION pin feet ?? : Step the path of the curve path
|
|
|
|
|
|
|
|
return {"FINISHED"}
|
|
|
|
|
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
# detect contact key
|
|
|
|
# [ 'array_index', 'auto_smoothing', 'bl_rna', 'color', 'color_mode', 'convert_to_keyframes', 'convert_to_samples', 'data_path',
|
|
|
|
# 'driver', 'evaluate', 'extrapolation', 'group', 'hide', 'is_empty', 'is_valid', 'keyframe_points', 'lock', 'modifiers', 'mute',
|
|
|
|
# 'range', 'rna_type', 'sampled_points', 'select', 'update', 'update_autoflags']
|
|
|
|
|
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
def pin_down_feets():
|
|
|
|
print(fn.helper())
|
|
|
|
obj = bpy.context.object
|
|
|
|
if obj.type != 'ARMATURE':
|
|
|
|
print('ERROR', 'active is not an armature type')
|
|
|
|
return
|
2022-04-08 19:35:20 +02:00
|
|
|
debug = fn.get_addon_prefs().debug
|
|
|
|
scn = bpy.context.scene
|
2021-04-06 18:30:25 +02:00
|
|
|
# Delete current action if its not the main one
|
2022-04-12 11:21:00 +02:00
|
|
|
# create a new '_pinned' one
|
2021-04-06 18:30:25 +02:00
|
|
|
act = fn.set_generated_action(obj)
|
|
|
|
if not act:
|
|
|
|
return ('ERROR', f'No action on {obj.name}')
|
2022-04-08 19:35:20 +02:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print('action', act.name)
|
2021-04-06 18:30:25 +02:00
|
|
|
|
|
|
|
act = obj.animation_data.action
|
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
to_change_list = [
|
2022-04-08 19:35:20 +02:00
|
|
|
(bpy.context.scene, 'frame_current'),
|
2022-04-13 18:38:03 +02:00
|
|
|
(bpy.context.scene, 'frame_start', bpy.context.scene.frame_start-100),
|
|
|
|
(bpy.context.scene, 'frame_end', bpy.context.scene.frame_end+100),
|
2022-04-07 14:36:47 +02:00
|
|
|
(bpy.context.scene.render, 'use_simplify', True),
|
|
|
|
(bpy.context.scene.render, 'simplify_subdivision', 0),
|
|
|
|
]
|
2021-04-08 19:25:05 +02:00
|
|
|
## Link armature in a new collection and exclude all the others
|
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
## STORE for context manager store/restore /-
|
|
|
|
tmp_col = bpy.data.collections.get('TMP_COLLECTION_PINNING')
|
|
|
|
if not tmp_col:
|
|
|
|
tmp_col = bpy.data.collections.new('TMP_COLLECTION_PINNING')
|
|
|
|
if tmp_col.name not in bpy.context.scene.collection.children:
|
|
|
|
bpy.context.scene.collection.children.link(tmp_col)
|
|
|
|
if obj not in tmp_col.objects[:]:
|
|
|
|
tmp_col.objects.link(obj)
|
2021-04-08 19:25:05 +02:00
|
|
|
|
|
|
|
for vlc in bpy.context.view_layer.layer_collection.children:
|
|
|
|
if vlc.collection == tmp_col:
|
|
|
|
continue
|
2022-04-07 14:36:47 +02:00
|
|
|
to_change_list.append((vlc, 'exclude', True))
|
|
|
|
|
2021-04-08 19:25:05 +02:00
|
|
|
#-/
|
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
with fn.attr_set(to_change_list):
|
|
|
|
t0 = time()
|
|
|
|
ct = 0
|
|
|
|
done = {}
|
2022-04-08 19:35:20 +02:00
|
|
|
viewed_data_paths = []
|
2022-04-07 14:36:47 +02:00
|
|
|
for fcu in act.fcurves:
|
2022-04-08 19:35:20 +02:00
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
# check only location
|
|
|
|
if not fcu.data_path.endswith('.location'):
|
|
|
|
continue
|
2022-04-08 19:35:20 +02:00
|
|
|
|
2022-04-13 19:34:12 +02:00
|
|
|
all_extremes = [k for k in fcu.keyframe_points if k.type == 'EXTREME']
|
|
|
|
if not all_extremes:
|
|
|
|
if debug: print(f'skip (no extremes marks): {fcu.data_path}')
|
|
|
|
continue
|
|
|
|
|
|
|
|
if len(all_extremes) == len(fcu.keyframe_points):
|
|
|
|
if debug: print(f'skip (only extremes): {fcu.data_path}')
|
|
|
|
continue
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-08 19:35:20 +02:00
|
|
|
# skip same data path with different array index to avoid multiple iteration
|
|
|
|
if fcu.data_path in viewed_data_paths:
|
2022-04-13 19:34:12 +02:00
|
|
|
if debug: print(f'skip (already evaluated): {fcu.data_path}')
|
2022-04-08 19:35:20 +02:00
|
|
|
continue
|
2022-04-13 19:34:12 +02:00
|
|
|
|
|
|
|
# TODO might need to filter the other array index (using a find)
|
|
|
|
# to do the action only on the one that has the best set of markers
|
|
|
|
# maybe check the wider values ? (meanning it's in direction of move)
|
2022-04-08 19:35:20 +02:00
|
|
|
|
|
|
|
viewed_data_paths.append(fcu.data_path)
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-08 19:35:20 +02:00
|
|
|
prop = fcu.data_path.split('.')[-1]
|
|
|
|
|
|
|
|
b_name = fcu.data_path.split('"')[1] # print('b_name: ', b_name, fcu.is_valid)
|
|
|
|
|
|
|
|
## TEST : only foot bones
|
|
|
|
if not 'foot' in b_name:
|
|
|
|
continue
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
pb = obj.pose.bones.get(b_name)
|
|
|
|
if not pb:
|
|
|
|
print(f'{b_name} is invalid')
|
|
|
|
continue
|
2022-04-08 19:35:20 +02:00
|
|
|
|
|
|
|
contact_ranges = []
|
2022-04-07 14:36:47 +02:00
|
|
|
start_contact = None
|
2022-04-08 19:35:20 +02:00
|
|
|
prev = None
|
2022-04-07 14:36:47 +02:00
|
|
|
for k in fcu.keyframe_points:
|
|
|
|
if k.type == 'EXTREME':
|
2022-04-08 19:35:20 +02:00
|
|
|
prev = k
|
2022-04-07 14:36:47 +02:00
|
|
|
if start_contact is None:
|
2022-04-08 19:35:20 +02:00
|
|
|
start_contact = int(k.co.x)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if start_contact is not None:
|
|
|
|
# print(f'contact range {start_contact} - {k.co.x:.0f}')
|
|
|
|
if prev:
|
|
|
|
contact_ranges.append((start_contact, int(prev.co.x)))
|
|
|
|
start_contact = None
|
|
|
|
prev = None
|
|
|
|
|
|
|
|
if not contact_ranges:
|
|
|
|
if debug >= 2: print(f'SKIP (no extreme keys): {b_name} > {prop}')
|
|
|
|
continue
|
|
|
|
|
|
|
|
if debug: print(f'fcurve: {b_name} > {prop}')
|
|
|
|
|
2022-04-13 18:38:03 +02:00
|
|
|
# iterate in reverse ranges (not really necessary)
|
|
|
|
for r in reversed(contact_ranges):
|
2022-04-08 19:35:20 +02:00
|
|
|
print(f'range: {r}')
|
|
|
|
first = True
|
|
|
|
for i in range(r[0], r[1]+1)[::-1]: # start from the end of the range
|
|
|
|
# for i in range(r[0], r[1]+1):
|
|
|
|
bpy.context.scene.frame_set(i)
|
|
|
|
if first:
|
2022-04-07 14:36:47 +02:00
|
|
|
# record coordinate relative to referent object (or world coord)
|
|
|
|
bone_mat = pb.matrix.copy()
|
|
|
|
# bone_mat = obj.matrix_world @ pb.matrix.copy()
|
2022-04-08 19:35:20 +02:00
|
|
|
first = False
|
2021-04-06 18:30:25 +02:00
|
|
|
continue
|
|
|
|
|
2022-04-08 19:35:20 +02:00
|
|
|
# print(f'Apply on {b_name} at {i}')
|
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
#-# assign previous matrix
|
2022-04-08 19:35:20 +02:00
|
|
|
# pbl = pb.location.copy()
|
2022-04-07 14:36:47 +02:00
|
|
|
|
|
|
|
pb.matrix = bone_mat # Exact same position
|
2022-04-08 19:35:20 +02:00
|
|
|
|
|
|
|
## maybe align on a specific axis
|
2022-04-07 14:36:47 +02:00
|
|
|
# pb.location.x = pbl.x # dont touch x either
|
2022-04-08 19:35:20 +02:00
|
|
|
|
|
|
|
#pb.location.z = pbl.z # Z is not necessarily up in local axis, need to check first
|
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
# pb.location.y = l.y (weirdly not working)
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
# bpy.context.view_layer.update()
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
#-# moyenne des 2 ?
|
|
|
|
# pb.location, pb.rotation_euler, pb.scale = average_two_matrix(pb.matrix, bone_mat) ## marche pas du tout !
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
## insert keyframe
|
|
|
|
pb.keyframe_insert('location')
|
2022-04-08 19:35:20 +02:00
|
|
|
|
2022-04-07 14:36:47 +02:00
|
|
|
# only touched Y location
|
2022-04-08 19:35:20 +02:00
|
|
|
pb.keyframe_insert('rotation_euler')
|
|
|
|
|
|
|
|
# if i == r[1]+1: # (last key) in normal
|
|
|
|
# if i == r[0]: # (last key) in reverse
|
|
|
|
# continue
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-08 19:35:20 +02:00
|
|
|
# k.type = 'JITTER' # 'BREAKDOWN' 'MOVING_HOLD' 'JITTER'
|
2022-04-07 14:36:47 +02:00
|
|
|
ct += 1
|
|
|
|
|
2022-04-08 19:35:20 +02:00
|
|
|
print(f'--\n{ct} keys changed/added in {time()-t0:.2f}s\n--') # fcurves treated in
|
2021-04-08 19:25:05 +02:00
|
|
|
|
|
|
|
## RESTORE
|
|
|
|
# without >> 433 keys changed in 29.15s
|
|
|
|
# with all collection excluded >> 433 keys changed in 25.00s
|
2022-04-08 19:35:20 +02:00
|
|
|
# with simplify set to 0 >> 9.57s
|
2021-04-08 19:25:05 +02:00
|
|
|
|
|
|
|
tmp_col.objects.unlink(obj)
|
|
|
|
bpy.data.collections.remove(tmp_col)
|
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
|
2022-04-20 12:02:19 +02:00
|
|
|
class AW_OT_pin_feets(bpy.types.Operator):
|
|
|
|
bl_idname = "autowalk.pin_feets"
|
2021-04-06 18:30:25 +02:00
|
|
|
bl_label = "Pin Feets"
|
2022-04-13 18:38:03 +02:00
|
|
|
bl_description = "Pin feets on keys marked as extreme\n(duplicate to a new 'pinned' action)"
|
2021-04-06 18:30:25 +02:00
|
|
|
bl_options = {"REGISTER", "UNDO"}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return context.object and context.object.type == 'ARMATURE'
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
# context.scene.anim_cycle_settings.expand_on_selected_bones
|
|
|
|
err = pin_down_feets()
|
|
|
|
if err:
|
|
|
|
self.report({err[0]}, err[1])
|
|
|
|
if err[0] == 'ERROR':
|
|
|
|
return {"CANCELLED"}
|
|
|
|
|
|
|
|
return {"FINISHED"}
|
|
|
|
|
2022-04-12 19:24:47 +02:00
|
|
|
|
2022-04-13 18:38:03 +02:00
|
|
|
# --- Quick action management
|
|
|
|
|
2022-04-20 12:02:19 +02:00
|
|
|
class AW_OT_set_action(bpy.types.Operator):
|
|
|
|
bl_idname = "autowalk.set_action"
|
2022-04-12 19:24:47 +02:00
|
|
|
bl_label = "Set action by name"
|
|
|
|
bl_description = "Set action on active object using passed name"
|
|
|
|
bl_options = {"REGISTER", "INTERNAL"}
|
|
|
|
|
|
|
|
act_name : bpy.props.StringProperty(options={'SKIP_SAVE'})
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
act = bpy.data.actions.get(self.act_name)
|
|
|
|
if not act:
|
|
|
|
self.report({'ERROR'}, f'Could not find action {self.act_name} in bpy.data.actions')
|
|
|
|
return {"CANCELLED"}
|
|
|
|
|
|
|
|
context.object.animation_data.action = act
|
|
|
|
return {"FINISHED"}
|
|
|
|
|
2022-04-20 12:02:19 +02:00
|
|
|
class AW_OT_step_back_actions(bpy.types.Operator):
|
|
|
|
bl_idname = "autowalk.step_back_actions"
|
2022-04-12 19:24:47 +02:00
|
|
|
bl_label = "Actions Step Back"
|
2022-04-13 18:38:03 +02:00
|
|
|
bl_description = "Step back to a previous action if 'baked' or 'pinned' action are not ok"
|
2022-04-12 19:24:47 +02:00
|
|
|
bl_options = {"REGISTER", "INTERNAL", "UNDO"}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return context.object and context.object.type == 'ARMATURE'
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
2022-04-13 18:38:03 +02:00
|
|
|
if context.object.animation_data.use_tweak_mode:
|
|
|
|
self.report({'ERROR'}, f'Cannot access animation in NLA')
|
|
|
|
return {"CANCELLED"}
|
|
|
|
|
2022-04-12 19:24:47 +02:00
|
|
|
act = context.object.animation_data.action
|
|
|
|
base_name = act.name.replace('_baked', '').replace('_pinned', '')
|
|
|
|
base_name = re.sub(r'\.\d{3}', '', base_name) # remove duplicate to search everything that has the same base
|
|
|
|
|
|
|
|
# self.actions = [a for a in bpy.data.actions if a.name.startswith(base_name) and not a.name == act.name] # skip current action
|
|
|
|
self.actions = [a for a in bpy.data.actions if a.name.startswith(base_name)]
|
|
|
|
|
|
|
|
if not len(self.actions):
|
|
|
|
self.report({'ERROR'}, f'no other action found for {act.name}\nUsing basename{base_name}')
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
if len(self.actions) == 1:
|
|
|
|
context.object.animation_data.action = self.actions[0]
|
|
|
|
return self.execute(context)
|
|
|
|
|
|
|
|
self.actions.sort(key=lambda x: len(x.name))
|
|
|
|
return context.window_manager.invoke_props_popup(self, event)
|
|
|
|
|
|
|
|
|
|
|
|
def draw(self, context):
|
|
|
|
layout = self.layout
|
|
|
|
# layout.label(text=f'Current Action: {context.object.animation_data.action.name}')
|
|
|
|
layout.label(text='Actions with same name base:')
|
|
|
|
for a in self.actions:
|
|
|
|
if a == context.object.animation_data.action:
|
|
|
|
layout.label(text=f'(current) >> {a.name}', icon='ACTION')
|
|
|
|
continue
|
2022-04-21 14:25:43 +02:00
|
|
|
layout.operator('autowalk.set_action', text=a.name, icon='ACTION').act_name = a.name
|
2022-04-12 19:24:47 +02:00
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
return {"FINISHED"}
|
|
|
|
|
|
|
|
|
2021-04-06 18:30:25 +02:00
|
|
|
classes=(
|
2022-04-20 12:02:19 +02:00
|
|
|
AW_OT_bake_cycle_and_step,
|
|
|
|
AW_OT_pin_feets,
|
|
|
|
AW_OT_set_action,
|
|
|
|
AW_OT_step_back_actions,
|
2021-04-06 18:30:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def register():
|
|
|
|
for cls in classes:
|
|
|
|
bpy.utils.register_class(cls)
|
|
|
|
|
|
|
|
def unregister():
|
|
|
|
for cls in reversed(classes):
|
|
|
|
bpy.utils.unregister_class(cls)
|