如何使用Python在Blender 2.78a中制作关键帧?

3
我刚开始学习Blender和Python,在我的第一层中有一个名为"BallB"的球体。
现在我想使用Python在Blender中制作一个简单的冒泡动画,但我无法制作关键帧。这应该发生在第二层。
我尝试了很多次,但出现了许多错误...我找到的所有代码片段都不起作用,并且我的脚本总是崩溃并显示Python错误,例如
RuntimeError: Operator bpy.ops.anim.change ... expected an timeline/animation area to be activated
等等。
有人能给我一些提示吗?
我想学习在Blender中编写动画脚本,因此非常感谢每一个可以帮助我的提示;-)
我的代码:
import bpy, math, random

d           = 4
anz         = 100
frameAnz    = 10

scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 100


for anz in range (0,anz):

    ball = bpy.data.objects["ballB"]   

    tball = ball.copy()
    xpos = -1 * (d/2) + random.randint(0,(d-1))
    xpos += random.random()
    ypos = -1 * (d/2) + random.randint(0,(d-1))
    ypos += random.random()
    zpos =  random.randint(0,(d-1))
    zpos += random.random()



    bn = str(anz).zfill(5)
    bn = "zz_Ball-" + bn

    tball.name = bn
    tball.location = (xpos, ypos, zpos)
    sz = random.uniform(0.015,0.09)


    tball.scale = (sz,sz,sz)

    #tball.nodes["Emission"].inputs[1].default_value = 200
    tball.select = False
    scene.objects.link(tball)
    #print ("done!")

obj = bpy.context

for actFrame in range(1,frameAnz):
   # scene = bpy.context.scene
#    scene.keyframe_insert(data_path="gravity", frame = actFrame)


    for ob in scene.objects:

        ploc = ob.location
        #print (ploc)
        xpos = ploc[0]
        ypos = ploc[1]
        zpos = ploc[2]

        zpos = zpos + random.random()
        ob.location = (xpos, ypos, zpos)
        #ypos = ball.location[1]
        #zpos = ball.location]2]

        #zpos = zpos - random.random()

        #ball.location = (xpoy, ypos, zpos)
        #obj.keyframe_insert_menu('Location')
        #bpy.context.scene.frame_set(0)
    #scene = bpy.context.scene
    #scene.keyframe_insert(data_path="Location", frame=actFrame)

实际上它看起来是这样的:

enter image description here

1个回答

2
你离成功不远了,你需要使用 obj.keyframe_insert(),使用索引参数可以仅关键帧化一个位置值。
但是,你会遇到一个问题,即复制初始对象意味着新对象将使用相同的动画数据,使它们一起移动。你可以创建一个新对象并使用相同的网格数据。
对象的 layers 属性是一个包含20个布尔值的数组,用于指定其可见性。当你将对象添加到场景中时,它将被设置为在活动层上可见,因此在将其链接到场景后设置此属性。
import bpy, random

d           = 4
anz         = 100
frameAnz    = 20
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 100
ball = bpy.data.objects["ballB"]

for anz in range (0,anz):
    xpos = -1 * (d/2) + random.randint(0,(d-1))
    xpos += random.random()
    ypos = -1 * (d/2) + random.randint(0,(d-1))
    ypos += random.random()
    zpos =  random.randint(0,(d-1))
    zpos += random.random()

    bn = str(anz).zfill(5)
    bn = "zz_Ball-" + bn

    tball = bpy.data.objects.new(bn, ball.data)
    tball.location = (xpos, ypos, zpos)
    sz = random.uniform(0.015,0.09)
    tball.scale = (sz,sz,sz)

    tball.select = False
    scene.objects.link(tball)
    tball.layers = [False,True] + [False]*18

for actFrame in range(1,frameAnz):
    for ob in scene.objects:
        ob.location.z += random.random()
        ob.keyframe_insert(data_path='location', index=2, frame=actFrame)

谢谢,@Sambler - 这增加了我对Blender中动画的理解。 昨天我找到了我的第一个解决方案: http://blender.stackexchange.com/questions/70456/how-to-make-keyframes-in-blender-2-78a-using-python/70478#70478我相信这不会是我关于迷人的Blender和Python的最后一个问题;-) - Atari800XL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接