【问题标题】:How to make keyframes in Blender 2.78a using Python?如何使用 Python 在 Blender 2.78a 中制作关键帧?
【发布时间】:2017-01-04 06:25:33
【问题描述】:

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

现在我想在 Blender 中使用 Python 制作一个简单的冒泡动画,但我无法制作关键帧。这应该发生在第 2 层。

我尝试了很多,但遇到了很多错误......我发现的所有片段都不起作用,我的脚本总是因 Python 错误而崩溃,例如

RuntimeError: Operator 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)

其实是这样的:

【问题讨论】:

    标签: python blender


    【解决方案1】:

    你很接近,你想使用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 中动画的理解。昨天我找到了我的第一个解决方案:blender.stackexchange.com/questions/70456/… 我相信这不会是我关于迷人的 Blender 和 Python 的最后一个问题 ;-)
    猜你喜欢
    • 2019-10-18
    • 2016-04-16
    • 2011-11-25
    • 1970-01-01
    • 2015-09-14
    • 2020-01-06
    • 2012-02-09
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多