blender 2.80 experiment 7 - keyframes

Nikolaus Gradwohl2019-01-14T05:20:22+00:00

This blender experiment is based on the curve generation python script from my erlier post - Ths time I added keyframes to the locations of the bezier curve vertices and their handles.

Keyframing a curve vertex from a python script is pretty simple. All that is required is calling the keyframe_insert method on the point object and specifing the path to the attribute that should be keyframed.

For example

b.bezier_points[i].co = (1,1,1)
    b.bezier_points[i].keyframe_insert(data_path='co')

inserts a new keyframe for the position of a curve vertex at the current frame of the scene.

to change the frame i used

bpy.context.scene.frame_set(framenumber)

you can download the blend file here

keyframed curve vertices

Here ist the complete source of the python script I used to generate this curve:

import bpy
from math import pi,cos,sin
import random

cd = bpy.data.curves.new(name='Curve', type='CURVE')
cd.dimensions='3D'

b = cd.splines.new('BEZIER')

for i in range(60):
    a =  i/10.0 * 2.0*pi

    r2 = random.uniform(0.1,3)
    x = cos(a)*r2
    y = sin(a)*r2
    if i > 0:
        b.bezier_points.add(1)
        b.bezier_points[-1].co = (x,y,i/5)

for i in range(60):
    co1 = (0,0,0)    
    if i>0:
        co1 =   b.bezier_points[i-1].co  

    co2 = (0,0,12)
    if i<59:
        co2 =   b.bezier_points[i+1].co  
    co =   b.bezier_points[i].co  
    a = (co2[0] -co1[0],
         co2[1] -co1[1],
         co2[2] -co1[2])

    b.bezier_points[i].handle_left = (
        co[0] - a[0]/6.0,
        co[1] - a[1]/6.0,
        co[2] - a[2]/6.0)
    b.bezier_points[i].handle_right = (
        co[0] + a[0]/6.0,
        co[1] + a[1]/6.0,
        co[2] + a[2]/6.0)

for t in range(5):          
    bpy.context.scene.frame_set(t*50)
    for i in range(60):        
        a =  i/10.0 * 2.0*pi
        r2 = random.uniform(0.1,3)
        x = cos(a)*r2
        y = sin(a)*r2
        b.bezier_points[i].co = (x,y,i/5)                        
        b.bezier_points[i].keyframe_insert(data_path='co')


    for i in range(60):
        co1 = (0,0,0)    
        if i>0:
            co1 =   b.bezier_points[i-1].co  

        co2 = (0,0,12)
        if i<59:
            co2 =   b.bezier_points[i+1].co  
        co =   b.bezier_points[i].co  
        a = (co2[0] -co1[0],
             co2[1] -co1[1],
             co2[2] -co1[2])

        b.bezier_points[i].handle_left = (
            co[0] - a[0]/6.0,
            co[1] - a[1]/6.0,
            co[2] - a[2]/6.0)
        b.bezier_points[i].handle_right = (
            co[0] + a[0]/6.0,
            co[1] + a[1]/6.0,
            co[2] + a[2]/6.0)
        b.bezier_points[i].keyframe_insert(data_path='handle_left')
        b.bezier_points[i].keyframe_insert(data_path='handle_right')

od = bpy.data.objects.new('CurveObject', cd)
od.location=(0,0,0)
od.data.resolution_u=12
od.data.bevel_resolution=12
od.data.fill_mode = 'FULL'
od.data.bevel_depth=0.1

bpy.context.scene.collection.objects.link(od)
Tweet This! submit to reddit Digg! Tags: | no comments | no trackbacks

See also:

blender 2.80 experiment 5 - scripted curve
Animation Node experiment - closed curve
Animation Node experiment - script node spiral
denoising blender animations with opencv and python
animation nodes knot

Trackbacks

Comments

Leave a response

Leave a comment