From 93d3b64772396f32b78d60536105f5bff7a0512f Mon Sep 17 00:00:00 2001 From: Thomas Barlow Date: Sun, 14 Jan 2024 02:12:38 +0000 Subject: [PATCH 1/3] Fix: Add Mesh Extra Objects: Inverted normals of solids when vertex positions are inverted A number of cases of `add_mesh_solid.createSolid` invert the vertex positions, but this turns the solid inside-out, causing the normals to be flipped. This is fixed by also reversing the order of the vertices of each face in these cases, which results in their normals being flipped. --- add_mesh_extra_objects/add_mesh_solid.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/add_mesh_extra_objects/add_mesh_solid.py b/add_mesh_extra_objects/add_mesh_solid.py index ef9cc541d..1f478a323 100644 --- a/add_mesh_extra_objects/add_mesh_solid.py +++ b/add_mesh_extra_objects/add_mesh_solid.py @@ -51,6 +51,11 @@ def vSum(list): return reduce(lambda a, b: a + b, list) +# Get a copy of the input faces, but with the normals flipped by reversing the order of the vertex indices of each face. +def flipped_face_normals(faces): + return [list(reversed(vertex_indices)) for vertex_indices in faces] + + # creates the 5 platonic solids as a base for the rest # plato: should be one of {"4","6","8","12","20"}. decides what solid the # outcome will be. @@ -146,7 +151,8 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): vInput, fInput = source(dualSource[plato]) supposedSize = vSum(vInput[i] for i in fInput[0]).length / len(fInput[0]) vInput = [-i * supposedSize for i in vInput] # mirror it - return vInput, fInput + # Inverting vInput turns the mesh inside-out, so normals need to be flipped. + return vInput, flipped_face_normals(fInput) return source(plato) elif 0 < vtrunc <= 0.5: # simple truncation of the source vInput, fInput = source(plato) @@ -161,7 +167,8 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): vInput = [i * supposedSize for i in vInput] return vInput, fInput vInput = [-i * supposedSize for i in vInput] - return vInput, fInput + # Inverting vInput turns the mesh inside-out, so normals need to be flipped. + return vInput, flipped_face_normals(fInput) # generate connection database vDict = [{} for i in vInput] @@ -269,6 +276,10 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): if supposedSize and not dual: # this to make the vtrunc > 1 work supposedSize *= len(fvOutput[0]) / vSum(vOutput[i] for i in fvOutput[0]).length vOutput = [-i * supposedSize for i in vOutput] + # Inverting vOutput turns the mesh inside-out, so normals need to be flipped. + flip_normals = True + else: + flip_normals = False # create new faces by replacing old vert IDs by newly generated verts ffOutput = [[] for i in fInput] @@ -287,7 +298,10 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): ffOutput[x].append(fvOutput[i][vData[i][3].index(x) - 1]) if not dual: - return vOutput, fvOutput + feOutput + ffOutput + fOutput = fvOutput + feOutput + ffOutput + if flip_normals: + fOutput = flipped_face_normals(fOutput) + return vOutput, fOutput else: # do the same procedure as above, only now on the generated mesh # generate connection database -- 2.30.2 From f07fee1b0b6273eb87038db49cb93f5e21609496 Mon Sep 17 00:00:00 2001 From: Thomas Barlow Date: Mon, 15 Jan 2024 01:45:19 +0000 Subject: [PATCH 2/3] Match function/variable naming style of the surrounding code --- add_mesh_extra_objects/add_mesh_solid.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/add_mesh_extra_objects/add_mesh_solid.py b/add_mesh_extra_objects/add_mesh_solid.py index 1f478a323..b25d00156 100644 --- a/add_mesh_extra_objects/add_mesh_solid.py +++ b/add_mesh_extra_objects/add_mesh_solid.py @@ -52,8 +52,8 @@ def vSum(list): # Get a copy of the input faces, but with the normals flipped by reversing the order of the vertex indices of each face. -def flipped_face_normals(faces): - return [list(reversed(vertex_indices)) for vertex_indices in faces] +def flippedFaceNormals(faces): + return [list(reversed(vertexIndices)) for vertexIndices in faces] # creates the 5 platonic solids as a base for the rest @@ -152,7 +152,7 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): supposedSize = vSum(vInput[i] for i in fInput[0]).length / len(fInput[0]) vInput = [-i * supposedSize for i in vInput] # mirror it # Inverting vInput turns the mesh inside-out, so normals need to be flipped. - return vInput, flipped_face_normals(fInput) + return vInput, flippedFaceNormals(fInput) return source(plato) elif 0 < vtrunc <= 0.5: # simple truncation of the source vInput, fInput = source(plato) @@ -168,7 +168,7 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): return vInput, fInput vInput = [-i * supposedSize for i in vInput] # Inverting vInput turns the mesh inside-out, so normals need to be flipped. - return vInput, flipped_face_normals(fInput) + return vInput, flippedFaceNormals(fInput) # generate connection database vDict = [{} for i in vInput] @@ -277,9 +277,9 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): supposedSize *= len(fvOutput[0]) / vSum(vOutput[i] for i in fvOutput[0]).length vOutput = [-i * supposedSize for i in vOutput] # Inverting vOutput turns the mesh inside-out, so normals need to be flipped. - flip_normals = True + flipNormals = True else: - flip_normals = False + flipNormals = False # create new faces by replacing old vert IDs by newly generated verts ffOutput = [[] for i in fInput] @@ -299,8 +299,8 @@ def createSolid(plato, vtrunc, etrunc, dual, snub): if not dual: fOutput = fvOutput + feOutput + ffOutput - if flip_normals: - fOutput = flipped_face_normals(fOutput) + if flipNormals: + fOutput = flippedFaceNormals(fOutput) return vOutput, fOutput else: # do the same procedure as above, only now on the generated mesh -- 2.30.2 From 759322ad711f1a8de6082d6b80ecd5a770c89f95 Mon Sep 17 00:00:00 2001 From: Thomas Barlow Date: Tue, 16 Jan 2024 20:36:59 +0000 Subject: [PATCH 3/3] Increase Add Mesh Extra Objects version --- add_mesh_extra_objects/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/add_mesh_extra_objects/__init__.py b/add_mesh_extra_objects/__init__.py index 98e412aac..a5d3c9d5b 100644 --- a/add_mesh_extra_objects/__init__.py +++ b/add_mesh_extra_objects/__init__.py @@ -13,7 +13,7 @@ bl_info = { "name": "Extra Objects", "author": "Multiple Authors", - "version": (0, 3, 9), + "version": (0, 3, 10), "blender": (2, 80, 0), "location": "View3D > Add > Mesh", "description": "Add extra mesh object types", -- 2.30.2