mirror of
https://github.com/3b1b/manim.git
synced 2025-07-31 22:13:30 +08:00
Added initial_config to digest_config, so that there is a way to recall how a mobject/animation/scene was initialized
This commit is contained in:
33
helpers.py
33
helpers.py
@ -252,14 +252,14 @@ def get_all_descendent_classes(Class):
|
|||||||
result.append(Child)
|
result.append(Child)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def filtered_locals(local_args):
|
def filtered_locals(caller_locals):
|
||||||
result = local_args.copy()
|
result = caller_locals.copy()
|
||||||
ignored_local_args = ["self", "kwargs"]
|
ignored_local_args = ["self", "kwargs"]
|
||||||
for arg in ignored_local_args:
|
for arg in ignored_local_args:
|
||||||
result.pop(arg, local_args)
|
result.pop(arg, caller_locals)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def digest_config(obj, kwargs, local_args = {}):
|
def digest_config(obj, kwargs, caller_locals = {}):
|
||||||
"""
|
"""
|
||||||
Sets init args and CONFIG values as local variables
|
Sets init args and CONFIG values as local variables
|
||||||
|
|
||||||
@ -268,19 +268,25 @@ def digest_config(obj, kwargs, local_args = {}):
|
|||||||
be easily passed into instantiation, and is attached
|
be easily passed into instantiation, and is attached
|
||||||
as an attribute of the object.
|
as an attribute of the object.
|
||||||
"""
|
"""
|
||||||
### Assemble list of CONFIGs from all super classes
|
|
||||||
|
# Assemble list of CONFIGs from all super classes
|
||||||
classes_in_hierarchy = [obj.__class__]
|
classes_in_hierarchy = [obj.__class__]
|
||||||
configs = []
|
static_configs = []
|
||||||
while len(classes_in_hierarchy) > 0:
|
while len(classes_in_hierarchy) > 0:
|
||||||
Class = classes_in_hierarchy.pop()
|
Class = classes_in_hierarchy.pop()
|
||||||
classes_in_hierarchy += Class.__bases__
|
classes_in_hierarchy += Class.__bases__
|
||||||
if hasattr(Class, "CONFIG"):
|
if hasattr(Class, "CONFIG"):
|
||||||
configs.append(Class.CONFIG)
|
static_configs.append(Class.CONFIG)
|
||||||
|
|
||||||
#Order matters a lot here, first dicts have higher priority
|
#Order matters a lot here, first dicts have higher priority
|
||||||
all_dicts = [kwargs, filtered_locals(local_args), obj.__dict__]
|
caller_locals = filtered_locals(caller_locals)
|
||||||
all_dicts += configs
|
all_dicts = [kwargs, caller_locals, obj.__dict__]
|
||||||
|
all_dicts += static_configs
|
||||||
|
all_new_dicts = [kwargs, caller_locals] + static_configs
|
||||||
obj.__dict__ = merge_config(all_dicts)
|
obj.__dict__ = merge_config(all_dicts)
|
||||||
|
#Keep track of the configuration of objects upon
|
||||||
|
#instantiation
|
||||||
|
obj.initial_config = merge_config(all_new_dicts)
|
||||||
|
|
||||||
def merge_config(all_dicts):
|
def merge_config(all_dicts):
|
||||||
all_config = reduce(op.add, [d.items() for d in all_dicts])
|
all_config = reduce(op.add, [d.items() for d in all_dicts])
|
||||||
@ -295,6 +301,15 @@ def merge_config(all_dicts):
|
|||||||
config[key] = merge_config([config[key], value])
|
config[key] = merge_config([config[key], value])
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
def soft_dict_update(d1, d2):
|
||||||
|
"""
|
||||||
|
Adds key values pairs of d2 to d1 only when d1 doesn't
|
||||||
|
already have that key
|
||||||
|
"""
|
||||||
|
for key, value in d2.items():
|
||||||
|
if key not in d1:
|
||||||
|
d1[key] = value
|
||||||
|
|
||||||
def digest_locals(obj, keys = None):
|
def digest_locals(obj, keys = None):
|
||||||
caller_locals = filtered_locals(
|
caller_locals = filtered_locals(
|
||||||
inspect.currentframe().f_back.f_locals
|
inspect.currentframe().f_back.f_locals
|
||||||
|
Reference in New Issue
Block a user