From bbd23db44df965bccbe7f8fd87398d65020d54c9 Mon Sep 17 00:00:00 2001 From: SE2Dev Date: Sun, 2 Apr 2017 18:24:33 -0400 Subject: [PATCH] Add Support for Normalizing Bone Weights --- xmodel.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/xmodel.py b/xmodel.py index 85591b8..538385c 100644 --- a/xmodel.py +++ b/xmodel.py @@ -1,5 +1,6 @@ from itertools import repeat from time import strftime +from math import sqrt def __clamp_float__(value, range=(-1.0, 1.0)): @@ -10,6 +11,11 @@ def __clamp_multi__(value, range=(-1.0, 1.0)): return tuple([max(min(v, range[1]), range[0]) for v in value]) +def __normalized__(iterable): + d = 1.0 / sqrt(sum([v * v for v in iterable])) + return [v * d for v in iterable] + + def deserialize_image_string(ref_string): if len(ref_string) == 0: return {"color": "$none.tga"} @@ -600,6 +606,14 @@ def __load_materials__(self, file, version): return lines_read + def normalize_weights(self): + """ + Normalize the bone weights for all verts (in all meshes) + """ + for mesh in self.meshes: + for vert in mesh.verts: + vert.weights = __normalized__(vert.weights) + def LoadFile(self, path, split_meshes=True): file = open(path, "r") # file automatically keeps track of what line its on across calls