0

I want to find angle of an cylinder so that it will always perpendicular to the position in another object faces. I got face(polygon in python) position by 'bpy.context.active_object.data.polygons['index_number'].center' and each face also have rotation saved as 'bpy.context.active_object.data.polygons['index_number'].normal' . For the cylinder I also have cylinder angle by 'rotation_euler'. In Attached photo the green one is fine. but the red one seems not perpendicular to the face. Anyone can help? Thanks.

figure 1

dip deb
  • 35
  • 1
  • 6
  • I did not find related questions, if any then request to give link. – dip deb Aug 16 '21 at 08:40
  • To little information. How do you get polygon number? How do you run the script? Why don't you use snap instruments? – Crantisz Aug 16 '21 at 08:44
  • That I can handle. Just run a for loop and find out new polygons with area range. For a particular polygon or face its position and normal is fixed. So using that information I want to set cylinder with perpendicular to that specific face. – dip deb Aug 16 '21 at 09:10
  • Hello ! It may be your "get_perpendicular_angle" (or however you named it) function has a flaw. Could you post a snippet of the algorithm ? Also, you almost certainly will hit a roadblock once you start rotating or moving your objects in world space. Make sure you familiarize yourself with object.matrix_world which is used to transform from local to global space and vice versa – Gorgious Aug 16 '21 at 09:13
  • 1
    Do you change the position also? Just to make sure that correct face has been picked? – Crantisz Aug 16 '21 at 09:13
  • 'Gorgious' I did not use matrix. According to your comment it seems a little bit difficult. – dip deb Aug 16 '21 at 09:21
  • "Crantisz" face selection is not problem. I can save their index number in a list and using that number to get specific face position to manipulate the position of cylinder when necessary. – dip deb Aug 16 '21 at 09:22
  • Probable dupe https://blender.stackexchange.com/questions/19533/align-object-to-vector-using-python https://blender.stackexchange.com/a/167514/15543 https://blender.stackexchange.com/questions/27491/python-vertex-normal-according-to-world – batFINGER Aug 16 '21 at 09:52

1 Answers1

1

According to batFINGER comment, I checked those posts and try some variation and found my expected solution. I used .normal.to_track_quat('Z','X') for each face to make it perpendicular and convert it .to_euler() for each face (or polygon in python). For example, if I want the cylinder to be perpendicular to face number 116 then code will like,

polygon = bpy.data.objects['Circle'].data.polygons

cylinder = bpy.data.objects['Cylinder'] cylinder.location = polygon[116].center

cylinder.rotation_euler = polygon[116].normal.to_track_quat('Z','X').to_euler()

enter image description here

dip deb
  • 35
  • 1
  • 6