簡體   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