Skip to content

Commit

Permalink
add dependant_mobs helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
markreidvfx committed Jun 30, 2023
1 parent 146f801 commit c86a7b3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/avb/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def __init__(self):
self.sifted_settings.append(s)

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

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

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

for i in range(object_count):
bin_obj = BinItem.__new__(BinItem, root=self.root)
Expand Down
3 changes: 3 additions & 0 deletions src/avb/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ 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:
Expand Down
35 changes: 33 additions & 2 deletions src/avb/trackgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import datetime

from . import core
from .core import AVBPropertyDef, AVBRefList
from .components import Component
from .core import AVBPropertyDef, walk_references
from . import utils
from . import mobid
from . utils import peek_data
from .components import Component, SourceClip

TRACK_LABEL_FLAG = 1 << 0
TRACK_ATTRIBUTES_FLAG = 1 << 1
Expand Down Expand Up @@ -225,6 +225,37 @@ def write(self, f):
else:
ctx.write_s16(f, 0)

def dependant_mobs(self):
"""
Yields all mobs that this mob is dependant on in depth first order.
"""

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

for obj in walk_references(mob):
if isinstance(obj, SourceClip):
ref_mob = obj.mob
if not ref_mob:
continue
if ref_mob.mob_id not in visited:
stack.append(ref_mob)
children_processed = False

if children_processed:
stack.pop(-1)
if mob.mob_id not in visited:
visited.add(mob.mob_id)
if mob is not self:
yield mob


@utils.register_class
class TrackEffect(TrackGroup):
Expand Down
59 changes: 59 additions & 0 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,64 @@ def test_copy(self):
assert mob_b
compare(mob_a, mob_b)

def test_copy_mastermob_depends(self):
result_file = os.path.join(result_dir, 'copy_mastermobs.avb')

mob_ids = set()
with avb.open(test_file_01) as a:
with avb.open() as b:
mobs = {}
for mob in a.content.mastermobs():
mobs[mob.mob_id] = mob
for m in mob.dependant_mobs():
mobs[m.mob_id] = m

for mob in mobs.values():
mob_ids.add(mob.mob_id)
new_mob = mob.copy(b)
b.content.add_mob(new_mob)


b.write(result_file)

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)
compare(mob_a, mob_b)

def test_copy_compositionmobs(self):
result_file = os.path.join(result_dir, 'copy_compositionmob.avb')

mob_ids = set()
with avb.open(test_file_01) as a:
with avb.open() as b:

mobs = {}
for mob in a.content.compositionmobs():
mobs[mob.mob_id] = mob
for m in mob.dependant_mobs():
mobs[m.mob_id] = m

for mob in mobs.values():
mob_ids.add(mob.mob_id)
new_mob = mob.copy(b)
b.content.add_mob(new_mob)


b.write(result_file)

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)
compare(mob_a, mob_b)

if __name__ == "__main__":
unittest.main()

0 comments on commit c86a7b3

Please sign in to comment.