-
-
Save pgtwitter/32c30f941f648295e33582f79781edc7 to your computer and use it in GitHub Desktop.
停止点のあるsin(cos)を用いたモーションをつけるコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import bpy | |
import numpy as np | |
def modified_sin(frames, target_fs, delta, amp, freq, shift, fn): | |
hold_fs = [f for fs in [list(range(v-delta, v+delta)) for v in target_fs] for f in fs] | |
cycle = frames - len(hold_fs) | |
ny = [amp*fn(freq*2*np.pi/cycle*t)-shift for t in range(cycle)] | |
result = [] | |
ni = 0 | |
for i in range(frames): | |
result.append(ny[ni]) | |
ni = ni+1 if i not in hold_fs else ni | |
return result | |
def setArmature(target_fs): | |
obj = bpy.data.objects['Armature'] | |
bone = obj.pose.bones['Handle'] | |
xs = modified_sin(251, target_fs, 13, 3, 1, 0, np.cos) | |
zs = modified_sin(251, target_fs, 13, 3, 2, 3, np.sin) | |
ys = [3.5*np.sin(6*np.pi/251*t)-10.0 for t in range(251)] | |
zeroLocations = [] | |
nys = [] | |
for t in range(251): | |
x = xs[t] | |
z = zs[t] | |
y = ys[t] | |
y = -7-.2 if y > -7-.2 else y | |
nys.append(y) | |
if t in target_fs: | |
zeroLocations.append((t, (x, y+7+.6, 3-z))) | |
bone.location = [x, y, z] | |
bone.keyframe_insert(data_path='location', frame=t) | |
return zeroLocations | |
def setCubes(zeroFrameAndLocation): | |
ys = [3.5*np.sin(6*np.pi/251*t)-10.0 for t in range(251)] | |
ys = [-7-.2 if y > -7-.2 else y for y in ys] | |
for i, (frame, location) in enumerate(zeroFrameAndLocation): | |
obj = bpy.data.objects[f'Cube.{i+1:03d}'] | |
obj.rigid_body.enabled = False | |
obj.keyframe_insert(data_path='rigid_body.enabled', frame=1) | |
obj.rigid_body.kinematic = True | |
obj.keyframe_insert(data_path='rigid_body.kinematic', frame=1) | |
obj.location = (location[0], location[1]-2.01, location[2]) | |
obj.keyframe_insert(data_path='location', frame=1) | |
obj.location = (location[0], location[1]-.5, location[2]) | |
obj.keyframe_insert(data_path='location', frame=5) | |
obj.keyframe_insert(data_path='location', frame=frame+5) | |
for j in range(6): | |
f = frame+5+j+1 | |
obj.location = (location[0], -7-ys[f]-.25, location[2]) | |
obj.keyframe_insert(data_path='location', frame=f) | |
obj.location = location | |
obj.keyframe_insert(data_path='location', frame=(frame+12)) | |
obj.keyframe_insert(data_path='location', frame=(frame+13)) | |
obj.keyframe_insert(data_path='location', frame=(frame+14)) | |
obj.rigid_body.enabled = True | |
obj.keyframe_insert(data_path='rigid_body.enabled', frame=(frame+14)) | |
obj.rigid_body.kinematic = False | |
obj.keyframe_insert(data_path='rigid_body.kinematic', frame=(frame+14)) | |
for action in bpy.data.actions: | |
bpy.data.actions.remove(action) | |
target_fs = [21, 105, 189] | |
setCubes(setArmature(target_fs)) | |
for action in bpy.data.actions: | |
for f in action.fcurves: | |
for k in f.keyframe_points: | |
k.interpolation = 'LINEAR' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/shorts/Q0HRmZw6JlA