From fb2e29bb321bd9d83fd280d7c8e39ac7df1cb301 Mon Sep 17 00:00:00 2001 From: Tim Beyer <35711942+TimFelixBeyer@users.noreply.github.com> Date: Tue, 9 May 2023 10:22:54 +1000 Subject: [PATCH] Avoid altering score on write #1557 See #1557 --- music21/musicxml/m21ToXml.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/music21/musicxml/m21ToXml.py b/music21/musicxml/m21ToXml.py index afffbb55d..c9f38708d 100644 --- a/music21/musicxml/m21ToXml.py +++ b/music21/musicxml/m21ToXml.py @@ -496,9 +496,10 @@ def fromGeneralObject(self, obj: prebase.ProtoM21Object): outObj = None if isinstance(obj, stream.Stream) and self.makeNotation: - obj.makeRests(refStreamOrTimeRange=[0.0, obj.highestTime], + # This is where the first (and hopefully only) copy is made. + obj = obj.makeRests(refStreamOrTimeRange=[0.0, obj.highestTime], fillGaps=True, - inPlace=True, + inPlace=False, hideRests=True, # just to fill up MusicXML display timeRangeFromBarDuration=True, ) @@ -517,14 +518,14 @@ def fromGeneralObject(self, obj: prebase.ProtoM21Object): def fromScore(self, sc): ''' - Copies the input score and runs :meth:`~music21.stream.Score.makeNotation` on the copy. + Runs :meth:`~music21.stream.Score.makeNotation` on the copy. ''' - scOut = sc.makeNotation(inPlace=False) - if not scOut.isWellFormedNotation(): - warnings.warn(f'{scOut} is not well-formed; see isWellFormedNotation()', + sc.makeNotation(inPlace=True) + if not sc.isWellFormedNotation(): + warnings.warn(f'{sc} is not well-formed; see isWellFormedNotation()', category=MusicXMLWarning) - # scOut.makeImmutable() - return scOut + # sc.makeImmutable() + return sc def fromPart(self, p): ''' @@ -548,20 +549,20 @@ def fromMeasure(self, m): solutions in Part or Stream production. ''' m.coreGatherMissingSpanners() - mCopy = m.makeNotation() - if mCopy.style.measureNumbering is None: + m.makeNotation(inPlace=True) + if m.style.measureNumbering is None: # Provide a default - mCopy.style.measureNumbering = 'measure' + m.style.measureNumbering = 'measure' clef_from_measure_start_or_context = m.getContextByClass( clef.Clef, getElementMethod=common.enums.ElementSearch.AT_OR_BEFORE_OFFSET ) if clef_from_measure_start_or_context is None: - mCopy.clef = clef.bestClef(mCopy, recurse=True) + m.clef = clef.bestClef(m, recurse=True) else: - mCopy.clef = clef_from_measure_start_or_context + m.clef = clef_from_measure_start_or_context p = stream.Part() - p.append(mCopy) + p.append(m) p.metadata = copy.deepcopy(getMetadataFromContext(m)) context_part = m.getContextByClass(stream.Part) if context_part is not None: