Skip to content

Commit

Permalink
Merge pull request #44 from neomonkeus/refactor/doc_tests
Browse files Browse the repository at this point in the history
Refactor/doc tests
  • Loading branch information
neomonkeus committed Sep 18, 2019
2 parents f2789ea + b8d7ea3 commit 440c61c
Show file tree
Hide file tree
Showing 60 changed files with 2,804 additions and 2,707 deletions.
89 changes: 60 additions & 29 deletions pyffi/formats/nif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ def __eq__(self, x):
if isinstance(x, type(None)):
return False
if not isinstance(x, NifFormat.Vector3):
raise TypeError("do not know how to compare Vector3 and %s"%x.__class__)
raise TypeError("do not know how to compare Vector3 and %s" % x.__class__)
if abs(self.x - x.x) > NifFormat.EPSILON: return False
if abs(self.y - x.y) > NifFormat.EPSILON: return False
if abs(self.z - x.z) > NifFormat.EPSILON: return False
Expand Down Expand Up @@ -2335,17 +2335,17 @@ class bhkLimitedHingeConstraint:
def apply_scale(self, scale):
"""Scale data."""
# apply scale on transform
self.sub_constraint.limited_hinge.pivot_a.x *= scale
self.sub_constraint.limited_hinge.pivot_a.y *= scale
self.sub_constraint.limited_hinge.pivot_a.z *= scale
self.sub_constraint.limited_hinge.pivot_b.x *= scale
self.sub_constraint.limited_hinge.pivot_b.y *= scale
self.sub_constraint.limited_hinge.pivot_b.z *= scale
self.limited_hinge.pivot_a.x *= scale
self.limited_hinge.pivot_a.y *= scale
self.limited_hinge.pivot_a.z *= scale
self.limited_hinge.pivot_b.x *= scale
self.limited_hinge.pivot_b.y *= scale
self.limited_hinge.pivot_b.z *= scale

def update_a_b(self, parent):
"""Update the B data from the A data. The parent argument is simply a
common parent to the entities."""
self.sub_constraint.limited_hinge.update_a_b(self.get_transform_a_b(parent))
self.limited_hinge.update_a_b(self.get_transform_a_b(parent))

class bhkListShape:
def get_mass_center_inertia(self, density = 1, solid = True):
Expand Down Expand Up @@ -2406,24 +2406,24 @@ class bhkMalleableConstraint:
def apply_scale(self, scale):
"""Scale data."""
# apply scale on transform
self.sub_constraint.ragdoll.pivot_a.x *= scale
self.sub_constraint.ragdoll.pivot_a.y *= scale
self.sub_constraint.ragdoll.pivot_a.z *= scale
self.sub_constraint.ragdoll.pivot_b.x *= scale
self.sub_constraint.ragdoll.pivot_b.y *= scale
self.sub_constraint.ragdoll.pivot_b.z *= scale
self.sub_constraint.limited_hinge.pivot_a.x *= scale
self.sub_constraint.limited_hinge.pivot_a.y *= scale
self.sub_constraint.limited_hinge.pivot_a.z *= scale
self.sub_constraint.limited_hinge.pivot_b.x *= scale
self.sub_constraint.limited_hinge.pivot_b.y *= scale
self.sub_constraint.limited_hinge.pivot_b.z *= scale
self.ragdoll.pivot_a.x *= scale
self.ragdoll.pivot_a.y *= scale
self.ragdoll.pivot_a.z *= scale
self.ragdoll.pivot_b.x *= scale
self.ragdoll.pivot_b.y *= scale
self.ragdoll.pivot_b.z *= scale
self.limited_hinge.pivot_a.x *= scale
self.limited_hinge.pivot_a.y *= scale
self.limited_hinge.pivot_a.z *= scale
self.limited_hinge.pivot_b.x *= scale
self.limited_hinge.pivot_b.y *= scale
self.limited_hinge.pivot_b.z *= scale

def update_a_b(self, parent):
"""Update the B data from the A data."""
transform = self.get_transform_a_b(parent)
self.sub_constraint.limited_hinge.update_a_b(transform)
self.sub_constraint.ragdoll.update_a_b(transform)
self.limited_hinge.update_a_b(transform)
self.ragdoll.update_a_b(transform)

class bhkMoppBvTreeShape:
def get_mass_center_inertia(self, density=1, solid=True):
Expand Down Expand Up @@ -4691,19 +4691,50 @@ def get_skin_deformation(self):
normals = [ NifFormat.Vector3() for i in range(self.data.num_vertices) ]
sumweights = [ 0.0 for i in range(self.data.num_vertices) ]
skin_offset = skindata.get_transform()
# store one transform & rotation per bone
bone_transforms = []
for i, bone_block in enumerate(skininst.bones):
bonedata = skindata.bone_list[i]
bone_offset = bonedata.get_transform()
bone_matrix = bone_block.get_transform(skelroot)
transform = bone_offset * bone_matrix * skin_offset
scale, rotation, translation = transform.get_scale_rotation_translation()
for skinweight in bonedata.vertex_weights:
index = skinweight.index
weight = skinweight.weight
vertices[index] += weight * (self.data.vertices[index] * transform)
if self.data.has_normals:
normals[index] += weight * (self.data.normals[index] * rotation)
sumweights[index] += weight
bone_transforms.append( (transform, rotation) )

# the usual case
if skindata.has_vertex_weights:
for i, bone_block in enumerate(skininst.bones):
bonedata = skindata.bone_list[i]
transform, rotation = bone_transforms[i]
for skinweight in bonedata.vertex_weights:
index = skinweight.index
weight = skinweight.weight
vertices[index] += weight * (self.data.vertices[index] * transform)
if self.data.has_normals:
normals[index] += weight * (self.data.normals[index] * rotation)
sumweights[index] += weight
# we must get weights from the partition
else:
skinpartition = skininst.skin_partition
for block in skinpartition.skin_partition_blocks:
# get transforms for this block
block_bone_transforms = [bone_transforms[i] for i in block.bones]

# go over each vert in this block
for vert_index, vertex_weights, bone_indices in zip(block.vertex_map,
block.vertex_weights,
block.bone_indices):
# skip verts that were already processed in an earlier block
if sumweights[vert_index] != 0.0:
continue
# go over all 4 weight / bone pairs and transform this vert
for weight, b_i in zip(vertex_weights, bone_indices):
if weight > 0.0:
transform, rotation = block_bone_transforms[b_i]
vertices[vert_index] += weight * (self.data.vertices[vert_index] * transform)
if self.data.has_normals:
normals[vert_index] += weight * (self.data.normals[vert_index] * rotation)
sumweights[vert_index] += weight

for i, s in enumerate(sumweights):
if abs(s - 1.0) > 0.01:
Expand Down
82 changes: 41 additions & 41 deletions pyffi/object_models/xml/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@

from pyffi.utils.graph import DetailNode, EdgeFilter


class _ListWrap(list, DetailNode):
"""A wrapper for list, which uses get_value and set_value for
getting and setting items of the basic type."""

def __init__(self, element_type, parent = None):
def __init__(self, element_type, parent=None):
self._parent = weakref.ref(parent) if parent else None
self._elementType = element_type
# we link to the unbound methods (that is, self.__class__.xxx
Expand Down Expand Up @@ -119,20 +120,21 @@ def get_detail_child_names(self, edge_filter=EdgeFilter()):
"""Yield child names."""
return ("[%i]" % row for row in range(list.__len__(self)))


class Array(_ListWrap):
"""A general purpose class for 1 or 2 dimensional arrays consisting of
either BasicBase or StructBase elements."""

logger = logging.getLogger("pyffi.nif.data.array")
arg = None # default argument
arg = None # default argument

def __init__(
self,
element_type = None,
element_type_template = None,
element_type_argument = None,
count1 = None, count2 = None,
parent = None):
self,
element_type=None,
element_type_template=None,
element_type_argument=None,
count1=None, count2=None,
parent=None):
"""Initialize the array type.
:param element_type: The class describing the type of each element.
Expand All @@ -147,10 +149,10 @@ def __init__(
array is an attribute of."""
if count2 is None:
_ListWrap.__init__(self,
element_type = element_type, parent = parent)
element_type=element_type, parent=parent)
else:
_ListWrap.__init__(self,
element_type = _ListWrap, parent = parent)
element_type=_ListWrap, parent=parent)
self._elementType = element_type
self._parent = weakref.ref(parent) if parent else None
self._elementTypeTemplate = element_type_template
Expand All @@ -161,32 +163,30 @@ def __init__(
if self._count2 is None:
for i in range(self._len1()):
elem_instance = self._elementType(
template = self._elementTypeTemplate,
argument = self._elementTypeArgument,
parent = self)
template=self._elementTypeTemplate,
argument=self._elementTypeArgument,
parent=self)
self.append(elem_instance)
else:
for i in range(self._len1()):
elem = _ListWrap(element_type = element_type, parent = self)
elem = _ListWrap(element_type=element_type, parent=self)
for j in range(self._len2(i)):
elem_instance = self._elementType(
template = self._elementTypeTemplate,
argument = self._elementTypeArgument,
parent = elem)
template=self._elementTypeTemplate,
argument=self._elementTypeArgument,
parent=elem)
elem.append(elem_instance)
self.append(elem)

def _len1(self):
"""The length the array should have, obtained by evaluating
the count1 expression."""
"""The length the array should have, obtained by evaluating the count1 expression."""
if self._parent is None:
return self._count1.eval()
else:
return self._count1.eval(self._parent())

def _len2(self, index1):
"""The length the array should have, obtained by evaluating
the count2 expression."""
"""The length the array should have, obtained by evaluating the count2 expression."""
if self._count2 is None:
raise ValueError('single array treated as double array (bug?)')
if self._parent is None:
Expand All @@ -199,8 +199,7 @@ def _len2(self, index1):
return expr[index1]

def deepcopy(self, block):
"""Copy attributes from a given array which needs to have at least as
many elements (possibly more) as self."""
"""Copy attributes from a given array which needs to have at least as many elements (possibly more) as self."""
if self._count2 is None:
for i in range(self._len1()):
attrvalue = self[i]
Expand Down Expand Up @@ -259,27 +258,27 @@ def update_size(self):
if new_size < old_size:
del self[new_size:old_size]
else:
for i in range(new_size-old_size):
for i in range(new_size - old_size):
elem = self._elementType(
template = self._elementTypeTemplate,
argument = self._elementTypeArgument)
template=self._elementTypeTemplate,
argument=self._elementTypeArgument)
self.append(elem)
else:
if new_size < old_size:
del self[new_size:old_size]
else:
for i in range(new_size-old_size):
for i in range(new_size - old_size):
self.append(_ListWrap(self._elementType))
for i, elemlist in enumerate(list.__iter__(self)):
old_size_i = len(elemlist)
new_size_i = self._len2(i)
if new_size_i < old_size_i:
del elemlist[new_size_i:old_size_i]
else:
for j in range(new_size_i-old_size_i):
for j in range(new_size_i - old_size_i):
elem = self._elementType(
template = self._elementTypeTemplate,
argument = self._elementTypeArgument)
template=self._elementTypeTemplate,
argument=self._elementTypeArgument)
elemlist.append(elem)

def read(self, stream, data):
Expand All @@ -298,22 +297,22 @@ def read(self, stream, data):
if self._count2 is None:
for i in range(len1):
elem = self._elementType(
template = self._elementTypeTemplate,
argument = self._elementTypeArgument,
parent = self)
template=self._elementTypeTemplate,
argument=self._elementTypeArgument,
parent=self)
elem.read(stream, data)
self.append(elem)
else:
for i in range(len1):
len2i = self._len2(i)
if len2i > 0x10000000:
raise ValueError('array too long (%i)' % len2i)
elemlist = _ListWrap(self._elementType, parent = self)
elemlist = _ListWrap(self._elementType, parent=self)
for j in range(len2i):
elem = self._elementType(
template = self._elementTypeTemplate,
argument = self._elementTypeArgument,
parent = elemlist)
template=self._elementTypeTemplate,
argument=self._elementTypeArgument,
parent=elemlist)
elem.read(stream, data)
elemlist.append(elem)
self.append(elemlist)
Expand All @@ -323,8 +322,8 @@ def write(self, stream, data):
self._elementTypeArgument = self.arg
len1 = self._len1()
if len1 != self.__len__():
raise ValueError('array size (%i) different from to field \
describing number of elements (%i)'%(self.__len__(),len1))
raise ValueError('array size (%i) different from to field describing number of elements (%i)' %
(self.__len__(), len1))
if len1 > 0x10000000:
raise ValueError('array too long (%i)' % len1)
if self._count2 is None:
Expand All @@ -334,8 +333,8 @@ def write(self, stream, data):
for i, elemlist in enumerate(list.__iter__(self)):
len2i = self._len2(i)
if len2i != elemlist.__len__():
raise ValueError("array size (%i) different from to field \
describing number of elements (%i)"%(elemlist.__len__(),len2i))
raise ValueError("array size (%i) different from to field describing number of elements (%i)" %
(elemlist.__len__(), len2i))
if len2i > 0x10000000:
raise ValueError('array too long (%i)' % len2i)
for elem in list.__iter__(elemlist):
Expand Down Expand Up @@ -406,5 +405,6 @@ def _elementList(self, **kwargs):
for elem in list.__iter__(elemlist):
yield elem


from pyffi.object_models.xml.basic import BasicBase
from pyffi.object_models.xml.struct_ import StructBase
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ universal = 1
[metadata]
license_file = LICENSE.rst

[pep8]
# line length
ignore=E501

[coverage:run]
source = pyffi

Expand Down
Loading

0 comments on commit 440c61c

Please sign in to comment.