X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=blender%2Fio_mspgl%2Fmesh.py;h=077acc2e57014227a652ad64ff268061b18c1e7b;hb=3b14c996f1249a3e8660a699bb9e16815344bdf2;hp=3ad3cecc7c570d47b3d8cad22c3dbf0949c11b83;hpb=af4769679fc41cfd95bb12777423f7672ba5f661;p=libs%2Fgl.git diff --git a/blender/io_mspgl/mesh.py b/blender/io_mspgl/mesh.py index 3ad3cecc..077acc2e 100644 --- a/blender/io_mspgl/mesh.py +++ b/blender/io_mspgl/mesh.py @@ -1,3 +1,4 @@ +import bpy import math import mathutils @@ -13,6 +14,7 @@ class Edge: else: self._medge = me self.smooth = False + self.key = me.key self.faces = [] def __getattr__(self, attr): @@ -45,19 +47,21 @@ class Vertex: def __init__(self, mv): if mv.__class__==Vertex: self._mvert = mv._mvert - self.normal = mv.normal self.uvs = mv.uvs[:] self.tan = mv.tan self.bino = mv.bino - self.group_weight_scale = mv.group_weight_scale else: self._mvert = mv self.uvs = [] self.tan = None self.bino = None - self.group_weight_scale = 1 + self.index = mv.index + self.co = mv.co + self.normal = mv.normal self.flag = False + self.edges = [] self.faces = [] + self.groups = mv.groups[:] def __getattr__(self, attr): return getattr(self._mvert, attr) @@ -68,9 +72,20 @@ class Vertex: return cmp(self.index, other.index) +class VertexGroup: + def __init__(self, base): + self._base = base + self.group = base.group + self.weight = base.weight + + def __getattr__(self, attr): + return getattr(self._mvert, attr) + + class Face: def __init__(self, mf): self._mface = mf + self.index = mf.index self.edges = [] self.vertices = mf.vertices[:] self.uvs = [] @@ -84,6 +99,10 @@ class Face: return 1 return cmp(self.index, other.index) + def pivot_vertex(self, v): + n = self.vertices.index(v) + return [(n+i)%len(self.vertices) for i in range(len(self.vertices))] + def pivot_vertices(self, *vt): flags = [(v in vt) for v in self.vertices] l = len(self.vertices) @@ -98,6 +117,11 @@ class Face: return e raise KeyError("No edge %s"%(key,)) + def other_edge(self, e, v): + for d in self.edges: + if d!=e and v in d.vertices: + return d + def get_neighbors(self): neighbors = [e.other_face(self) for e in self.edges] return list(filter(bool, neighbors)) @@ -111,12 +135,18 @@ class Line: class UvLayer: - def __init__(self, l, t): - self._layer = l - self.uvtex = t - self.name = self.uvtex.name + def __init__(self, arg): + if type(arg)==str: + self._layer = None + self.name = arg + else: + self._layer = arg + self.name = arg.name + self.uvs = [d.uv for d in self.data] + self.unit = None self.hidden = False + dot = self.name.find('.') if dot>=0: ext = self.name[dot:] @@ -128,56 +158,77 @@ class UvLayer: def __getattr__(self, attr): return getattr(self._layer, attr) -class FakeUvLayer: - def __init__(self, n): - self.uvtex = None - self.name = n - self.unit = None - self.hidden = False class Mesh: def __init__(self, m): self._mesh = m + self.winding_test = m.winding_test + self.tbn_vecs = m.tbn_vecs + self.vertex_groups = m.vertex_groups + self.vertices = [Vertex(v) for v in self.vertices] self.faces = [Face(f) for f in self.polygons] + self.edges = [Edge(e) for e in self.edges] + self.loops = self.loops[:] self.materials = self.materials[:] + if self.use_uv=='NONE' or not self.uv_layers: + self.uv_layers = [] + else: + self.uv_layers = [UvLayer(u) for u in self.uv_layers] + self.uv_layers = sorted([u for u in self.uv_layers if not u.hidden], key=(lambda u: (u.unit or 1000, u.name))) + + if self.use_uv=='UNIT0': + self.uv_layers = [self.uv_layers[0]] - self.uv_layers = [UvLayer(self.uv_layers[i], self.uv_textures[i]) for i in range(len(self.uv_layers))] - self.assign_texture_units() + next_unit = max((u.unit+1 for u in self.uv_layers if u.unit is not None), default=0) + for u in self.uv_layers: + if not u.unit: + u.unit = next_unit + next_unit += 1 + for v in self.vertices: + v.groups = [VertexGroup(g) for g in v.groups] + + 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") + f.vertices = [self.vertices[i] for i in f.vertices] for v in f.vertices: v.faces.append(f) - for u in self.uv_layers: - f.uvs.append([u.data[f.loop_indices[i]].uv for i in range(len(f.vertices))]) - self.edges = dict([(e.key, Edge(e)) for e in self.edges]) - for f in self.faces: for k in f.edge_keys: - e = self.edges[k] - e.faces.append(self.faces[f.index]) + e = edge_map[k] + e.faces.append(f) f.edges.append(e) - self.lines = [Line(e) for e in self.edges.values() if not e.faces] - for l in self.lines: - l.vertices = [self.vertices[i] for i in l.vertices] + for e in self.edges: + e.vertices = [self.vertices[i] for i in e.vertices] + for v in e.vertices: + v.edges.append(e) - if self.use_auto_smooth: - smooth_limit = math.cos(self.auto_smooth_angle) + if self.use_lines: + self.lines = [Line(e) for e in self.edges if not e.faces] else: - smooth_limit = -1 - - for e in self.edges.values(): - e.vertices = [self.vertices[i] for i in e.vertices] - e.check_smooth(smooth_limit) + self.lines = [] def __getattr__(self, attr): return getattr(self._mesh, attr) + def transform(self, matrix): + for v in self.vertices: + v.co = matrix*v.co + def splice(self, other): + if len(self.uv_layers)!=len(other.uv_layers): + raise ValueError("Meshes have incompatible UV layers") + for i, u in enumerate(self.uv_layers): + if u.name!=other.uv_layers[i].name: + raise ValueError("Meshes have incompatible UV layers") + material_map = [] for m in other.materials: if m in self.materials: @@ -186,195 +237,261 @@ class Mesh: material_map.append(len(self.materials)) self.materials.append(m) + for i, u in enumerate(self.uv_layers): + u.uvs += other.uv_layers[i].uvs + offset = len(self.vertices) - for v in other.vertices: + self.vertices += other.vertices + for v in self.vertices[offset:]: v.index += offset - self.vertices.append(v) + + loop_offset = len(self.loops) + self.loops += other.loops offset = len(self.faces) - for f in other.faces: + self.faces += other.faces + for f in self.faces[offset:]: f.index += offset + f.loop_start += loop_offset + f.loop_indices = range(f.loop_start, f.loop_start+f.loop_total) if other.materials: f.material_index = material_map[f.material_index] - self.faces.append(f) - for e in other.edges.values(): + offset = len(self.edges) + self.edges += other.edges + for e in self.edges[offset:]: + e.index += offset e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index) - self.edges[e.key] = e self.lines += other.lines - def flatten_faces(self): - for f in self.faces: - f.use_smooth = False + def prepare_smoothing(self, progress): + smooth_limit = -1 + if self.smoothing=='NONE': + for f in self.faces: + f.use_smooth = False - for e in self.edges.values(): - e.check_smooth(1) + smooth_limit = 1 + elif self.use_auto_smooth: + smooth_limit = math.cos(self.auto_smooth_angle) + + for e in self.edges: + e.check_smooth(smooth_limit) + + progress.push_task("Sharp edges", 0.0, 0.7) + self.split_vertices(self.find_smooth_group, progress) + + if self.smoothing!='BLENDER': + progress.set_task("Updating normals", 0.7, 1.0) + self.compute_normals(progress) + + progress.pop_task() + + def prepare_vertex_groups(self, obj): + for v in self.vertices: + if v.groups: + weight_sum = sum(g.weight for g in v.groups) + v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)[:self.max_groups_per_vertex] + weight_scale = weight_sum/sum(g.weight for g in v.groups) + for g in v.groups: + g.weight *= weight_scale + + if obj.parent and obj.parent.type=="ARMATURE": + armature = obj.parent.data + bone_indices = {b.name: i for i, b in enumerate(armature.bones)} + group_index_map = {i: i for i in range(len(obj.vertex_groups))} + for g in first_obj.vertex_groups: + if g.name in bone_indices: + group_index_map[g.index] = bone_indices[g.name] + + for v in self.vertices: + for g in v.groups: + g.group = group_index_map[g.group] + + def prepare_uv(self, obj, progress): + if obj.material_tex and self.use_uv!='NONE': + layer = UvLayer("material_tex") + + if self.use_uv=='UNIT0': + self.uv_layers = [layer] + layer.unit = 0 + else: + self.uv_layers.append(layer) + layer.unit = max((u.unit+1 for u in self.uv_layers if u.unit is not None), default=0) + + layer.uvs = [None]*len(self.loops) + for f in self.faces: + uv = mathutils.Vector(((f.material_index+0.5)/len(self.materials), 0.5)) + for i in f.loop_indices: + layer.uvs[i] = uv + + array_uv_layers = [t.uv_layer for m in self.materials if m.array_atlas for t in m.texture_slots if t and t.texture_coords=='UV'] + array_uv_layers = [u for u in self.uv_layers if u.name in array_uv_layers] + + if array_uv_layers: + for f in self.faces: + layer = 0 + if f.material_index