Dynamic Text Animated

Hi!

I’m so happy that I’m working with Blender daily. More then work I’m learning a lot, and is my pleasure and almost obligation to share it back to the community.

Today I needed to animate the years along the frames in an animation. I couldn’t find anything in the forums so I decided to code it. I used Yorik’s parametric dimension as start point.

import Blender as B 
 
def when (frame,  years): 
     
    iniY = 0 
 
    for y in range(len(years)): 
        if frame > years[y][1]: 
            iniY = y 
 
    iniYear       = years[iniY][0] 
    iniFrame      = years[iniY][1] 
    iniFrameDelay = years[iniY][2] 
 
    finYear  = years[iniY+1][0] 
    finFrame = years[(iniY+1)][1] 
 
    frameRange = finFrame - (iniFrame + iniFrameDelay) 
    yearRange = finYear - iniYear 
     
    normFrame = float(frame - iniFrame - iniFrameDelay) 
    normFrame = normFrame/frameRange 
 
    if normFrame > 0: 
        newYear = str(int(iniYear + (yearRange * normFrame))) 
    else: 
        newYear = iniYear 
     
    return str(newYear) 
 
if B.bylink: 
 
    actualFrame = B.Get("curframe") 
 
    year = B.link 
    dynYear = year.getData() 
 
    Years = [] 
 
    # year = [year, initial frame, duration of frames] 
    Years.append([1800, 1, 10]) 
    Years.append([1850, 100, 50]) 
    Years.append([1994, 170, 100]) 
    Years.append([2008, 300, 50]) 
    Years.append([2050, 400, 50]) 
     
     
    oldYear=dynYear.getText() 
    newYear=when (actualFrame,Years) 
 
    if newYear != oldYear: 
        dynYear.setText(newYear) 
        year.makeDisplayList() 
        B.Window.RedrawAll()

To use this script you should create an object Text, Enable Scripts Link to this this object and link this script to the event Frame Change. To change the years you just need to change these lines:

# year = [year, initial frame, duration of frames]
Years.append([1800, 1, 10])
Years.append([1850, 100, 50])
Years.append([1994, 170, 100])
Years.append([2008, 300, 50])
Years.append([2050, 400, 50])

Adding the year, the initial frame to this year, and the duration of of this year in frames.

Released to Blender 2.46, but probably works in old versions.
Fell free to use, change, comment, share, …

Great hugs,
Dalai Felinto

Thanks for sharing codes. Looking interesting.

dfelinto,
The script does not actually work. I get and error on line 36.


 'Scene' object has no attribute 'getData'

In your instructions, you say to create a font object and script link to it. But your script never refers to the name of the default font object which is typically called “Font”.

I would like to see this work, can you help?

Thanks for the interesting …

In the Enable Script Link window we have two different options:
Selected Script Link
and Scene Script Link.

You should select the text object and enable the Selected Script Link. That way you don’t need to worry about names :slight_smile:

Please let me know if it works for you.

Abraços,
Dalai

dfelinto,

Sorry, it still reports the same error and does not work.

I have used the scriptlink before so I’m sure I have it set up correctly. I think there is some flaw in the code. somewhere in the code, you kust reference the object eh?

I’m pretty sure it is working in Blender 2.46 in WIndows. What version of Blender are you running?

Anyways, I submitted the file to a Brazilian forum, so you can download from there.

http://www.blender.com.br/components/com_fireboard/uploaded/files/dynamicYear.blend

Have fun !

delinto,

Thank you for posting the BLEND file. I got it to work. I had linked my object to the Scene Script Link, not the Selected Script Link.

This little example helps me a lot.

I just put this script in the Blender Python Cookbook:

http://en.wikibooks.org/wiki/Blender_3D:_Blending_Into_Python/Cookbook#Dynamic_Text

And I put a video with an example online:

http://www.vimeo.com/1311712

:slight_smile:

Once again, I hope it can help more people.