Skip to content

Instantly share code, notes, and snippets.

@LettError
Created November 10, 2017 09:41
Show Gist options
  • Save LettError/992eb8618dd02e4efeacd296afb27b23 to your computer and use it in GitHub Desktop.
Save LettError/992eb8618dd02e4efeacd296afb27b23 to your computer and use it in GitHub Desktop.
Example of how to use a mutatorMath designspace to transform n-dimensional Location objects from one point in space to another. AFAICT this is how the XVAR transformations would work.
from mutatorMath.objects.mutator import buildMutator, Location
# "That is, travel to any part of the Universe without moving."
# This is a demo of how to warp locations in a n-dimensional space.
# The mutatorMath Location object is a more or less a basoc python dictionary.
# But it can also behave as a math object, and that makes it possible
# to calculate xvar like transformations. In this example Location objects
# are both used to place an object in a designspace, as well as the objects themselves.
# Let's take 3 axes
# That means the 8 corners have these locations for the input of the transformation.
a = Location(snap=0, crackle=0, pop=0)
b = Location(snap=1, crackle=0, pop=0)
c = Location(snap=1, crackle=1, pop=0)
d = Location(snap=0, crackle=1, pop=0)
e = Location(snap=0, crackle=0, pop=1)
f = Location(snap=1, crackle=0, pop=1)
g = Location(snap=1, crackle=1, pop=1)
h = Location(snap=0, crackle=1, pop=1)
# and these are their respective destinations. For this example the values are just made up.
a_out = Location(snap=0.25, crackle=0.25, pop=0.25)
b_out = Location(snap=0.75, crackle=0.25, pop=0.25)
c_out = Location(snap=0.75, crackle=0.75, pop=0.25)
d_out = Location(snap=0.25, crackle=0.75, pop=0.25)
e_out = Location(snap=0.25, crackle=0.25, pop=0.75)
f_out = Location(snap=0.75, crackle=0.25, pop=0.75)
g_out = Location(snap=0.75, crackle=0.75, pop=0.75)
h_out = Location(snap=0.25, crackle=0.75, pop=0.75)
# make a list of the in / out values
items = zip([a,b,c,d,e,f,g,h], [a_out,b_out,c_out,d_out,e_out,f_out,g_out,h_out])
# let's make a mutator with these locations.
bias, m = buildMutator(items)
assert bias == Location(snap=0, crackle=0, pop=0)
# we can now ask the mutator for transformations.
# First make sure all the corners return the expected values:
for t1, t2 in items:
assert m.makeInstance(t1) == t2
# Yup that works, how about intermediates?
# Halfway between a and b along the snap axis
assert m.makeInstance(Location(snap=.5, crackle=0, pop=0)) == Location(snap=.5, crackle=.25, pop=.25)
# Halfway between a and c along the snap and crackle axes
assert m.makeInstance(Location(snap=.5, crackle=.5, pop=0)) == Location(snap=.5, crackle=.5, pop=.25)
# Halfway between a and g on one of the long diagonals
assert m.makeInstance(Location(snap=.5, crackle=.5, pop=.5)) == Location(snap=.5, crackle=.5, pop=.5)
# and that is how it goes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment