Pivot point inside/outside of frame?

Hi, Python noob with a very specific question here.

Is there any “value” in the Python API that allows me to check if the pivot point of a selected object is either inside or outside the frame of the camera? Something which returns a “true”/“false” state maybe?


There is an included function in the bpy_extras module that will give you the viewspace coordinates of a 3d point. Here is an example function that you could plug into your script:

import bpy,bpy_extras

def inframe():
    scn = bpy.context.scene
    cam = scn.camera
    co = bpy.context.active_object.location
    viewloc = bpy_extras.object_utils.world_to_camera_view(scene=scn, obj=cam, coord=co)
    if viewloc[0] > 1:
    elif viewloc[0] < 0:
    elif viewloc[1] > 1:
    elif viewloc[1] < 0:
    else: return(True)

This works with the scene camera and the active object. Hope that helps!

Wow, exactly what I was looking for, thank you!

Another question:

The goal of my script is to move a selected object along its Y-axis until its pivot point matches with the edge of the camera frame.

I went about this by creating a while-loop which moves the object one very small length at a time, and if the state of “inframe” changes, the loop ends. At the moment however, the loop runs forever, crashing Blender.

Any ideas where the problem might be/alternative solutions?

Do you want object to move while Blender struggles to render a frame? I don’t think this is possible. I could be wrong, ofc.
If not, you should move object on frame change, before or after. Idk if this is still valid but should be something similar.

Your while loop is calling while True. You need to have it call the inframe() function. You also don’t need that first if statement, and that offset is very small. If you absolutely need that precision, it will take a while to process.

while inframe():

Again, thanks for your help Sterling, it worked just fine!