繁体   English   中英

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

[英]How to make keyframes in Blender 2.78a using Python?

我是Blender的新手,还是Python的新手,在我的第1层上,有一个名为“ BallB”的球。

现在,我想在Blender中使用Python进行简单的冒泡动画,但无法创建关键帧。 这应该在第2层上发生。

我尝试了很多并且遇到了很多错误...我发现的所有代码片段均无法正常运行,并且脚本始终因Python-Errors崩溃

RuntimeError:操作员bpy.ops.anim.change ...预计将激活时间线/动画区域

还有很多。

有人对我有提示吗?

我想在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)

实际上看起来是这样的:

在此处输入图片说明

亲近了,您想使用obj.keyframe_insert() ,使用index参数可以仅对一个位置值进行关键帧设置。

您将遇到的一个问题是,复制初始对象意味着新对象将使用相同的动画数据,并使它们一致移动。 您可以创建一个新对象并使用相同的网格数据。

对象图层属性是一个由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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM