5
$\begingroup$

I'm completely stuck :-/

I've made a handler-script, that takes with every new frame a new line from the text-editor and puts that line into the text-object in 3d-space. It works absolutely fine - but when I want to render it, is it not working any more.

Why is that?? Other handler scripts from me are working fine...

import bpy

'''Text-Objekt muss angewählt sein und im Object-Mode
##################################################'''

def Text_Line_by_Line(Scene):

    FrameNumber = bpy.context.scene.frame_current - 1 #aktuelle Frame-Nummer minus 1 weil python 1 = 0

    TextFile = bpy.data.texts['Text'] #<------------Text-Name eingeben
    TextLine = TextFile.lines[FrameNumber].body  # Nimmt die Text-Line als String

    bpy.ops.object.editmode_toggle() 
    # Vorherigen Text löschen
    bpy.ops.font.select_all() #selektieren
    bpy.ops.font.delete()   #löschen

    bpy.ops.font.text_insert(text=TextLine)  #Fügt den Text ein

    bpy.ops.object.editmode_toggle()

bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append(Text_Line_by_Line) 
$\endgroup$

1 Answer 1

7
$\begingroup$

Don't use operators in handlers

enter image description here

For the most part don't use operators in handlers. When rendering for example, the context needed will not be there. I imagine edit mode toggle means nada to the renderer. Use API methods and the passed argument scene

In example below, displays a line of bpy.data.texts["Text"] as the body of text object scene.objects["Text"]

import bpy

# pass arguments to build a handler
def text_body_handler(obname, textname):
    # the handler method
    def text_body(scene):
        text = bpy.data.texts.get(textname)
        text_ob = scene.objects.get(obname)
        if not text and text_ob:
            print("Error: check names")
            return 
        f = scene.frame_current
        lines = len(text.lines)
        if text_ob and f <= lines:
            text_ob.data.body = text.lines[f-1].body
 
    return text_body

bpy.app.handlers.frame_change_post.clear()           
bpy.app.handlers.frame_change_post.append(text_body_handler("Text", "Text"))

Textblock bpy.data.texts["Text"]

I had written him a letter 
which I had, 
for want of better Knowledge, 
sent to where I met him 
down the Lachlan, years ago,
He was shearing when I knew him, 
so I sent the letter to him,
Just ‘on spec’, addressed as follows,
‘Clancy, of The Overflow’.
$\endgroup$
2
  • $\begingroup$ Wow.... Thank you sooo much! Your script is working perfect. Here I can see clearly, that I've got no idea about programming... I can't understand your script - but it works and I'm very happy :) I wish you a great time! $\endgroup$
    – Andi
    Commented Dec 16, 2019 at 20:31
  • 1
    $\begingroup$ Hi @Andi. In case the answer is "wow" or even solved your issue, please learn how to accept and upvote it for future visitors. Cheers $\endgroup$
    – p2or
    Commented Dec 17, 2019 at 9:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .