Changeset View
Changeset View
Standalone View
Standalone View
rigify/utils/misc.py
| Show All 13 Lines | |||||
| # along with this program; if not, write to the Free Software Foundation, | # along with this program; if not, write to the Free Software Foundation, | ||||
| # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| # | # | ||||
| #======================= END GPL LICENSE BLOCK ======================== | #======================= END GPL LICENSE BLOCK ======================== | ||||
| # <pep8 compliant> | # <pep8 compliant> | ||||
| import math | import math | ||||
| import collections | |||||
| from itertools import tee, chain, islice, repeat | |||||
| from mathutils import Vector, Matrix, Color | from mathutils import Vector, Matrix, Color | ||||
| #============================================= | #============================================= | ||||
| # Math | # Math | ||||
| #============================================= | #============================================= | ||||
| def angle_on_plane(plane, vec1, vec2): | def angle_on_plane(plane, vec1, vec2): | ||||
| """ Return the angle between two vectors projected onto a plane. | """ Return the angle between two vectors projected onto a plane. | ||||
| """ | """ | ||||
| ▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | def gamma_correct(color): | ||||
| corrected_color = Color() | corrected_color = Color() | ||||
| for i, component in enumerate(color): | for i, component in enumerate(color): | ||||
| corrected_color[i] = linsrgb_to_srgb(color[i]) | corrected_color[i] = linsrgb_to_srgb(color[i]) | ||||
| return corrected_color | return corrected_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 | # Misc | ||||
| #============================================= | #============================================= | ||||
| def copy_attributes(a, b): | def copy_attributes(a, b): | ||||
| keys = dir(a) | keys = dir(a) | ||||
| for key in keys: | for key in keys: | ||||
| if not key.startswith("_") \ | if not key.startswith("_") \ | ||||
| and not key.startswith("error_") \ | and not key.startswith("error_") \ | ||||
| and key != "group" \ | and key != "group" \ | ||||
| and key != "is_valid" \ | and key != "is_valid" \ | ||||
| and key != "rna_type" \ | and key != "rna_type" \ | ||||
| and key != "bl_rna": | and key != "bl_rna": | ||||
| try: | try: | ||||
| setattr(b, key, getattr(a, key)) | setattr(b, key, getattr(a, key)) | ||||
| except AttributeError: | except AttributeError: | ||||
| pass | 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 | |||||