8

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.

gandalf3
  • 157,169
  • 58
  • 601
  • 1,133
Arun TechM
  • 101
  • 1
  • 1
  • 4
  • related: http://blender.stackexchange.com/questions/16275/python-implementation-of-track-to-aim-constraint?rq=1 – zeffii May 30 '15 at 07:06
  • also related: http://blender.stackexchange.com/questions/19533/align-object-to-vector-using-python?rq=1 – zeffii May 30 '15 at 07:18
  • If any of those suggestions helps answer your question, we can mark this as duplicate (no need to delete it) – zeffii May 30 '15 at 07:19
  • No they aren't answering it completely – Arun TechM Jun 01 '15 at 04:18
  • 1
    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. – zeffii Jun 01 '15 at 05:46
  • Does obj.rotation_euler = Euler((0.3, 0.3, 0.4), 'XYZ') not suffice ? – zeffii Jun 01 '15 at 12:09
  • Thanks zefii. ob.rotation_euler = [1.5708,0.0,1.5708] did the trick. Thanks for the help – Arun TechM Jun 02 '15 at 04:46
  • good . sometimes we think of complicated solutions to simple problems, sorry for the noise! onwards! – zeffii Jun 02 '15 at 14:10

2 Answers2

8

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

Mentalist
  • 19,092
  • 7
  • 94
  • 166
zeffii
  • 39,634
  • 9
  • 103
  • 186
1
  • Consider [track to] constraint constraint panel in properties area constraint in action

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

pink vertex
  • 9,896
  • 1
  • 25
  • 44
atomicbezierslinger
  • 14,279
  • 28
  • 42
  • 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 – Arun TechM Jun 01 '15 at 04:07
  • 4
    FYI, the big arrow looks like something else... – Alex Jul 01 '18 at 06:44
  • @Alex Well you know what they say on Mars where everything is Green. Big Constraint Big Arrow. – atomicbezierslinger Jul 02 '18 at 17:06