Created
March 11, 2015 09:06
-
-
Save zeffii/e2a80b44824cea0c653c to your computer and use it in GitHub Desktop.
Vector median or tuple median from a nested list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from mathutils import Vector | |
def get_median(input_lists): | |
_list = [] | |
for vec_list in input_lists: | |
median = Vector() | |
for vec in vec_list: | |
if isinstance(vec, Vector): | |
median = median + vec | |
elif isinstance(vec, tuple) and (len(vec) == 3): | |
median = median + Vector(vec) | |
elif isinstance(vec, tuple) and (len(vec) == 2): | |
median = median + Vector((vec[0], vec[1], 0)) | |
# in case of no vectors in the list, return 0 vector | |
# if one vector, that's the media | |
len_sublist = len(vec_list) | |
if len_sublist > 1: | |
median = median / len_sublist | |
elif len_sublist == 0: | |
median = Vector() | |
_list.append(median) | |
return _list | |
list1 = [(1,0,0), (2,0,0)] | |
list2 = [] | |
f = get_median([list1, list2]) | |
print(f) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment