1
$\begingroup$

I can track objects with camera constraints. I want to update object which we aimed(tracking new objects). So, I tried to update target object of the constraint but it does not work. Now, I am trying to kill camera constraint but camera should be remain so I would set it to track another object. The camera constraint code shown below.

cam_data = bpy.data.cameras.new('camera')
cam = bpy.data.objects.new('camera', cam_data)
bpy.context.collection.objects.link(cam)
cons = cam.constraints.new(type='TRACK_TO')
cons.target = track_object
scene.camera = cam
$\endgroup$
3
  • $\begingroup$ Do you mean that the constraint isn't updated when you change its target ? $\endgroup$
    – Gorgious
    Commented Mar 8, 2022 at 10:00
  • $\begingroup$ Yes, it still aim old target. $\endgroup$
    – dasmehdix
    Commented Mar 8, 2022 at 16:02
  • $\begingroup$ Your code fragment doesn't show how you set target_object. Are you sure it's set to the right object? $\endgroup$ Commented Mar 8, 2022 at 16:34

1 Answer 1

3
$\begingroup$

Your code fragment is missing two things:

  1. How you set the scene variable. I'll assume you wanted the active scene and use
scene = bpy.context.scene
  1. How you set the track_object variable. I'll assume you wanted the active object and use
track_object = bpy.context.active_object

With those two changes, your code looks like

import bpy

track_object = bpy.context.active_object
scene = bpy.context.scene

cam_data = bpy.data.cameras.new('camera')
cam = bpy.data.objects.new('camera', cam_data)
bpy.context.collection.objects.link(cam)
cons = cam.constraints.new(type='TRACK_TO')
cons.target = track_object
scene.camera = cam

and when I run it it creates a camera with a Track To constraint set to point at the active object in the scene I created.

Camera track to constraint pointing at active object

When I later execute

cons.target = bpy.data.objects["Suzanne"]

and I have a Monkey in the scene, the Track-To target changes to the Monkey.

So your problem seems to be that you haven't set track_object to the object you want as the target.

EDIT:

Here is a Blend file with a cube and a monkey. I've modified the code to use the cube rather than the active object as the first target of the track to, but you can double check my work. It's a 3.0.1 file, but should work just fine in any Blender version since 2.8

$\endgroup$
4
  • $\begingroup$ My code is almost same like you provide. My problem is not tracking an object. My problem is tracking a new object after tracking an old object. I mean, after your code try to track a new object, it would not work. $\endgroup$
    – dasmehdix
    Commented Mar 9, 2022 at 19:46
  • 2
    $\begingroup$ It works fine with my code if I write something like cons.target = bpy.data.objects["Suzanne"] and I have a Monkey in the scene, it will switch the target to to the Monkey. $\endgroup$ Commented Mar 9, 2022 at 20:11
  • $\begingroup$ Really, it does not work on mine. Ok, I'll try again. $\endgroup$
    – dasmehdix
    Commented Mar 9, 2022 at 20:27
  • $\begingroup$ Perhaps you've done something in your code so that cons is no longer the track-to constraint? Try running all of your code in the Python console. You should see the target in the track-to change when you run the second bit of code. $\endgroup$ Commented Mar 9, 2022 at 20:39

You must log in to answer this question.

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