循环只保存最后一次迭代
Loop only saves last iteration
我在 blender's api 的上下文中使用 python 所以这个问题可能是那个 api 特有的(我也在那个论坛上问过)但我想我我正在犯一个更通用的 python 错误,所以我认为在这种情况下可能值得一问。请注意,我是 python.
的初学者
我正在编写一个相当简单的脚本,它循环遍历场景的每一帧,将一些动画数据从类型复制和转换为另一种类型(电枢装备 -> shapekeys)。然后我遍历每一帧并尝试将新的动画数据设置为关键帧值 1,并将所有其他键设置为 0。
将问题从这个特定问题中抽象出来,例如场景中有 13 帧,每帧有 13 种可能的动画状态,对于每个场景,这些动画状态中的 1 应该设置为值 1,而所有其他的应该设置为 0。我遇到的问题是,而不是每帧都将其相应的动画状态设置为 1,无论帧如何,只有最后一个动画状态设置为 1。
请看下面我的代码。我包括所有内容以供参考,但第一部分工作正常,这是存在问题的第二部分。
#blender import
import bpy
#save the total number of frames as var
frames = bpy.context.scene.frame_end + 1
#loop through frames, jump to each frame, add the armature, set as shapekey
for frame in range(frames):
bpy.context.scene.frame_set(frame)
bpy.ops.object.modifier_add(type='ARMATURE')
bpy.context.object.modifiers["Armature"].object = bpy.data.objects["rig"]
bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier="Armature")
#loop through shapekeys and add as keyframe per frame, this is where the issue is.
for shapekey in bpy.data.shape_keys:
for i, keyblock in enumerate(shapekey.key_blocks):
if keyblock.name != 'Basis':
curr = i - 1
if curr != frame:
keyblock.value = 0
keyblock.keyframe_insert("value",frame=curr)
else:
keyblock.value = 1
keyblock.keyframe_insert("value",frame=curr)
我希望发生的是,对于每一帧,对应的形状键的关键帧值为 1,而所有其他帧的关键帧值为 0。
所以:
对于第 0 帧,'Armature' shapekey 值为 1,所有其他值为 0
对于第 1 帧,'Armature.001' shapekey 值为 1,所有其他值为 0
对于第 2 帧,'Armature.002' shapekey 值为 1,所有其他值为 0
但仅针对所有帧 'Armature.013' shapekey 的值为 1,所有其他帧都设置为 0。
出于这个原因,我认为我犯了一个一般性错误,其中每个循环都以某种方式覆盖了最后一个循环,因此为什么只显示最后一个迭代。
我希望我已经足够清楚地解释了这个问题。 Here 是我在 blender 论坛上的问题,如果有帮助的话,其中包含一个示例文件。
回答我自己想出来的问题。以防万一其他人需要它,下面的脚本可以按预期工作。
import bpy
#save the total number of frames as var
frames = bpy.context.scene.frame_end + 1
#loop through frames, jump to each frame, add the armature, set as shapekey
for frame in range(frames):
bpy.context.scene.frame_set(frame)
bpy.ops.object.modifier_add(type='ARMATURE')
bpy.context.object.modifiers["Armature"].object = bpy.data.objects["rig"]
bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier="Armature")
#for each frame, loop through shapekeys and add as keyframe per frame, set value to 1 if current frame = corresponding shapekey
for frame in range(frames):
for shapekey in bpy.data.shape_keys:
for i, keyblock in enumerate(shapekey.key_blocks):
if keyblock.name != 'Basis':
curr = i - 1
if curr != frame:
keyblock.value = 0
keyblock.keyframe_insert("value", frame=frame)
else:
keyblock.value = 1
keyblock.keyframe_insert("value", frame=frame)
我在 blender's api 的上下文中使用 python 所以这个问题可能是那个 api 特有的(我也在那个论坛上问过)但我想我我正在犯一个更通用的 python 错误,所以我认为在这种情况下可能值得一问。请注意,我是 python.
的初学者我正在编写一个相当简单的脚本,它循环遍历场景的每一帧,将一些动画数据从类型复制和转换为另一种类型(电枢装备 -> shapekeys)。然后我遍历每一帧并尝试将新的动画数据设置为关键帧值 1,并将所有其他键设置为 0。
将问题从这个特定问题中抽象出来,例如场景中有 13 帧,每帧有 13 种可能的动画状态,对于每个场景,这些动画状态中的 1 应该设置为值 1,而所有其他的应该设置为 0。我遇到的问题是,而不是每帧都将其相应的动画状态设置为 1,无论帧如何,只有最后一个动画状态设置为 1。
请看下面我的代码。我包括所有内容以供参考,但第一部分工作正常,这是存在问题的第二部分。
#blender import
import bpy
#save the total number of frames as var
frames = bpy.context.scene.frame_end + 1
#loop through frames, jump to each frame, add the armature, set as shapekey
for frame in range(frames):
bpy.context.scene.frame_set(frame)
bpy.ops.object.modifier_add(type='ARMATURE')
bpy.context.object.modifiers["Armature"].object = bpy.data.objects["rig"]
bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier="Armature")
#loop through shapekeys and add as keyframe per frame, this is where the issue is.
for shapekey in bpy.data.shape_keys:
for i, keyblock in enumerate(shapekey.key_blocks):
if keyblock.name != 'Basis':
curr = i - 1
if curr != frame:
keyblock.value = 0
keyblock.keyframe_insert("value",frame=curr)
else:
keyblock.value = 1
keyblock.keyframe_insert("value",frame=curr)
我希望发生的是,对于每一帧,对应的形状键的关键帧值为 1,而所有其他帧的关键帧值为 0。
所以:
对于第 0 帧,'Armature' shapekey 值为 1,所有其他值为 0
对于第 1 帧,'Armature.001' shapekey 值为 1,所有其他值为 0
对于第 2 帧,'Armature.002' shapekey 值为 1,所有其他值为 0
但仅针对所有帧 'Armature.013' shapekey 的值为 1,所有其他帧都设置为 0。
出于这个原因,我认为我犯了一个一般性错误,其中每个循环都以某种方式覆盖了最后一个循环,因此为什么只显示最后一个迭代。
我希望我已经足够清楚地解释了这个问题。 Here 是我在 blender 论坛上的问题,如果有帮助的话,其中包含一个示例文件。
回答我自己想出来的问题。以防万一其他人需要它,下面的脚本可以按预期工作。
import bpy
#save the total number of frames as var
frames = bpy.context.scene.frame_end + 1
#loop through frames, jump to each frame, add the armature, set as shapekey
for frame in range(frames):
bpy.context.scene.frame_set(frame)
bpy.ops.object.modifier_add(type='ARMATURE')
bpy.context.object.modifiers["Armature"].object = bpy.data.objects["rig"]
bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier="Armature")
#for each frame, loop through shapekeys and add as keyframe per frame, set value to 1 if current frame = corresponding shapekey
for frame in range(frames):
for shapekey in bpy.data.shape_keys:
for i, keyblock in enumerate(shapekey.key_blocks):
if keyblock.name != 'Basis':
curr = i - 1
if curr != frame:
keyblock.value = 0
keyblock.keyframe_insert("value", frame=frame)
else:
keyblock.value = 1
keyblock.keyframe_insert("value", frame=frame)