To programmatically add a constraint to the camera in my scene, I do the following:
import bpy from mathutils import Vector from mathutils import Quaternion import random camera = bpy.data.objects.get("Camera") target_object = bpy.data.objects.get("Cube") camera.constraints.clear() track_to = camera.constraints.new('TRACK_TO') track_to.target = target_object track_to.track_axis = 'TRACK_NEGATIVE_Z' track_to.up_axis = 'UP_Y' # <- is it possible to change this somehow?
This works fine and the camera correctly follows the cube. However, for my task, I need the camera constraint to randomly change the selected
up_axis to an arbitrary vector on the plane, which lies orthogonal to the
track_axis every time I run the script. I hope Figure 1 clarifies my goal. Is there a simple way to do this?
I already tried:
loc, rot, scale = camera.matrix_world.decompose() new_rot = Quaternion(rot.axis, random.uniform(-180, 180)) camera.constraints.clear() # <- seems to be required for the rotation changes to take effect camera.rotation_quaternion = new_rot
But this approach causes the camera to entirely lose focus on the cube.
Thank you in advance!