Python Implementation of Track-to/aim constraint

I'm writing a python script that creates a large number of objects and I'd like them all to point at a certain point in 3d space.

(left is what I've got, right is what I want)

Pretty much exactly like the 'Track-to' constraint, but I don't need it to update interactively or be animated, I just need to rotate each object when they are created. I also don't care about the up-direction at all, since the objects are cylindrically symmetrical.

I know how to create the objects, but I don't know how to aim them. I'm not good at maths, so I don't even know where to begin with this other than that it'll probably involve vectors and matrices (which I don't really understand).

The reason I don't want to use the existing constraint system is because it's a bit slow when dealing with a lot of objects, and I'd like to learn a bit about this sort of maths.

I can't explain the math to do it manually, but you can use python to create a constraint that will do the rotation for you. You can then apply the visual transform and delete the constraint if you don't want to leave it there.

targetobj = bpy.data.objects['Sphere']
pointyobj = bpy.data.objects['Cone']

ttc = pointyobj.constraints.new(type='TRACK_TO')
ttc.target = targetobj
ttc.track_axis = 'TRACK_Z'
# we don't care about the up_axis
# but default is Z and it needs to be different that track_axis
ttc.up_axis = 'UP_X'

bpy.ops.object.select_all(action='DESELECT')
pointyobj.select = True
bpy.ops.object.visual_transform_apply()

pointyobj.constraints.remove(ttc)


Not sure if it will help but I believe the trackto_evaluate function currently on line 965 of constraint.c is the math behind the constraint.

• To make a camera point to a target using Python, I used the following values: ttc.track_axis = 'TRACK_NEGATIVE_Z'; ttc.up_axis = 'UP_Y'; – Justas Apr 24 '18 at 21:26

Join your objects. Control-J. See below.

For mathematical recreations you might see the link

http://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula

Also consider free online books from your library if possible. Computer Graphics, 3D Computer Graphics.

For Immediate Results

(01) Create a suitable emitter mesh for placement of your objects. Duplicate your pointer object as archive .. Make mesh data single user. Use Particles on emitter mesh with your object with a suitable center ..Particles will not copy constraints. Set emission number to your taste. See Blender Particles online educational videos if necessary on a video internet site.

(02) [Convert] Particles to Real Objects under Modifier Tab. You will have many new objects selected for user convenience.

(03) Group the new objects

(04) Add Constraint Track To on a single object of group .. the active object. Select whole group keeping the Active Object and ......[Copy constraints to selected objects]. You will have many objects pointing to the same target. You may move target or group as whole to test.

If your computer has enough CPU Power I would stop at this step. You might want to list the specs of your computer. Many people use track to constraints ... they are dynamic and flexible and appreciated. A couple of [Track to] constraints are your friends.

(04.5) If you cones are placed already ... you can use them in the next step.

(05) If it suits your tastes [Join] the objects of the group. You will have one complex mesh composed of the original of the cones. Join is Control-J in object mode .. See Blender menus. You will have one [track to] constraint. You can remove the constraint and rotate the complex mesh into place on last time ..since the mesh will move when the constraint is deleted. Now there are now zero [track to] constraints.

Consider dupligroups.

In the image above is an emitter mesh, red cube target, original gold cone with constraint, backup of cone, particle system, converted particles in green and a copy to show that it is now a single mesh

• Thanks for the detailed answer, but as the title says I'm looking for a scripted python way, or at least an explanation of the math. – jbrick Sep 30 '14 at 8:53