How to change the bar line type of a measure
I'd like to add a "End bar" in my score whenever I add a System break. However my code crashes MuseScore.
// if there is another part ("page") after this one, add a linebreak
var lbreak = newElement(Element.LAYOUT_BREAK);
lbreak.layoutBreakType = 2; // section break
cursor.rewindToTick(cur_time); // rewing to the last note
cursor.add(lbreak);
var bar = newElement(Element.BAR_LINE);
bar.barLineType = 32;
//cursor.rewindToTick(cur_time); // rewing to the last note
cursor.add(bar); // -- CRASH OF MUSESCORE --
Should I instead of adding the bar, search for the current bar of the current measure and try to change its type ?
Comments
Indeed. Don't add a barline, change the existing one.
In reply to Indeed. Don't add a barline,… by jeetee
I guess there is some kind of bug here.
In a new measure added by the API no BAR_LINE element can be found at its end by the API.
The code is basically this one:
It is working in an existing score.
For score created by the API, I've made 4 scenarios, all based on the creation of a score with one measure.
Scenario 1: Don't add any other measure, search for a bar line in the default first measure
==> OK
Scenario 2: Add a new measure at the end of the score, search for a bar line in the default first measure
==> OK
Scenario 3: Add a new measure at the end of the score, search for a bar line in the new measure
==> KO. Last element found is a Rest
Scenario 4: Add atwo new measures at the end of the score, search for a bar line in the middle measure (so in an API created one)
==> KO. Last element found is a Rest
PS: other ways to get to the last segment have not worked either, such as looping from
cursor.segment
withnext()
andnextInMeasure()
, ...In reply to I guess there is some kind… by parkingb
It's not as much a bug as wrong/incomplete usage here.
All you need to do is wrap the
score.appendMeasures()
call withscore.startCmd()
andscore.endCmd()
. If you don't, then the Plugin API is not obliged to fully process that command before continuing onwards. So whenever you need to rely on the result of a score-changing action to start a different kind of action (or ask information about the result) make sure to wrap said action(s) between a start/endCmd call.In reply to It's not as much a bug as… by jeetee
As further example evidence.
The "end" element being retrieved before the call to endCmd is not yet the actual end of the score at that moment in time (check the tick value).
Which produces the following output:
In reply to As further example evidence… by jeetee
Thanks. It works.
But it is very close from the bug ;-)
All my score manipulations were enclosed in one single big startCmd/endCmd.
This BAR_LINE is the only action that requires an intermediate endCmd/startCmd.
Adding measures, time signatures, staff text, notes, chords, chord symbols, layout break, ... All of these are working correctly with one single startCmd/endCmd.
It might be interesting to investigate why the BAR_LINE doesn't...