Skip to content

Commit 0ce78d1

Browse files
committed
custom normals export: Improve performance by using foreach_get()
1 parent 5ae8de0 commit 0ce78d1

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

export/mesh_converter.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numpy as np
2+
13
import bpy
24
from contextlib import contextmanager
35
from time import time
@@ -24,19 +26,14 @@ def get_custom_normals_slow(mesh):
2426
if not mesh.has_custom_normals:
2527
return None
2628

27-
# Flat list of 3-element normal vectors
28-
custom_normals = [0] * (len(mesh.loops) * 3)
29-
30-
for loop_tri in mesh.loop_triangles:
31-
loops = loop_tri.loops
32-
split_normals = loop_tri.split_normals
33-
34-
start = loops[0] * 3
35-
custom_normals[start:start + 3] = split_normals[0][:]
36-
start = loops[1] * 3
37-
custom_normals[start:start + 3] = split_normals[1][:]
38-
start = loops[2] * 3
39-
custom_normals[start:start + 3] = split_normals[2][:]
29+
# Note: for readability, split_normals_array should be of shape (n_loops, 3),
30+
# where each row is a normal vector.
31+
# However, foreach_get() needs a flat sequence,
32+
# so to save two ravel() operations, a flat array is used directly
33+
n_loops = len(mesh.loops)
34+
split_normals_array = np.empty(n_loops * 3)
35+
mesh.loops.foreach_get('normal', split_normals_array)
36+
custom_normals = split_normals_array.tolist() # currently, LuxCore is hard-coded to expect a list.
4037

4138
return custom_normals
4239

0 commit comments

Comments
 (0)