生成动画有两种方法,一种是直接调用object.keyframe_insert(),一种是生成一个fcurve来控制动画,贴代码
# 方法1先设置属性再绑定动画
obj = bpy.data.objects['Cube']
obj.location = (0.0, 0.0, 0.0)
# obj.keyframe_insert(data_path="location", frame=context.scene.frame_start)没有index表示绑定xyz三个属性,index=2表示绑定z轴
obj.keyframe_insert(data_path="location", index=2, frame=context.scene.frame_start)
obj.location = (0.0, 0.0, 10.0)
obj.keyframe_insert(data_path="location", index=2, frame=context.scene.frame_end)
# 方法2需要创建fcurve
obj.animation_data_create()
action = bpy.data.actions.new(name="CubeAction")
obj.animation_data.action = action
# f = action.fcurves.new(data_path="location")没有index表示绑定xyz三个属性,index=2表示绑定z轴
f = action.fcurves.new(data_path="location", index=2)
keyframe_points = f.keyframe_points
k1 = keyframe_points.insert(
frame=context.scene.frame_start,
value=0
)
k1.interpolation = "LINEAR"
k2 = keyframe_points.insert(
frame=context.scene.frame_end,
value=10
)
k2.interpolation = "LINEAR"