1
$\begingroup$

I am importing FBX files that have camera placeholders. They will import as Empty's in blender that may have useful animation. I want to add add a camera to each as a child (inherit animation) and align the view to the placeholder's alignment, parent it, and add a TRACK_TO constraint if there is a "cameraName.Target" Empty placeholder.

$\endgroup$
1
$\begingroup$

The following is an example script that worked for me. I first (manually) created a collection named "cameras" and moved all the Empties into that collection. I skip any existing cameras in case I run the script more than once.

import bpy

for obj in bpy.data.objects:
    hasACamera = False
    if ( ("Camera" in obj.name) and ("Target" not in obj.name) and (obj.type != 'CAMERA') ):
        for child in obj.children:
            if (child.type == 'CAMERA'):
                hasACamera = True
        if ( hasACamera is True ):
            print( obj.name + ": already has a camera")
            continue
        print( obj )
        print( obj.type )
        bpy.ops.object.camera_add()
        bpy.context.object.name = obj.name + ".Camera"
        print( bpy.context.object )
        print( bpy.context.active_object )
        sel = bpy.data.objects[bpy.context.object.name]
        print( sel.name + " align"  )
        bpy.ops.transform.transform(mode='ALIGN', value=(0, 0, 0, 0), orient_type='GLOBAL',
         orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=True, use_proportional_edit=False,
         proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
        sel.parent = obj
        bpy.ops.object.collection_link(collection='cameras')
    if ( (obj.type == 'CAMERA') and (".Camera" in obj.name) ):
        targetName = obj.name[0:-7] + ".Target"
        try:
            targetObj = bpy.data.objects[targetName]
            if obj.constraints["Track To"] is None:
                print( "adding Track To constraint" )
                obj.constraints.new(type='TRACK_TO')
                obj.constraints["Track To"].target = targetObj
        except:
            print()
```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.