[英]Loop only saves last iteration
I am using python in the context of blender 's api so it could be that this problem is particular to that api (am asking in that forum too) but I think I am making a more generic python error so I thought it might be worth asking in this context. 我在Blender的api上下文中使用python,所以可能这个问题是该api特有的(我也在该论坛中问过),但是我认为我在犯一个更通用的python错误,因此我认为这可能是值得的在这种情况下问。 Please note I am a complete beginner to python.
请注意,我是python的完整入门者。
I am writing a fairly simple script that loops through every frame of a scene copying and translating some animation data from type to another (armature rig -> shapekeys). 我正在编写一个相当简单的脚本,该脚本循环遍历场景的每一帧,并将某些动画数据从类型转换为另一种(电枢装置-> shapekey)。 I then go through each frame and try to set the new animation data as a keyframe value of 1, and set all other keys to 0.
然后,我遍历每一帧,尝试将新的动画数据设置为关键帧值1,并将所有其他关键点设置为0。
To abstract the problem away from this particular issue, for eg 13 frames in a scene, each frame has 13 possible animation states, and for each scene 1 of those animation states should be set to a value of 1 while all the others should be set to 0. The problem I am having is that rather than each frame having its corresponding animation state set to 1, only the last animation state is set to 1 regardless of the frame. 为了使问题远离此特定问题,例如对于一个场景中的13帧,每个帧具有13种可能的动画状态,并且对于每个场景,这些动画状态中的1个应设置为值1,而所有其他状态应设置为设置为0。我遇到的问题是,与其将相应的动画状态设置为1,而不是将每个帧的最后一个动画状态设置为1,而不是每个帧。
Please see my code below. 请在下面查看我的代码。 I include all for reference but the first section works fine, it is the 2nd section where the issue exists.
我提供所有内容供参考,但是第一部分工作正常,这是存在问题的第二部分。
#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)
What I expect to happen is that for each frame, the corresponding shapekey will have a keyframed value of 1 and all the others have 0. 我希望发生的是,对于每个帧,对应的shapekey的关键帧值将为1,而所有其他的key值将为0。
So: 所以:
for frame 0, 'Armature' shapekey has value 1, all others have 0 对于第0帧,“ Armature” shapekey的值为1,其他所有值为0
for frame 1, 'Armature.001' shapekey has value 1, all others have 0 对于第1帧,“ Armature.001” shapekey的值为1,其他所有值为0
for frame 2, 'Armature.002' shapekey has value 1, all others have 0 对于第2帧,“ Armature.002” shapekey的值为1,其他所有值为0
But instead for all frames only 'Armature.013' shapekey has value 1 and all others are set to 0. 但是,对于所有帧,只有'Armature.013'shapekey的值为1,所有其他帧的值都设置为0。
For this reason I think I am making a generic error, where somehow each loop is overwriting the last hence why only the last iteration shows. 因此,我认为我正在犯一个通用错误,其中每个循环都以某种方式覆盖了最后一个循环,因此为什么只显示最后一个迭代。
I hope I have explained the issue clearly enough. 我希望我已经足够清楚地解释了这个问题。 Here is my question in the blender forum where a sample file is included if that helps at all.
这是我在Blender论坛中的问题,如果有帮助的话,其中包括示例文件。
Answering my own question as I figured it out. 我想出了自己的问题。 Just in case anyone else has a need for it, the below script works as expected.
万一其他人有需要,下面的脚本将按预期工作。
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.