← Files CapCutARCHIVED FILE
skills/capcut-stdraft-builder/scripts/_draft.py
3.24 KB · Nov 23, 2026 · 12:11 UTC
"""Draft structure ops: load, validated output, time ranges, layer space. Knows no material types."""
from stdraft_builder import STDraftModel
def load(draft):
"""Restore a private model and index its normalized segments."""
model = STDraftModel()
if model.restore(draft) != 0:
raise ValueError('Cannot restore draft')
index = {s['id']: s for s in model.dump(compact=False)['segments']}
roots = [s['id'] for s in index.values() if s['type'] == 'DRAFT']
if len(roots) != 1:
raise ValueError('Draft must have one root')
return model, roots[0], index
def finish(model, original, added, updated, warnings=(), removed=()):
"""Validate once and return a plain result dictionary.
original is this operation's input draft. Errors it already carried are downgraded
to PRE_EXISTING_INVALID warnings, and only newly introduced ones raise — segments
written by another endpoint may not be recognized locally, and one unrelated edit
should not fail wholesale over them. check_result is returned as-is; valid may
still be false.
"""
check = model.check()
messages = list(warnings)
if not check['valid']:
current = _errors(check)
inherited = set(_errors(_original_check(original)))
if any(item not in inherited for item in current):
raise ValueError(check)
messages.extend('PRE_EXISTING_INVALID: ' + item for item in current)
return {'draft': model.dump(compact=False), 'added_segment_ids': added,
'updated_segment_ids': updated, 'removed_segment_ids': list(removed),
'warnings': messages, 'check_result': check}
def _errors(check):
"""Flatten check errors into comparable strings; the rule name avoids cross-rule false matches."""
return ['[%s] %s: %s' % (name, error['id'], error['msg'])
for name, rule in sorted(check['rules'].items())
for error in rule['errors']]
def _original_check(draft):
"""The pre-operation check result. Only needed when the later check fails; free for a valid draft."""
model = STDraftModel()
if model.restore(draft) != 0:
raise ValueError('Cannot restore draft')
return model.check()
def span(segment):
"""Read a normalized segment's project interval."""
timeline = segment.get('timeline', {})
start = timeline.get('start.ms', 0)
return start, start + timeline.get('duration.ms', 0)
def occupied(index, root, is_audio, exclude=None):
"""Occupied (layer, start, end) among root-level siblings. Audio and visuals have separate layer spaces."""
return [(s.get('layout', {}).get('layer', 0), *span(s))
for s in index.values()
if s.get('parent_id') == root and s['id'] != exclude
and (s['type'] == 'AUDIO') == is_audio]
def overlaps(taken, layer, start, duration):
"""Whether existing content on this layer intersects [start, start+duration)."""
return any(l == layer and start < right and left < start + duration
for l, left, right in taken)
def next_free_layer(taken, start, duration):
"""Place new content above layers overlapping its project interval."""
return max([0] + [layer + 1 for layer, left, right in taken
if start < right and left < start + duration])
SHA-256: a6092a99018c75ef42cfbe63331a8d66ceaa69dcbb7ef07059230f53b9d14f1c