# how to find the angles to set the cylinder direction as perpendicular to the faces(polygons in python) of an object? [duplicate]

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.

• I did not find related questions, if any then request to give link. Aug 16, 2021 at 8:40
• To little information. How do you get polygon number? How do you run the script? Why don't you use snap instruments? Aug 16, 2021 at 8: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. Aug 16, 2021 at 9: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 Aug 16, 2021 at 9:13
• Do you change the position also? Just to make sure that correct face has been picked? Aug 16, 2021 at 9:13

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()