(solved)selecting text from file/user input

http://dl.dropbox.com/u/671816/blender/spinning%20text.blend
This 2.54 code sets text read in from a file (or string by default) to an already rotating/keyed text object in the 3d window
The blend file works the best -since it’s already linked n’ all.
modify the first few variables to get it to do what you want.

import bpy
from bpy import *


##// MODIFY THESE VARIABLES :
input = 'Hello There Panda Bear' # what you want rotating text to say
useFile = False; #define whether to use a file or not
path = 'c:/txt.txt' # only if using a file
render = False # render openGL animation?
################################
if(useFile):
    f = open(path,'r')
    first =f.readline() ##// stop at end of first line to keep it simple

else:
    f = input ## set this string to the current text 
    first =f


ob = bpy.ops.object
ob.select_all(action='DESELECT')
ob.select_by_type(extend=False, type='TEXT')

#how do I select object by name, and ONLY that object? 
# try selecting one of the meshes or the camera first, and then run the script. error will occur.
txt = bpy.data.objects['Text'] 
if txt.select:
    ob.editmode_toggle()
    bpy.ops.font.move(type='LINE_BEGIN')
    bpy.ops.font.delete(type='ALL')
    bpy.ops.font.text_insert(text=first.upper())
    ob.editmode_toggle()


if(useFile):
    f.close()
## render out animation
if(render):
    bpy.ops.render.opengl(animation=True, view_context=True)



The Problems are in the middle - selecting things. I tried deselecting all the items, then only selecting the text items (which is just one). this works if the text item is selected before the script runs. however, if any other item is selected first, the program runs into an error - somehow it’s toggling the mesh edit mode even though it isn’t active.

any suggestions on how to make it select only the object named “Text” from any pre-selection configuration ?

I’m trying to make this as easy as possible so anyone playing with the file won’t accidentally mess it up.

where this is going:
Eventually, I’d like to have someone just input text into a window outside of blender, then have the text inputted into a text animation/graphic template and rendered out without ever seeing the blenderGUI. Essentially a newbie template library ;it would be useful for helping people who really don’t have the time to learn blender but want some good templates.

Here is how I do it in 2.5.

If you know the name and type of object you want to fetch, just call the appropriate routine.


############################################################################
# Code for returning objects, groups or meshes.
############################################################################
def fetchIfObject (passedName= ""):
    try:
        result = bpy.data.objects[passedName]
    except:
        result = None
    return result

def fetchIfMesh(passedName=""):
    try:
        result = bpy.data.meshes[passedName]
    except:
        result = None
    return result

def fetchIfCurve (passedCurveName):
    result = None
    try:
        result = bpy.data.curves[passedCurveName]
    except:
        pass
    return result
    
def fetchIfText3D (passedName):
    result = None
    tempCurve = fetchIfCurve(passedName)
    try:
        n = tempCurve.active_textbox
        #No error, this must be a font curve.
        result = tempCurve
    except:
        #Error, this is a non-font curve.
        pass
    return result

def fetchIfLight (passedLightName):
    result = None
    try:
        result = bpy.data.lamps[passedLightName]
    except:
        pass
    return result

def returnScene ():
    return bpy.context.scene

def returnChildren(ob):
    return [ob_child for ob_child in bpy.context.scene.objects if ob_child.parent == ob]

def isObjectLinkedToScene (passedObjectName):
    localScene = returnScene()
    result = False
    listOfNames = returnObjectsInScene(localScene)
    if passedObjectName in listOfNames:
        result = True
    return result

Usage:


txt = fetchIfText3D(passedName)
txt.body = "myString Here"

Remember, the passedName is the name of the text curve, not the text object.

by the way one function is not defined
returnObjectsInScene!

also where can you find this curve name for the text object ?i can see the Text name in Text panel but not for the curve associated

thanks happy 2.5

@Atom: Thanks Atom. Setting the text works now. That was a much simpler process too.:yes:
@ RickyBlender:“also where can you find this curve name for the text object ?i can see the Text name in Text panel but not for the curve associated”
Hover your cursor over the that panel, should read at the bottom

Python: TextCurve.name

That should be the name of the curve - it’s independent of the text object.
here’s the code so far (and previous blend is updated):

import bpy
from bpy import *

def main():
    yes = True
    no = False
    file = False
    ##// MODIFY THESE VARIABLES :
    input = 'Hello World' # what you want rotating text to say
    useFile = yes #define whether to use a file or not
    path = 'c:/txt.txt' # only if using a file
    render = no # render openGL animation?
    ################################
    if(useFile):
        try:
            f = open(path,'r')
            first =f.readline() ##// stop at end of first line to keep it simple
            file = True
        except:
            file=False
            first = input
    else:
        first = input       
    
    setText(first.upper())
    
    if(file):
        f.close()
    ## render out animation
    if(render):
        bpy.ops.render.opengl(animation=True, view_context=True)

def setText (message="",passedName= "Text"):
    text = fetchIfText3D(passedName)
    if(fetchIfText3D(passedName)):
        text.body = message
        return True
    return False

def fetchIfCurve (passedCurveName):
    result = None
    try:
        result = bpy.data.curves[passedCurveName]
    except:
        pass
    return result
    
def fetchIfText3D (passedName):
    result = None
    tempCurve = fetchIfCurve(passedName)
    try:
        n = tempCurve.active_textbox
        #No error, this must be a font curve.
        result = tempCurve
    except:
        #Error, this is a non-font curve.
        pass
    return result

main()  

Updated blend file with python script.

Attachments

example.blend (392 KB)

sorry but the text window is empty !

thanks

could you be more specific?