diff options
Diffstat (limited to 'rigify/utils/misc.py')
-rw-r--r-- | rigify/utils/misc.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/rigify/utils/misc.py b/rigify/utils/misc.py index 2ca7b016..b0f79ea7 100644 --- a/rigify/utils/misc.py +++ b/rigify/utils/misc.py @@ -19,8 +19,12 @@ # <pep8 compliant> import math +import collections + +from itertools import tee, chain, islice, repeat from mathutils import Vector, Matrix, Color + #============================================= # Math #============================================= @@ -82,6 +86,49 @@ def gamma_correct(color): #============================================= +# Iterators +#============================================= + + +def padnone(iterable, pad=None): + return chain(iterable, repeat(pad)) + + +def pairwise_nozip(iterable): + "s -> (s0,s1), (s1,s2), (s2,s3), ..." + a, b = tee(iterable) + next(b, None) + return a, b + + +def pairwise(iterable): + "s -> (s0,s1), (s1,s2), (s2,s3), ..." + a, b = tee(iterable) + next(b, None) + return zip(a, b) + + +def map_list(func, *inputs): + "[func(a0,b0...), func(a1,b1...), ...]" + return list(map(func, *inputs)) + + +def skip(n, iterable): + "Returns an iterator skipping first n elements of an iterable." + iterator = iter(iterable) + if n == 1: + next(iterator, None) + else: + next(islice(iterator, n, n), None) + return iterator + + +def map_apply(func, *inputs): + "Apply the function to inputs like map for side effects, discarding results." + collections.deque(map(func, *inputs), maxlen=0) + + +#============================================= # Misc #============================================= @@ -98,3 +145,23 @@ def copy_attributes(a, b): setattr(b, key, getattr(a, key)) except AttributeError: pass + + +def assign_parameters(target, val_dict=None, **params): + data = { **val_dict, **params } if val_dict else params + for key, value in data.items(): + try: + target[key] = value + except Exception as e: + raise Exception("Couldn't set {} to {}: {}".format(key,value,e)) + + +def select_object(context, object, deselect_all=False): + view_layer = context.view_layer + + if deselect_all: + for objt in view_layer.objects: + objt.select_set(False) # deselect all objects + + object.select_set(True) + view_layer.objects.active = object |