0
$\begingroup$

what I need is to import a camera track from an external file and to do this in a Python script. The external track is a sequence of camera poses (location + rotation) in world coordinates. How to do this in Blender Python? Since the Camera Solver uses a constraint to do this, I was wondering if I have to use something similar, but I am not sure (and if not, why the camera solver uses a constraint?).

I know I can add to the camera a (location + rotation) keyframe for each frame, that is what I already did, but I am wondering why the camera solver does not do this and why and, as a consequence, if there is some reason I am not considering that makes the use of a constraint a better solution to keyframes. Blender devs added a specific constraint type for this, there must be a reason.

$\endgroup$
3
  • 4
    $\begingroup$ Possible duplicate of Importing camera tracking data from external sources $\endgroup$
    – batFINGER
    Commented Aug 9, 2018 at 17:02
  • $\begingroup$ I added to the question why I am not sure that is the right way to do it. $\endgroup$
    – MadMage
    Commented Aug 10, 2018 at 21:09
  • 1
    $\begingroup$ The camera solver constraint uses the solved data based on your editable settings to translate the camera. With no influence, or if the constraint is removed it has no effect, or side effects. See no need for a constraint if you are simply importing translation data form a static file. Could import tracks to empty and constrain camera to empty to avoid key-framing cam directly. $\endgroup$
    – batFINGER
    Commented Aug 11, 2018 at 13:18

1 Answer 1

1
$\begingroup$

Something similar to this works for me, didn't test if this code runs without errors, but it may help you

import bpy
import numpy as np
dirPath = "C:/Users/examplepath/examplepath/examplepath/camera.blend/Object/"
nameList = ['Camera0','Camera1','Camera2','Camera3']

# [euler0, euler1, euler2, x, y, z]
cam0 = [77.487, -0.000244, 91.4015, 13.6156, 0.094522, 2.19869]
cam1 = [70.2677, -0.000819, 173.623, 1.1328, 11.1731, 3.33687]
cam2 = [85.5084, 0.000169, -90.6852, -12.8706, 0.355527, 1.29131]
cam3 = [74.6794, -0.002039, 11.1926, 3.2923, -15.5643, 4.444]

camList = [cam0, cam1, cam2, cam3]

def setupCamera(scene, c):
    pi = np.pi

    scene.camera.rotation_euler[0] = c[0] * (pi / 180.0)
    scene.camera.rotation_euler[1] = c[1] * (pi / 180.0)
    scene.camera.rotation_euler[2] = c[2] * (pi / 180.0)

    scene.camera.location.x = c[3]
    scene.camera.location.y = c[4]
    scene.camera.location.z = c[5]

    return

# Append cameras to the scene
for i in range(nameList.__len__()):
    bpy.ops.wm.append(filename=nameList[i], directory=dirPath)

scene = bpy.data.scenes["Scene"]

for i in range(camList.__len__()):
    bpy.ops.object.camera_add()
    newCamera = bpy.data.objects['Camera']
    newCamera.name = 'c' + str(i)

    newCamera.rotation_mode = 'XYZ'
    scene.camera = newCamera

    config = camList[i]
    setupCamera(scene=scene, c=config)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .