Skip to content

Commit

Permalink
remove build_mob_dict, replace with simpler find_by_mob_id
Browse files Browse the repository at this point in the history
  • Loading branch information
markreidvfx committed Jul 1, 2023
1 parent 2a705b5 commit f06f412
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
3 changes: 1 addition & 2 deletions examples/avb2aaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def check_source_clip(f, avb_source_clip):
mob_id = MobID(bytes_le=avb_source_clip.mob_id.bytes_le)
if zero_mob_id(mob_id):
return 0
avb_mob = avb_source_clip.root.content.mob_dict[avb_source_clip.mob_id]
avb_mob = avb_source_clip.root.content.find_by_mob_id(avb_source_clip.mob_id)
if mob_id in f.content.mobs:
mob = f.content.mobs[mob_id]
else:
Expand Down Expand Up @@ -797,7 +797,6 @@ def avb2aaf_main(path):
with avb.open(path) as avb_file:
with aaf2.open(path + ".aaf", 'w') as aaf_file:
register_definitions(aaf_file)
avb_file.content.build_mob_dict()
avb2aaf(aaf_file, avb_file)

# with aaf2.open(path + ".aaf", 'r') as aaf_file:
Expand Down
23 changes: 16 additions & 7 deletions src/avb/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class Bin(core.AVBObject):
AVBPropertyDef('attributes', 'BinAttr', 'reference'),
AVBPropertyDef('was_iconic', 'WasIconic', 'bool', False),
]
__slots__ = ('mob_dict')
__slots__ = ('_mob_dict')

def __init__(self):
super(Bin, self).__init__(self)
Expand All @@ -232,7 +232,7 @@ def __init__(self):
self.sifted_settings.append(s)

self.attributes = self.root.create.Attributes()
self.mob_dict ={}
self._mob_dict ={}

def read(self, f):
super(Bin, self).read(f)
Expand All @@ -256,7 +256,7 @@ def read(self, f):
object_count = ctx.read_u32(f)

self.items = []
self.mob_dict ={}
self._mob_dict ={}

for i in range(object_count):
bin_obj = BinItem.__new__(BinItem, root=self.root)
Expand Down Expand Up @@ -369,16 +369,25 @@ def write(self, f):
if self.class_id[:] == b'ABIN':
ctx.write_u8(f, 0x03)

def build_mob_dict(self):
self.mob_dict = {}
for mob in self.mobs:
self.mob_dict[mob.mob_id] = mob
def find_by_mob_id(self, mob_id):

# build mob_id dictionary
if not self._mob_dict:
for mob in self.mobs:
self._mob_dict[mob.mob_id] = mob

return self._mob_dict.get(mob_id, None)

def add_mob(self, mob):
bin_item = self.root.create.BinItem()
bin_item.mob = mob
self.items.append(bin_item)

if self._mob_dict:
self._mob_dict[mob.mob_id] = mob

return bin_item

@property
def mobs(self):
for item in self.items:
Expand Down
5 changes: 1 addition & 4 deletions src/avb/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,10 @@ def write(self, f):

@property
def mob(self):
if not self.root.content.mob_dict:
self.root.content.build_mob_dict()

if hasattr(self, 'mob_id'):
mob_id = self.mob_id
if mob_id:
return self.root.content.mob_dict.get(self.mob_id, None)
return self.root.content.find_by_mob_id(self.mob_id)

@property
def track(self):
Expand Down
3 changes: 0 additions & 3 deletions src/avb/trackgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ def dependant_mobs(self):
visited = set()
stack = [self]

if not self.root.content.mob_dict:
self.root.content.build_mob_dict()

while stack:
mob = stack[-1]
children_processed = True
Expand Down
16 changes: 6 additions & 10 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def test_copy(self):

with avb.open(test_file_01) as a:
with avb.open(result_file) as b:
b.content.build_mob_dict()

for mob_a in a.content.mobs:
mob_b = b.content.mob_dict.get(mob_a.mob_id, None)
mob_b = b.content.find_by_mob_id(mob_a.mob_id)
assert mob_b
compare(mob_a, mob_b)

Expand All @@ -59,11 +59,9 @@ def test_copy_mastermob_depends(self):

with avb.open(test_file_01) as a:
with avb.open(result_file) as b:
b.content.build_mob_dict()
a.content.build_mob_dict()
for mob_id in mob_ids:
mob_a = a.content.mob_dict.get(mob_id, None)
mob_b = b.content.mob_dict.get(mob_id, None)
mob_a = a.content.find_by_mob_id(mob_id)
mob_b = b.content.find_by_mob_id(mob_id)
compare(mob_a, mob_b)

def test_copy_compositionmobs(self):
Expand All @@ -89,11 +87,9 @@ def test_copy_compositionmobs(self):

with avb.open(test_file_01) as a:
with avb.open(result_file) as b:
b.content.build_mob_dict()
a.content.build_mob_dict()
for mob_id in mob_ids:
mob_a = a.content.mob_dict.get(mob_id, None)
mob_b = b.content.mob_dict.get(mob_id, None)
mob_a = a.content.find_by_mob_id(mob_id)
mob_b = b.content.find_by_mob_id(mob_id)
compare(mob_a, mob_b)

if __name__ == "__main__":
Expand Down

0 comments on commit f06f412

Please sign in to comment.