write

write(
    graph: OmkGraph,
    target: str | Path | IO[bytes],
    score: Score | None = None,
    omit: Iterable[type[OmkObject]] = (),
)

Writes score (the graph’s only Score when None) as MusicXML 4.0, compressed .mxl when the target’s name ends in .mxl. Nodes of the omit types are left out, with whatever hangs from them.

>>> import io
>>> from openmusickit.graph.graph import GraphMeta
>>> from openmusickit.io.musicxml import read
>>> from openmusickit.objects.context_event import MeterEvent
>>> from openmusickit.objects.note_event import NoteEvent
>>> from openmusickit.systems.wsmn.scoring.symbols import final_bar
>>> from openmusickit.systems.wsmn.temporal.symbols import half, two_four
>>> c4, d4 = TonalVector((0, 0, 0)), TonalVector((1, 2, 0))
>>> graph = OmkGraph(GraphMeta())
>>> line = [MeterEvent(meter=two_four), NoteEvent(tones={c4}, duration=half),
...         NoteEvent(tones={d4}, duration=half), DivisionEvent(shape=final_bar)]
>>> graph.add_line(line)
>>> score, flute = Score(title="Two bars"), Part(name="Flute")
>>> graph.add_line_to_score(score, line[0])
>>> graph.add_part_to_score(score, flute)
>>> graph.add_stint(flute, Stint(), line[0])
>>> out = io.BytesIO()
>>> write(graph, out)
>>> again = read(io.BytesIO(out.getvalue()))
>>> head = next(again.score_lines(next(again.nodes(Score))))
>>> [type(event).__name__ for event in again.walk_line(head)]
['MeterEvent', 'NoteEvent', 'DivisionEvent', 'NoteEvent', 'DivisionEvent']

The meter put the bar line between the two notes, as it would in the reader’s full form.

Raises

Name Type Description
UnexportableError if the graph holds no Score, or several and score is None, or the Score has no parts.