Timeline Manipulation with Python

Timeline Manipulation with Python

Create move edit Block and Keyframe inside Timeline

When you parse the Timeline Parsing with Python List all element Block and Key inside the timeline Read More you can use the function .isChildOf() to test if a track or parameter is linked to the object you want

Example: Block Replication

In the script below we first have a layer selector, then we have a function (that is used later) that help find the last block in a track and then we have the parsing of the timeline where we use elt.isChildOf() to know the track that correspond to the selection
Once we have the correct track we do some manipulation of the block ( cloning, finding the last one, moving the clone and then adding it to the track)
BlockMargin: Oil.Seconds(1)
layer: Oil.createObject("WeakPointer(Layer)")


t = script.parentElement.mainAnimation

layer = script.layer.get() #script.parentElement.layers["test"]

def getLastBlock(blocks): # helper function to find the last block
    position = 0
    for block in blocks:
        if block.position.get() >= position:
            lastBlock = block
            position = block.position.get()
    return lastBlock

for element, track in t.elementTracks.items(): #parsing of the timeline
    elt = element.get()
    
    if elt.isChildOf(layer): #finding the right element and 
        blocks = track.blocks
        newBlock = blocks[0].clone()
    
        lastOne = getLastBlock(blocks)
        newBlock.position.set(lastOne.position.get()+lastOne.length.get() +  script.BlockMargin.get())
        blocks.append(newBlock)

Example: Keyframes Replication

Here we have a similar script that above for the block but now we add new keyframe after the last one and change it's value with a random one
BlockMargin: Oil.Seconds(1)
layer: Oil.createObject("WeakPointer(Layer)")

from random import random

t = script.parentElement.mainAnimation

layer = script.layer.get() #script.parentElement.layers["test"]

def getLastKey(keys): # helper function to find the last block
    position = 0
    for key in keys:
        if key.position.get() >= position:
            lastKey = key
            position = key.position.get()
    return lastKey



for object, track in t.parameterTracks.items():
    parameter = object.get()
    
    if parameter.isChildOf(layer): #finding the right element
        print(f"Parameter {parameter.getFriendlyName()}")
        f = track.function
        newKey = f.keyframes[0].clone()


        lastKey = getLastKey(f.keyframes)


        newKey.position.set(lastKey.position.get() + script.BlockMargin.get())
        newKey.key.set(random()) # setting the key value

        f.keyframes.append(newKey)

Example: Using Makers

Here is a example to create blocks for every makers in a timeline
layer: Oil.createObject("WeakPointer(Layer)")

t = script.parentElement.mainAnimation

layer = script.layer.get() #script.parentElement.layers["test"]

print(layer.label)


def isAlreadyaBlock(time):
    for block in blocks:
        if block.position.get() == time:
            return 1
    return 0

for element, track in t.elementTracks.items(): #parsing of the timeline
    elt = element.get()
    
    if elt.isChildOf(layer): #finding the right element
        blocks = track.blocks

        for marker in t.timeMarkers:
            if isAlreadyaBlock(marker.position.get()):
                continue
            newBlock = blocks[0].clone()
            
            newBlock.position.set(marker.position)
            blocks.append(newBlock)
            print("New Block Added")

See Also: