if vertex.__class__==Vertex:
self.uvs = vertex.uvs[:]
self.tan = vertex.tan
- self.bino = vertex.bino
else:
self.uvs = []
self.tan = None
- self.bino = None
self.index = vertex.index
self.co = mathutils.Vector(vertex.co)
self.normal = mathutils.Vector(vertex.normal)
self.winding_test = mesh.winding_test
self.smoothing = mesh.smoothing
self.use_uv = mesh.use_uv
- self.tbn_vecs = mesh.tbn_vecs
- self.tbn_uvtex = mesh.tbn_uvtex
+ self.tangent_uvtex = mesh.tangent_uvtex
+ self.use_strips = mesh.use_strips
self.vertex_groups = mesh.vertex_groups
# Clone basic data
edge_map = {e.key: e for e in self.edges}
for f in self.faces:
if len(f.vertices)>4:
- raise ValueError("Ngons are not supported")
+ raise ValueError("Unsupported face on mesh {}: N-gon".format(self.name))
f.vertices = [self.vertices[i] for i in f.vertices]
for v in f.vertices:
else:
self.lines = []
+ # Check if tangent vectors are needed
+ if mesh.tangent_vecs=='NO':
+ self.tangent_vecs = False
+ elif mesh.tangent_vecs=='YES':
+ self.tangent_vecs = True
+ elif mesh.tangent_vecs=='AUTO':
+ from .material import Material
+ self.tangent_vecs = False
+ for m in self.materials:
+ mat = Material(m)
+ if mat.type=="pbr":
+ normal_prop = next((p for p in mat.properties if p.tex_keyword=="normal_map"), None)
+ if normal_prop and normal_prop.texture:
+ self.tangent_vecs = True
+
self.vertex_sequence = []
def transform(self, matrix):
def splice(self, other):
if len(self.uv_layers)!=len(other.uv_layers):
- raise ValueError("Meshes have incompatible UV layers")
+ raise ValueError("Meshes {} and {} have incompatible UV layers".format(self.name, other.name))
for i, u in enumerate(self.uv_layers):
if u.name!=other.uv_layers[i].name:
- raise ValueError("Meshes have incompatible UV layers")
+ raise ValueError("Meshes {} and {} have incompatible UV layers".format(self.name, other.name))
# Merge materials and form a lookup from source material indices to the
# merged material list
def apply_material_atlas(self, material_atlas):
for m in self.materials:
if m.name not in material_atlas.material_names:
- raise Exception("Material atlas is not compatible with Mesh")
+ raise Exception("Material atlas {} is not compatible with Mesh {}".format(material_atlas.name, self.name))
if self.use_uv=='NONE':
return
prog_count = len(self.uv_layers)
prog_step = 0
- # Split by the UV layer used for TBN vectors first so connectivity
- # remains intact for TBN vector computation
- tbn_layer_index = -1
- if self.tbn_vecs:
- if self.tbn_uvtex:
+ # Split by the UV layer used for tangent vectors first so connectivity
+ # remains intact for tangent vector computation
+ tangent_layer_index = -1
+ if self.tangent_vecs:
+ if self.tangent_uvtex:
uv_names = [u.name for u in self.uv_layers]
- if self.tbn_uvtex in uv_names:
- tbn_layer_index = uv_names.index(self.tbn_uvtex)
+ if self.tangent_uvtex in uv_names:
+ tangent_layer_index = uv_names.index(self.tangent_uvtex)
elif self.uv_layers[0].unit==0:
- tbn_layer_index = 0
+ tangent_layer_index = 0
- if tbn_layer_index>=0:
+ if tangent_layer_index>=0:
prog_count += 1
- progress.push_task_slice("Computing TBN", 0, prog_count)
- self.split_vertices(self.find_uv_group, progress, tbn_layer_index)
- progress.set_task_slice(self.tbn_uvtex, 1, prog_count)
- self.compute_tbn(tbn_layer_index, progress)
+ progress.push_task_slice("Computing tangents", 0, prog_count)
+ self.split_vertices(self.find_uv_group, progress, tangent_layer_index)
+ progress.set_task_slice(self.tangent_uvtex, 1, prog_count)
+ self.compute_tangents(tangent_layer_index, progress)
progress.pop_task()
prog_step = 2
else:
- raise Exception("TBN UV layer not found")
+ raise Exception("Invalid configuration on mesh {}: No tangent UV layer".format(self.name))
# Split by the remaining UV layers
for i, u in enumerate(self.uv_layers):
- if i==tbn_layer_index:
+ if i==tangent_layer_index:
continue
progress.push_task_slice(u.name, prog_step, prog_count)
progress.set_progress(i/len(self.vertices))
- def compute_tbn(self, index, progress):
- # This function is called at an early stage during UV preparation when
- # face UVs are not available yet
+ def compute_tangents(self, index, progress):
layer_uvs = self.uv_layers[index].uvs
for i, v in enumerate(self.vertices):
v.tan = mathutils.Vector()
- v.bino = mathutils.Vector()
for f in v.faces:
vi = f.pivot_vertex(v)
uv0 = layer_uvs[f.loop_indices[vi[0]]]
if div:
mul = edge1.angle(edge2)/div
v.tan += (edge1*dv2-edge2*dv1)*mul
- v.bino += (edge2*du1-edge1*du2)*mul
if v.tan.length:
v.tan.normalize()
- if v.bino.length:
- v.bino.normalize()
progress.set_progress(i/len(self.vertices))
e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
-def create_mesh_from_object(context, obj, progress, *, material_atlas=None):
+def create_mesh_from_object(context, obj, material_atlas, progress):
if obj.type!="MESH":
- raise Exception("Object is not a mesh")
+ raise Exception("Object {} is not a mesh".format(obj.name))
progress.push_task("Preparing mesh", 0.0, 0.2)
bmesh.vertex_groups = o.data.vertex_groups
bmesh.max_groups_per_vertex = o.data.max_groups_per_vertex
bmesh.use_uv = o.data.use_uv
- bmesh.tbn_vecs = o.data.tbn_vecs
- bmesh.tbn_uvtex = o.data.tbn_uvtex
+ bmesh.tangent_vecs = o.data.tangent_vecs
+ bmesh.tangent_uvtex = o.data.tangent_uvtex
me = Mesh(bmesh)
me.transform(m)