0
$\begingroup$

I want to render a 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. Everything else in the scene stays the same.

So, (as an example) here is MyText.py

import bpy

context = bpy.context
scene = context.scene

# delete all default objects
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=False)


# ADD TEXT HERE 

bpy.ops.object.text_add(view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

# EDIT TEXT HERE 

bpy.ops.object.editmode_toggle()
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.text_insert(text="M", accent=False)
bpy.ops.font.text_insert(text="Y", accent=False)
bpy.ops.font.text_insert(text=" ", accent=False)
bpy.ops.font.text_insert(text="T", accent=False)
bpy.ops.font.text_insert(text="E", accent=False)
bpy.ops.font.text_insert(text="X", accent=False)
bpy.ops.font.text_insert(text="T", accent=False)

bpy.ops.object.editmode_toggle()


bpy.ops.transform.rotate(value=1.5708, axis=(1, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)

bpy.context.object.data.extrude = 0.05

bpy.context.object.data.size = 1.0

MY TEXT

I want to accept user input to change the text (edit the copy) with a limit of up to 20 characters. The input will eventually come from an HTML form that uses php to update the MyText.py script with the USER TEXT all within the .blend file

import bpy

context = bpy.context
scene = context.scene

# delete all default objects
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=False)


# ADD TEXT HERE 

bpy.ops.object.text_add(view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

# EDIT TEXT HERE 

bpy.ops.object.editmode_toggle()
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION')
bpy.ops.font.text_insert(text="U", accent=False)
bpy.ops.font.text_insert(text="S", accent=False)
bpy.ops.font.text_insert(text="E", accent=False)
bpy.ops.font.text_insert(text="R", accent=False)
bpy.ops.font.text_insert(text=" ", accent=False)
bpy.ops.font.text_insert(text="T", accent=False)
bpy.ops.font.text_insert(text="E", accent=False)
bpy.ops.font.text_insert(text="X", accent=False)
bpy.ops.font.text_insert(text="T", accent=False)

bpy.ops.object.editmode_toggle()


bpy.ops.transform.rotate(value=1.5708, axis=(1, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)

bpy.context.object.data.extrude = 0.05

bpy.context.object.data.size = 1.0

USER TEXT INPUT

Is it possible to manipulate a .blend file in this manner? Do I need to call (execute) several python scripts to accomplish this, or can it be accomplished in one python script where (1) it loads the initial MyText.py, (2) accepts user input and changes the text accordingly (a new python script e.g. UserText.py?) and (3) renders the updated .blend file

I am using Terminal (Mac OS) to call the .blend file which has the initial (saved) python script MyText.py and output the file with formatting options to a specific output folder. I am using Blender 2.79

I am new to python and so any code examples would be extremely helpful and appreciated. Thank you in advance for any help you may provide.

$\endgroup$

1 Answer 1

1
$\begingroup$

First of all, that's an inefficient script: to change the content of a text object you just need to change his body property. As for the importing bit, I'm not sure how do you want to do it with html, but looking around I think it's an hard think to do with php. I usually do it with text files: in php you create a .txt file with your text (and if you need other variables) in the folder with the .py file, then your script becomes something like:

import bpy

# delete all default objects bpy.ops.object.select_all(action="SELECT") bpy.ops.object.delete(use_global=False)

#READ FILE

filename = 'yourfile.txt'
directory = '/home/username/Desktop'  #just an example, put it where you want
fullpath = os.path.join(directory, filename)

if os.path.exists(fullpath):

    with open(fullpath,'r') as file:
        text = file.readline()
    file.close()
else:
     text = "MY TEXT"

# ADD TEXT HERE 

bpy.ops.object.text_add(view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

# EDIT TEXT HERE 

bpy.context.active_object.data.body = text


bpy.ops.transform.rotate(value=1.5708, axis=(1, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)

bpy.context.object.data.extrude = 0.05

bpy.context.object.data.size = 1.0

(I didn't test this script, so there could be some little errors)

$\endgroup$

You must log in to answer this question.

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