Programmatically modifying a .blend file / need to change text inside a scene - how?

Hi there!

Currently I am thinking about rendering a large batch of short videos in blender. They are all based on the same .blend file, a simple scene with a text object in front of a background.

Now, the task is that each of the videos I need to render from that scene has a different text object, or simply: I need to change the text-object in every iteration of the rendering.

Normally, by hand, I would open blender, load the scene, select the object, go into edit mode, type in the text as required, exit edit mode, go to render tab, fire the render to a specific output video file.
I need to automate this!

Is it possible to manipulate a .blend file from outside Blender without breaking it? So I can write a Perl script that does the modification and then calls Blender in command-line mode?

I need to know if this is possible without using Python, as I would have to learn Python for that. I am fluent in Perl. (…I know, this forum section is for python scripts, but I could not find a better place to ask stuff about scripted usage of Blender…)

And if it is possible, please can somebody give directions.

Blender does have an onLoad script link. Simply put your code there. Then when the scene loads, you can read in a text file and assign the “copy” in the text file to the font object in the scene. Your external script/batch mechanism will have to populate the file that the BLEND file will read on load.

I had thought of a similar solution, but did not know where my Perl script ends and Blender should take over.

As I understand it:
the .blend file has a script in the scene’s text editor which has an onLoad hook. This gets executed once the .blend file is loaded and the Python in this sub would load my text file and modify the text-object. Right?

Can someone (you) post some code please, as I need to read into Python, the OnLoad and scripting stuff in general now… Having a basic structure of how such a script would look like would help me get results faster.

Here is some code that I used in a frame change event to alter text based upon what frame it was.


import Blender as B

if (B.bylink):
    actualFrame = B.Get("curframe")

    objText = B.link
    tempOBJ = objText.getData()

    result = "Default"
    if (actualFrame == 1): result = "Rewind"
    if (actualFrame > 1):result = "Playing..."
    if (actualFrame > 30):result = "One Second"
    if (actualFrame > 60):result = "Two Seconds"
    if (actualFrame > 90):result = "Three Seconds"
    if (actualFrame > 120):result = "Four Seconds"
        
    tempOBJ.setText(result)
    objText.makeDisplayList()
    B.Window.RedrawAll()

Now this will only work if your font object is still a font object, so you can not convert it to a mesh.

Reading a text file can be done with pure python and is not really Blender related, you can look that up on the web if you don’t know how. You could also search this forum as well.

This might be of more help, it will run even if not in a frame change event.


import Blender
from Blender import Text3d

# Read text file to populate result.    
result = "Four Seconds"

# Name of curve, not object.
tempOBJ = Blender.Text3d.Get("myFontCurve")
if tempOBJ != None:
    tempOBJ.setText(result)
    Blender.Window.RedrawAll()
else:
    #Email failure.
    print "failed."


#!BPY
import Blender
from Blender import Text3d

# Read text file to populate result.    
result = "Testing"

textOBJ = Blender.Text3d.Get("Text1")
if textOBJ != None:
    textOBJ.setText(result)
    Blender.Window.RedrawAll()
else:
    print "failed."

Mmmh, I am trying this in blender 2.49, but it does not work! I start blender along with this script, command is: “blender.exe -P script.py scene.blend”

Now, as the script executes on startup, it seems as if the scene at execution time is not yet present. On console “NameError: Curve ‘Text1’ not found.”. However, when I do it in blender, in the interactive console, it happily spits out the “[Text3d “Text1”]” hint.

Any help? The example form the docs works okay, used on a new scene. Somehow modifying a scene with existing objects/curves seems to be another story…

UPDATE: Solved!
I’ve just found out that the order you are passing things to the blender binary is important! So doing it this way: “blender.exe scene.blend -P script.py” does work like a charm: first load the scene, then run the script on it…

Another happy camper! :wink:

Nice, I do remember something about order of operations on command line parameters. Nice catch!