How do you tell if an object is within a title safe of the camera?

Hello,
I have text that changes every frame. I would like to find out if the text object is within the camera title safe and if the text is outside, scale it down so it fits the title safe. How do I do this?

My first guess is to use the bounding box of the text , and shoot a ray or something from the camera to tell whether it hits or not. I do not know how to do this in Blender which is why I’m posting here.

My second guess is to create a bounding box for the text that reaches the edges of the camera, and see if the text is INSIDE the bounding box.

Edit:

I made the script, it adjusts to fit the text inside the box. I’m having issues with it not updating once the text has changed, I have to run the script twice to have the script notice the text size is larger than the box after it changed. is there a window.redraw() I need to call?

import bpy

def main():
# get current frame and reference name based off of it.
change_name_with_frame(bpy.data.curves[‘Text’])
box_main = bpy.data.scenes[0].objects[‘box_main’]
text_main = bpy.data.scenes[0].objects[‘text_main’]
print(“text main” ,text_main.dimensions ,“box_main” ,box_main.dimensions)
print(“in box?” , text_is_in_box(text_main, box_main))
if text_is_in_box(text_main, box_main) :
# render out stuff
print("rendering… ")
else:
# scale
print(“scaling”)
text_scale_to_box(text_main,box_main)

def text_is_in_box(text_main, box_main):
#compare each of the components to make sure box is larger or equal to text
return text_main.dimensions.x == box_main.dimensions.x and
text_main.dimensions.y == box_main.dimensions.y and
text_main.dimensions.z == box_main.dimensions.z

def text_scale_to_box(text_main, box_main):
text_main.dimensions = text_main.dimensions *
box_main.dimensions.x/text_main.dimensions.x

def change_name_with_frame(text):
frame = bpy.context.scene.frame_current
names = [“Robert”, “Freddyyyyyyyyyy” , “George”]
try:
print(names)
print(names[0])
text.body = names[frame]
print(“changed text”)
except:
print(“an oopsie”)

main()

Here is the blend - frames 0,1,2 have different length strings associated with them. change the frame , run the script twice, see it update.
script.blend (474 KB)

Did you try calling update_from_editmode(), just at a wild guess?

Something that may also help get things in sync is calling Scene.frame_set() to, say, step the frame number by 1 and then back again to the correct frame.

Edit:
Okay this is not working . I tried updates , and I have to press “run script” twice to have it work.
Here is the script


When the name gets to be above 14 characters, it is supposed to shorten character spaces, if it gets above 29 characters, it is supposed to scale it.
Something isn’t getting updated, I’m not sure how to fix it.
I currently have it working by calling main() twice… there’s got to be a better way though :stuck_out_tongue: