0

I try to add the Date from a .csv file as text in Blender 2.91 via Python. It creates for each row a new Text and works fine in Viewport (one date per frame) but when I try to render this code as animation Blender crashes, if rendered as image it shows all Dates layered on each other:

import csv
import bpy

def render_stock(csv_path, start, start_frame): with open(csv_path) as stock_csv: stock_ob = csv.reader(stock_csv.read().splitlines())

stock_header = []
stock_data = []

for v in stock_ob:
    if not stock_header:
        stock_header = v
    else:
        v = [(float(v[0])),
             (float(v[1])),
             (float(v[2])),
             float(v[3]),
             str(v[4]),
             str(v[5])]
        stock_data.append(v)
        scale_factor = 0.3       


frames = start_frame
objs = []

for counter in range(len(stock_data)):

    v = stock_data[counter]


    bpy.ops.object.text_add(location =(9,15,0.5), rotation =(1.7,0,-1.9))
    bpy.context.object.data.body = (v[5])


def ani_handler(render):
    name = 'Text'
    objs = [obj for obj in render.objects.values() if name in obj.name]

    for i, obj in enumerate(objs):
        obj.hide_set(i != ((render.frame_current-1) % len(objs)))
bpy.app.handlers.frame_change_pre.append(ani_handler)



render_stock('/"filepath to" .csv', (0, 0, 0), 0)

PhilPhil
  • 1
  • 1
  • Wouldn't do it with hide / show. See https://blender.stackexchange.com/a/161107/15543 – batFINGER Jan 27 '21 at 06:19
  • Tried it, but failed with the last line of code: KeyError: 'bpy_prop_collection[key]: key "2021-01-25" not found' (changes "Text" to v[5] in scene.objects.get and bpy.data.texts, the Text objects are Named by v[5] ) – PhilPhil Jan 27 '21 at 11:04
  • Another try...Now I used your code from the linked answer as it is. And it runs without error. I have one text-object named "Text" and another 61 text-objects Named Text.001, Text.002, etc. Still in the render picture the overlay problem exists./ Any suggestions? – PhilPhil Jan 27 '21 at 11:22
  • Delete them?... – batFINGER Jan 27 '21 at 11:23
  • creates emptiness... I understand that in my code for every row in the csv a new text-object is added, as with your solution with every row a new line in the text-object should be created, so it works - or? – PhilPhil Jan 27 '21 at 12:06
  • now it works for two of the text-objects... but with the third I got in trouble: https://blender.stackexchange.com/questions/209793/how-to-exclude-a-new-text-body-from-a-text-body-handler – PhilPhil Jan 28 '21 at 15:17

1 Answers1

0

Thanks to batFINGER, the new code from this link: blender.stackexchange.com/a/161107/15543

gives a workaround.

First step: Inserting the Dates from the csv file into a new text file named "text".(by hand)

Second step: Putting the text_add line from above behind the csv loop with the text_body_handler as linked here. (run code and enjoy the Date in the rendering)

:)

PhilPhil
  • 1
  • 1