I want to use bpy
module to create a polygon in blender.
However, I have some problem with the normal of faces.
See the following example (adopt from this post):
Works:
import bpy
verts = [(1.0, 1.0, -1.0),
(1.0, -1.0, -1.0),
(-1.0, -1.0, -1.0),
(-1.0, 1.0, -1.0),
(1.0, 1.0, 1.0),
(1.0, -1.0, 1.0),
(-1.0, -1.0, 1.0),
(-1.0, 1.0, 1.0)]
faces = [(0, 1, 2, 3),
(4, 7, 6, 5),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7)]
mesh = bpy.data.meshes.new('CubeMesh')
mesh.from_pydata(verts, [], faces)
mesh.update()
cube = bpy.data.objects.new('Cube', mesh)
scn = bpy.context.scene
scn.objects.link(cube)
The normals in the edit mode are correctly outward.
Then, I did some experiment on the same example code like this:
Don't Work:
import bpy
verts = [(1.0, 1.0, -1.0),
(1.0, -1.0, -1.0),
(-1.0, -1.0, -1.0),
(-1.0, 1.0, -1.0),
(1.0, 1.0, 1.0),
(1.0, -1.0, 1.0),
(-1.0, -1.0, 1.0),
(-1.0, 1.0, 1.0)]
faces = [(0, 1, 2, 3),
(4, 5, 6, 7), # (4, 7, 6, 5) --> (4, 5, 6, 7)
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7)]
mesh = bpy.data.meshes.new('CubeMesh')
mesh.from_pydata(verts, [], faces)
mesh.update()
cube = bpy.data.objects.new('Cube', mesh)
scn = bpy.context.scene
scn.objects.link(cube)
If you copy and paste above code, you will see the face defined by (4, 5, 6, 7)
flipped down.
It seems like the order of the vertices which define a face are critical on determining the direction of the normal.
My question is then how Blender internally determines the direction of normal of a face given the vertices? Is it the right-hand rule or something else? If it is so, is there any helper function or module that can help me with ensuring an outward normal of a face with respect to the world origin (0, 0, 0)
?
Thanks.