From 97253368233bda799490e203fa886dc9dfb78203 Mon Sep 17 00:00:00 2001 From: Jammyjamjamman Date: Tue, 13 Apr 2021 01:33:57 +0100 Subject: [PATCH 1/2] G3D B290: Better vert compare * Use hashset for more efficient vert comparison. (Suggested by Titi.) * Create line of verts for basis mesh, for rare cases where there's no mesh frame where all the verts are different. * Fix bug in animation where all animation frames shifted by 1 (and last frame was not included in the animation). --- source/tools/glexemel/g3d_support_b290.py | 36 +++++++++++++---------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/source/tools/glexemel/g3d_support_b290.py b/source/tools/glexemel/g3d_support_b290.py index 281d7779..a2b4f8c3 100644 --- a/source/tools/glexemel/g3d_support_b290.py +++ b/source/tools/glexemel/g3d_support_b290.py @@ -403,15 +403,14 @@ def createMesh(filename, header, data, toblender, operator): for x in range(0, header.framecount): n_diff_verts = 0 # Current number of "different" vertices. # Find nubmer of different verts + diff_verts = set() + n_diff_verts = 0 for i in range(0, header.vertexcount * 3, 3): - vert_cmp = data.vertices[x * header.vertexcount * 3 + i:x * header.vertexcount * 3 + i + 3] - vert_diff = True - for j in range(i + 3, header.vertexcount * 3, 3): - if (data.vertices[x * header.vertexcount * 3 + j:x * header.vertexcount * 3 + j + 3] == vert_cmp): - vert_diff = False - break - n_diff_verts += vert_diff - + vert = tuple(data.vertices[x * header.vertexcount * 3 + i:x * header.vertexcount * 3 + i + 3]) + if (vert not in diff_verts): + n_diff_verts += 1 + diff_verts.add(vert) + if n_diff_verts == header.vertexcount: # if we've found a frame where all the verts are different, we don't need to do anymore searching. header_most_diff = x @@ -423,12 +422,19 @@ def createMesh(filename, header, data, toblender, operator): # the previous match, this frame is the new max. n_max_diff = n_diff_verts header_most_diff = x - - # Get the Vertices and Normals into empty Mesh - for x in range(0, header.vertexcount * 3, 3): - vertsCO.append(tuple(data.vertices[header_most_diff*header.vertexcount*3+x:header_most_diff*header.vertexcount*3+x+3])) - vertsNormal.extend([(data.normals[x], data.normals[x + 1], - data.normals[x + 2])]) + + if n_max_diff == header.vertexcount: + # Get the Vertices and Normals into empty Mesh + for x in range(0, header.vertexcount * 3, 3): + vertsCO.append(tuple(data.vertices[header_most_diff*header.vertexcount*3+x:header_most_diff*header.vertexcount*3+x+3])) + vertsNormal.extend([(data.normals[x], data.normals[x + 1], + data.normals[x + 2])]) + else: + operator.report({'WARNING'}, "Could not identify basis keyframe. Line of vertices used for import instead.\n(If no mesh is visible, please check other keyframes.)") + for x in range(0, header.vertexcount * 3, 3): + vertsCO.append((x*0.01, 0, 0)) + vertsNormal.extend([(data.normals[x], data.normals[x + 1], + data.normals[x + 2])]) faces = [] faceuv = [] @@ -548,7 +554,7 @@ def createMesh(filename, header, data, toblender, operator): # activate one shapekey per frame for i in range(0, header.framecount): - shape = mesh.shape_keys.key_blocks[i] + shape = mesh.shape_keys.key_blocks[i+1] shape.value = 0.0 shape.keyframe_insert("value", frame=i) shape.value = 1.0 From 86905dc6f7a78a6c33d03716733c8758dfdd690d Mon Sep 17 00:00:00 2001 From: Jammyjamjamman Date: Thu, 15 Apr 2021 20:16:44 +0100 Subject: [PATCH 2/2] G3D B290: remove line generating mesh code Line was generated for meshes with duplicate verts, to prevent loss of vertices. But this was a problem because some duplicate verts need removing. (E.g. some duplicate verts are generated for UV mapping. These should be removed during import.) This does mean the g3d script can lose information on import. --- source/tools/glexemel/g3d_support_b290.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/source/tools/glexemel/g3d_support_b290.py b/source/tools/glexemel/g3d_support_b290.py index a2b4f8c3..8aa78619 100644 --- a/source/tools/glexemel/g3d_support_b290.py +++ b/source/tools/glexemel/g3d_support_b290.py @@ -398,8 +398,10 @@ def createMesh(filename, header, data, toblender, operator): # different values in any frame. That could still cause problems. # Figure out a better solution :p (e.g. moving verts to random # different positions). + # (We tried putting the verts in a line, to prevent merging. + # This was a problem because there are some verts that need merging.) header_most_diff = 0 # This is the header we will use to define the mesh. - n_max_diff = 0 # Max number of "different" vertices found in a frame so far. + n_max_diff = 0 # Max number of "different" vertices found in the frame. for x in range(0, header.framecount): n_diff_verts = 0 # Current number of "different" vertices. # Find nubmer of different verts @@ -423,18 +425,11 @@ def createMesh(filename, header, data, toblender, operator): n_max_diff = n_diff_verts header_most_diff = x - if n_max_diff == header.vertexcount: - # Get the Vertices and Normals into empty Mesh - for x in range(0, header.vertexcount * 3, 3): - vertsCO.append(tuple(data.vertices[header_most_diff*header.vertexcount*3+x:header_most_diff*header.vertexcount*3+x+3])) - vertsNormal.extend([(data.normals[x], data.normals[x + 1], - data.normals[x + 2])]) - else: - operator.report({'WARNING'}, "Could not identify basis keyframe. Line of vertices used for import instead.\n(If no mesh is visible, please check other keyframes.)") - for x in range(0, header.vertexcount * 3, 3): - vertsCO.append((x*0.01, 0, 0)) - vertsNormal.extend([(data.normals[x], data.normals[x + 1], - data.normals[x + 2])]) + # Get the Vertices and Normals into empty Mesh + for x in range(0, header.vertexcount * 3, 3): + vertsCO.append(tuple(data.vertices[header_most_diff*header.vertexcount*3+x:header_most_diff*header.vertexcount*3+x+3])) + vertsNormal.extend([(data.normals[x], data.normals[x + 1], + data.normals[x + 2])]) faces = [] faceuv = []