Made slicing into mobjects return a Group or VGroup instead of a list

This commit is contained in:
Grant Sanderson
2018-01-24 11:25:55 -08:00
parent 239addea72
commit c7fa8c59ad
2 changed files with 16 additions and 6 deletions

View File

@ -15,11 +15,11 @@ class Mobject(object):
Mathematical Object Mathematical Object
""" """
CONFIG = { CONFIG = {
"color" : WHITE, "color" : WHITE,
"stroke_width" : DEFAULT_POINT_THICKNESS, "stroke_width" : DEFAULT_POINT_THICKNESS,
"name" : None, "name" : None,
"dim" : 3, "dim" : 3,
"target" : None, "target" : None,
} }
def __init__(self, *submobjects, **kwargs): def __init__(self, *submobjects, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
@ -663,8 +663,12 @@ class Mobject(object):
## Family matters ## Family matters
def __getitem__(self, index): def __getitem__(self, value):
return self.split()[index] self_list = self.split()
if isinstance(value, slice):
GroupClass = self.get_group_class()
return GroupClass(*self_list.__getitem__(value))
return self_list.__getitem__(value)
def __iter__(self): def __iter__(self):
return iter(self.split()) return iter(self.split())
@ -672,6 +676,9 @@ class Mobject(object):
def __len__(self): def __len__(self):
return len(self.split()) return len(self.split())
def get_group_class(self):
return Group
def split(self): def split(self):
result = [self] if len(self.points) > 0 else [] result = [self] if len(self.points) > 0 else []
return result + self.submobjects return result + self.submobjects

View File

@ -19,6 +19,9 @@ class VMobject(Mobject):
"make_smooth_after_applying_functions" : False, "make_smooth_after_applying_functions" : False,
} }
def get_group_class(self):
return VGroup
## Colors ## Colors
def init_colors(self): def init_colors(self):
self.set_style_data( self.set_style_data(