Python library for piecewise linear interpolation in multiple dimensions with multiple, arbitrarily placed, masters.
npx @tessl/cli install tessl/pypi-mutator-math@2.1.0MutatorMath is a Python library that provides advanced mathematical tools for piecewise linear interpolation in n-dimensional design spaces with any number of masters. Originally developed for font interpolation, it can handle any arithmetic objects that support basic mathematical operations. The library enables complex interpolation scenarios including on-axis and off-axis calculations, anisotropic coordinates, and multi-dimensional design spaces with named axes.
pip install MutatorMathfrom mutatorMath import Location, MutatorFor advanced functionality:
from mutatorMath.objects.location import Location
from mutatorMath.objects.mutator import Mutator, buildMutator
from mutatorMath.objects.bender import Benderfrom mutatorMath import Location, Mutator
from mutatorMath.objects.mutator import buildMutator
# Create locations in design space
master1_loc = Location(weight=100, width=75)
master2_loc = Location(weight=900, width=75)
master3_loc = Location(weight=100, width=125)
master4_loc = Location(weight=900, width=125)
# Create sample objects (can be any arithmetic objects)
master1_obj = 50 # thin, narrow
master2_obj = 200 # bold, narrow
master3_obj = 60 # thin, wide
master4_obj = 180 # bold, wide
# Build mutator from (location, object) pairs
items = [
(master1_loc, master1_obj),
(master2_loc, master2_obj),
(master3_loc, master3_obj),
(master4_loc, master4_obj)
]
bias, mutator = buildMutator(items)
# Interpolate at any location in design space
target = Location(weight=500, width=100)
result = mutator.makeInstance(target)
print(result) # Interpolated valueMutatorMath is organized into two main subpackages:
The core design uses Location objects to represent n-dimensional coordinates and Mutator objects to perform interpolation calculations based on master-instance relationships.
Foundation classes for multi-dimensional interpolation including Location objects for coordinate representation, Mutator objects for interpolation calculations, and Bender objects for non-linear transformations.
class Location(dict):
"""N-dimensional location object with arithmetic operations."""
def __init__(self, **kwargs): ...
def isOrigin(self) -> bool: ...
def expand(self, other): ...
class Mutator(dict):
"""Main interpolation class for calculating instances from masters."""
def __init__(self): ...
def makeInstance(self, location) -> any: ...
def setNeutral(self, obj): ...
def setBias(self, location): ...
def buildMutator(items, axes=None, bias=None) -> tuple[Location, Mutator]:
"""Build a mutator with (location, obj) pairs. Returns (bias, mutator) tuple."""
class Bender(object):
"""Non-linear transformation object for warping design spaces."""
def __init__(self, axes): ...
def __call__(self, location) -> Location: ...Specialized tools for font interpolation including designspace document processing, UFO instance generation, and XML-based design space descriptions.
def build(documentPath, outputUFOFormatVersion=2, roundGeometry=True,
verbose=True, logPath=None, progressFunc=None) -> list:
"""Simple builder for UFO designspaces."""
class DesignSpaceDocumentWriter(object):
"""Writer for designspace XML documents."""
def __init__(self, path, toolVersion=3, verbose=False): ...
def save(self, pretty=True): ...
class DesignSpaceDocumentReader(object):
"""Reader and processor for designspace XML documents."""
def __init__(self, path, ufoVersion=2, roundGeometry=True,
verbose=True, logPath=None, progressFunc=None): ...
def process(self): ...class MutatorError(Exception):
"""Exception class for MutatorMath-specific errors."""
def __init__(self, msg, obj=None): ...