Skip to content

Instantly share code, notes, and snippets.

@lgvr123
Last active May 1, 2022 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgvr123/8a58229bd8a3d96144e3b4c60afcb30e to your computer and use it in GitHub Desktop.
Save lgvr123/8a58229bd8a3d96144e3b4c60afcb30e to your computer and use it in GitHub Desktop.
apply accidentalBracketType from one excerpt to all excerpts
import QtQuick 2.9
import MuseScore 3.0
MuseScore {
menuPath: "Plugins.mcvePart"
description: "Retrieve all the properties about the selected chors and notes"
version: "1.0.0"
requiresScore: true
readonly property bool includeCurScore: true
/** the notes to which the fingering must be made. */
// -----------------------------------------------------------------------
// --- Read the score ----------------------------------------------------
// -----------------------------------------------------------------------
onRun: {
// 1) identify our starting note/accidental
var element = curScore.selection.elements[0];
var accidental;
var note;
var elscore = curScore;
var fullscore = findFullScore();
if (!elscore) {
console.warn("Fail to identify the real curSocre");
return;
}
// -note-accidental-
if (element === undefined) {
console.log("No selection");
return;
} else if (element.type == Element.ACCIDENTAL) {
accidental = element;
note = element.parent;
} else if (element.type == Element.NOTE) {
note = element;
accidental = element.accidental;
} else {
console.log("Fail to identify an accidental " + element.userName() + "/" + element.type);
return;
}
// 2) identifying where is the selected element
// -segment-track-excerpt/score
var segment = note;
while (segment.type != Element.SEGMENT) {
segment = segment.parent;
}
var tick = segment.tick;
var track = element.track;
var voice = element.voice;
// -"partScpre" excerpt-
var part = note.staff.part;
console.log("we are on " + tick + "/" + track + "/" + voice + "/" + part.longName + "(" + part.instrumentId + ")");
console.log("we are at pitch " + note.pitch + "/ accidental: " + note.accidentalType + "/" + accidental.accidentalBracket);
// 3) Identifying where to apply the change
var scores = [fullscore];
for (var i = 0; i < fullscore.excerpts.length; i++) {
scores.push(fullscore.excerpts[i].partScore);
}
// 4) Defining how to do
var bracketSetting = accidental.accidentalBracket;
//var bracketSetting = 2; //for testing since the cursor is in the same score right now
// 5) Applying the change
fullscore.startCmd();
for (var i = 0; i < scores.length; i++) {
console.log("~~~~ " + scores[i] + " ~~~~");
var _score = scores[i];
if (_score.is(curScore)) {
console.log("Our self. Bypassing");
continue;
}
var _parts = _score.parts;
for (var j = 0; j < _parts.length; j++) {
var _part = _parts[j];
console.log(".... " + _part.longName + " ("+(j+1)+"/"+_parts.length+") ....");
// if (_part.is(part)) { // <-- this how it should work, but it doesn't => checking with the instruments
if (_part.longName === part.longName && _part.instruments.length === part.instruments.length) {
console.log("!!MATCH!!");
} else {
continue;
}
// console.log("excerpts: "+partscore.excerpts.length+", tracks: "+partscore.ntracks);
var cursor = _score.newCursor();
var _track = _part.startTrack + voice;
cursor.track = _track;
cursor.rewindToTick(tick);
var cseg = cursor.segment;
if (cseg === null) {
console.log("nothing found at " + tick + "/" + _track);
continue;
}
var atSegment = cseg.elementAt(cursor.track);
console.log("found at " + tick + "/" + _track + ": " + (atSegment ? atSegment.userName() : "null") + " (" + cursor.segment.tick + ")");
if (cursor.segment.tick !== tick) {
console.log("Non matching tick");
continue;
}
console.log("looking for " + note.pitch + "/ accidental: " + note.accidentalType + "/" + (note.accidental ? note.accidental.accidentalBracket : "null"));
if (atSegment.type === Element.CHORD) {
for (var k = 0; k < atSegment.notes.length; k++) {
var _note = atSegment.notes[k];
console.log("we are at pitch " + _note.pitch + "/ accidental: " + _note.accidentalType + "/" + (_note.accidental ? _note.accidental.accidentalBracket : "null"));
if (_note.pitch !== note.pitch || _note.accidentalType !== note.accidentalType) {
console.log("Non matching pitch/accidental");
continue;
}
if (!_note.accidental || _note.accidentalType === 0)
continue;
try {
_note.accidental.accidentalBracket = bracketSetting;
console.log("\tchanged!");
} catch (error) {
console.warn("while setting note.accidental.accidentalBracket to " + bracketSetting + ":\n\t" + error);
}
}
}
}
}
fullscore.endCmd();
}
function findFullScore(score) {
if (typeof score === 'undefined')
score = curScore;
for (var i = 0; i < scores.length; ++i) {
var s = scores[i];
if (s.is(score)) {
return score;
}
var excerpts = s.excerpts;
for (var ei = 0; ei < excerpts.length; ++ei) {
if (excerpts[ei].partScore.is(score)) {
return s;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment