Track to Active Camera

Greetings Blender Artists,

So far in all my animations I’ve used only a single camera per scene, moving it about within a frame to make a cut from shot to shot. I’d like to convert to using multiple cameras through binding them with markers, for more freedom with constraints and what not. I’ve run into a small problem, though.

See, previously if I wanted an object/plane to be constantly tracked to the camera, I could simply make a Track To constraint and select my single camera. With multiple cameras, though, since I can only track it to a single camera, when the camera cuts to a new camera, the object is no longer facing the active camera, but a previous camera.

How can I track an object to the Active Camera instead of an object?

I’ve heard that some people make a Track To constraint for every camera and then toggle the Influence for the relevant shots. This can get quite tedious, though, with long scenes and multiple objects needing to the face the active camera.

Others create an empty with a lot of Child Of constraints for each camera and then track their planes to that empty, but that’s still a lot of hassle with Influence and what not.

Is there no simpler way?

Thanks in advance.

Attached:
Track to Active Camera.blend (540 KB)
Blend file with two camera set-up and an object tracked to the first camera. How do I make it switch to the new active camera automatically?

1 Like

The first thing that came to mind was a handler function, very useful to create custom constraints and whatnot.

So I coded a few lines and it seems to work pretty well :wink:
Just paste it in a Blender text block and run it, you can change a few parameters at lines 5, 6 and 7. If you want to change the constraints after you ran it, for example changing the axes, that’s totally fine. It just keeps updating the influences for you.


import bpy
scn = bpy.context.scene

# Some custom variables
object = scn.objects['Plane']
track_axis = 'TRACK_Z'
up_axis = 'UP_Y'


# Add camera constraints to an object (if necessary)
def track_to_cams(object, track, up):
    
    # Create a list of cameras already used as constraint targets
    cams_used = []
    for constr in object.constraints:
        if constr.type == 'TRACK_TO' and constr.target.type == 'CAMERA':
            cams_used.append(constr.target)
    
    # Add the remaining cameras as constraint targets
    for targ in scn.objects:
        if targ.type == 'CAMERA' and targ not in cams_used:
            constr = object.constraints.new('TRACK_TO')
            constr.name = 'Track To ' + targ.name
            constr.target = targ
            constr.track_axis = track
            constr.up_axis = up


# Define a handler function to run on each frame update
def update_track_constr(scene):
    
    # Loop through all object constraints 
    for constr in object.constraints:
        
        # Check whether to set the influence to 1.0 or 0.0
        if constr.type == 'TRACK_TO':
            if constr.target == scene.camera:
                constr.influence = 1.0
            elif constr.target.type == 'CAMERA':
                constr.influence = 0.0


# Run the track to cams function
track_to_cams(object, track_axis, up_axis)    
    
# Append the handler function
bpy.app.handlers.frame_change_pre.append(update_track_constr)

6 Likes

Thanks, richardvdoost! That script works great, but is there no way to do it without scripting?

Not that I’m aware of, if you want the script to run when you open your .blend file. Give the text a name that ends with .py, (i.e. camera_constraints.py) and then check the “register” checkbox.

Then you have to click “reload trusted” in the top right corner every time you open it. This is a Blender security feature. If you want to disable that feature, go to the user preferences -> File and enable “auto run python scripts”

Hello there!

This Script is AMAZING!!!
With it and my non-existing scripting skills I was able to do a copy transform to bring all the cameras in one (To create a ubercam to give to another department.)

The thing that I didn’t achieve yet though is to have the focal length to match all the cameras on the Ubercam (So pretty much to animate the focal on the ubercam to reflect what it looked like in the view)

If you think that would be doable within that script, that would be great!

One way or another, I am super grateful for this script!!!

Thanks a lot :smiley: !!!

Ray

I just wanted to say that this still works 6 years later ahah, good job and thank you !!

1 Like

btw,

I actually managed to make a simpler version:

But again, it wont work for Eevee. And it maybe just easier to use the code. But you know, just putting this out there that there is actually a way to do this without scripting

2 Likes

Hahah, thanks, that’s great :slight_smile: