1
$\begingroup$

here my first ever attempt to patch a simple python script for Blender 2.9 that could import a series of of svg files that conform an animation.

I want to be able to enable and disable their visibility so to simulate an animation, this is to be sent to a software called OsciStudio that generates sound from the objects on the viewport.

What I have tried is this:

import bpy
import glob
import os  

# path to your folder

importDir = "/Volumes/250GB/16mm/rotoscope/stillsprecol/select/" 
print("Importing all SVG from this directory", importDir)

os.chdir(importDir)

for files in glob.glob("*.svg"):
    print( files, "... imported!" )
    bpy.ops.import_curve.svg(filepath=files)
    bpy.ops.object.select_all(action='SELECT')

for i, obj in enumerate(bpy.context.selected_objects, 1):
    bpy.context.view_layer.objects.active = obj
    obj.name = "Curve" + str(i)


    obj.location = (3.0, 4.0, 10.0)
    obj.keyframe_insert(data_path="location", frame=1)

Importing works fine, i get all the files layered and selected, then moved to the specified location and theres one key frame added on the timeline!

But have no idea on how to add a keyframe to each layer, progressively and separated by 10 frames.

I'm using blender 2.9 under OS X

TIA for any, really any, help

$\endgroup$

1 Answer 1

0
$\begingroup$

check this out:

for files in glob.glob("*.svg"):
    print( files, "... imported!" )
    bpy.ops.import_curve.svg(filepath=files)
    bpy.ops.object.select_all(action='SELECT')

for i, obj in enumerate(bpy.context.selected_objects, 1):
    bpy.context.view_layer.objects.active = obj
    obj.name = "Curve" + str(i)


    for x in [10,20,30,40,50]: # the indentation in this line is important!!!
        obj.location = (0.0 + x , 0.0, 0.0) # ADAPT HERE
    
        obj.keyframe_insert(data_path="location", frame=x)

of course you have to adapt the line with "adapt here" to use some mathematics so that you get your progressiveley -> i have no idea what values you want to have there ;)

and of course you can do the "for x in " with a nicer loop...my version is for sure not the most elegant one - but the laziest one ;)

$\endgroup$
4
  • $\begingroup$ Chris many thanks your your help. I have understood how the loops work to generate the increment. Now I only miss how to apply this to each layer/curve in the Scene Collection. $\endgroup$
    – funkz
    Mar 28, 2021 at 16:05
  • $\begingroup$ it does it for every object, because in the main for loop (enumerate...) it walks through all curves (which we selected in the first for loop) and in the sub - for loop (which is executed for every object in the enumerate for loop) - that's why every object gets the keyframes. Maybe you didn't indent the for x in [10,20... loop? then it will only executed once. $\endgroup$
    – Chris
    Mar 28, 2021 at 17:30
  • $\begingroup$ Many thanks again for taking the time to teach and share your knowledge. $\endgroup$
    – funkz
    Mar 29, 2021 at 2:23
  • $\begingroup$ You are welcome! $\endgroup$
    – Chris
    Mar 29, 2021 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.