# How to set object's rotation from python?

I have more than 100 objects and I want them to point in a particular direction depending on their position. To move them to a particular position, I used:

bpy.context.scene.objects[""].location.xyz = ...


Is there similar command for setting the angle of rotation for an object? I am working in a crossroad with two lanes. The position of the cars are taken as input. So according to their coordinates they are placed on the road, pointing in the direction they need to. The track to constraint is not applying the correct direction to all of them. I checked my if condition. Some of the objects are getting hidden unnecessarily.

• Commented May 30, 2015 at 7:06
• also related: blender.stackexchange.com/questions/19533/… Commented May 30, 2015 at 7:18
• If any of those suggestions helps answer your question, we can mark this as duplicate (no need to delete it) Commented May 30, 2015 at 7:19
• No they aren't answering it completely Commented Jun 1, 2015 at 4:18
• If the suggestions aren't helping but you still would like a solution, take some effort to be more descriptive in your question. Paint a representative scenario, use screenshots of multiple views or upload a .blend file (again, of something representative..doesn't have to be 100s of points) so we can better judge what answer might be most suitable. Right now we know too little. Commented Jun 1, 2015 at 5:46

Here's how to set Euler rotation.

First: from mathutils import Euler

Then: obj.rotation_euler = Euler((0.3, 0.3, 0.4), 'XYZ')

If all the other track_to options are overkill. For more attribute references see the Object documentation

• May you add the line: from mathutils import Euler for the code to work Commented Mar 16, 2018 at 8:36
• Consider [track to] constraint

After you have setup the constraint for one object, select the other objects, select the object with the constraint afterwards, press Space in the viewport, type and choose Copy Constraints to Selected Objects

• I have a doubt. I tried using track to constraint as suggested by you. But once I run the script it is working for objects in particular positions only and not working for all of them. I have checked my if condition lot of times. But I'm not able to figure out where I went wrong. Could you please suggest where could I have gone wrong. Thanks Commented Jun 1, 2015 at 4:07
• FYI, the big arrow looks like something else...
– Alex
Commented Jul 1, 2018 at 6:44
• @Alex Well you know what they say on Mars where everything is Green. Big Constraint Big Arrow. Commented Jul 2, 2018 at 17:06