Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CGArtPython/1f294c42d72583e4c766ed7ed7454608 to your computer and use it in GitHub Desktop.
Save CGArtPython/1f294c42d72583e4c766ed7ed7454608 to your computer and use it in GitHub Desktop.
This is an example Blender Python script that shows how you can recalculate the normals (based on this tutorial https://www.youtube.com/watch?v=mN3n9b98HMk&lc=UgzNYFhB4FXUifXe1ml4AaABAg)
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),
(0, 2, 3),
(6, 5, 4),
(7, 6, 4),
(4, 5, 1),
(4, 1, 0),
(7, 4, 0),
(7, 0, 3),
(6, 7, 3),
(6, 3, 2),
(5, 6, 2),
(1, 2, 5), # example of an incorrect facing face, this should be (5, 2, 1)
]
edges = []
# create a mesh from the vert, edge, and face data
mesh_data = bpy.data.meshes.new("cube_data")
mesh_data.from_pydata(verts, edges, faces)
# create a object using the mesh data
mesh_obj = bpy.data.objects.new("cube_object", mesh_data)
# add the object into the current scene
bpy.context.collection.objects.link(mesh_obj)
# set the mesh object as the active object
bpy.context.view_layer.objects.active = mesh_obj
# switch to Edit Mode
bpy.ops.object.mode_set(mode='EDIT')
# recalculate the normals
bpy.ops.mesh.normals_make_consistent(inside=False)
# switch to Object Mode
bpy.ops.object.mode_set(mode='OBJECT')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment