Blender の PythonAPI を勉強中です。
今回は、Sphereが螺旋状に上にくるくる回りながら上がっていくアニメーションを作成しました。
以前作成した螺旋状にCubeを並べるものをベースにしています。
sinとcosを使う
sin,cosを使うのでmathをインポートします。
import math
positionsリストに、sinとcosを使って螺旋状に位置を並べていきます。
for d in range(300): positions.append((math.cos(math.radians(d)*10)*r, math.sin(math.radians(d)*10)*r, d*0.1))
並べたものをkeyframeに1フレームごとにインサートします。
for p in positions: bpy.context.scene.frame_set(frame) obj.location = p obj.keyframe_insert(data_path="location") frame += 1
コードの全体はこちら↓
import bpy import math r = 5 def delete_all(): for i in bpy.data.meshes: bpy.data.meshes.remove(i) positions = [] def make_animation(): for d in range(300): positions.append((math.cos(math.radians(d)*10)*r, math.sin(math.radians(d)*10)*r, d*0.1)) bpy.ops.mesh.primitive_uv_sphere_add() for o in bpy.data.objects: if o.type == "MESH": obj = o obj.animation_data_clear() frame = 0 for p in positions: bpy.context.scene.frame_set(frame) obj.location = p obj.keyframe_insert(data_path="location") frame += 1 bpy.context.scene.frame_set(0) if __name__ == '__main__': delete_all() make_animation()
動かしてみます。
一応思い通りに動きました。