[英]Loop only saves last iteration
我在Blender的api上下文中使用python,所以可能这个问题是该api特有的(我也在该论坛中问过),但是我认为我在犯一个更通用的python错误,因此我认为这可能是值得的在这种情况下问。 请注意,我是python的完整入门者。
我正在编写一个相当简单的脚本,该脚本循环遍历场景的每一帧,并将某些动画数据从类型转换为另一种(电枢装置-> shapekey)。 然后,我遍历每一帧,尝试将新的动画数据设置为关键帧值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)
我希望发生的是,对于每个帧,对应的shapekey的关键帧值将为1,而所有其他的key值将为0。
所以:
对于第0帧,“ Armature” shapekey的值为1,其他所有值为0
对于第1帧,“ Armature.001” shapekey的值为1,其他所有值为0
对于第2帧,“ Armature.002” shapekey的值为1,其他所有值为0
但是,对于所有帧,只有'Armature.013'shapekey的值为1,所有其他帧的值都设置为0。
因此,我认为我正在犯一个通用错误,其中每个循环都以某种方式覆盖了最后一个循环,因此为什么只显示最后一个迭代。
我希望我已经足够清楚地解释了这个问题。 这是我在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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.