How to remove notes in Musecore 2 plugins

• Sep 29, 2015 - 10:43

Hello

In Musescore 1.3 there was the possibility to remove notes with:

removeNote(integer index)

(there was a plugin named removenotes.js)

What's the way do to the same thing in a Musescore 2 plugin?

if i tried:
...
if (CURSOR.element && CURSOR.element.type == Element.CHORD)
{
var CHORD = CURSOR.element
var NOTE = CHORD.notes[0]
CHORD.remove(NOTE)
}
...
Musescore 2 crashes

In the Musescore plugin help you can find

"Chord
inherits ChordRest
Graphic representation of a chord. Single notes are handled as degenerated chords.
Methods
virtual void add(Ms::Element*)
add an element to the Chord

virtual void remove(Ms::Element*)
remove the element from the Chord"

PS: this would be very usefull to keep the highest note only for imported multinote midi

Thanks a lot for helpful instructions


Comments

Hey PimPim,

Thanks for you code. In fact your code is working but you have to be sure there is more than one note in the chord otherwise you need to transform it to a silent.

Here is the code for the top note using std color plugin:

Cheers

//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2017 Frederico Nardi
// ColorNote
// Copyright (C) 2012 Werner Schweer
// Copyright (C) 2013-2015 Nicolas Froment, Joachim Schmitz
// Copyright (C) 2014 Jörn Eichler
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================

import QtQuick 2.0
import MuseScore 1.0

MuseScore {
version: "1.0"
description: "This plugin remove all notes and keep the temblest one in the selection depending on pitch"
menuPath: "Plugins.Notes.Top Notes"

// Apply the given function to all notes in selection
// or, if nothing is selected, in the entire score

function applyToNotesInSelection(func) {
var cursor = curScore.newCursor();
cursor.rewind(1);
var startStaff;
var endStaff;
var endTick;
var fullScore = false;
if (!cursor.segment) { // no selection
fullScore = true;
startStaff = 0; // start with 1st staff
endStaff = curScore.nstaves - 1; // and end with last
} else {
startStaff = cursor.staffIdx;
cursor.rewind(2);
if (cursor.tick == 0) {
// this happens when the selection includes
// the last measure of the score.
// rewind(2) goes behind the last segment (where
// there's none) and sets tick=0
endTick = curScore.lastSegment.tick + 1;
} else {
endTick = cursor.tick;
}
endStaff = cursor.staffIdx;
}
console.log(startStaff + " - " + endStaff + " - " + endTick)
for (var staff = startStaff; staff <= endStaff; staff++) {
for (var voice = 0; voice < 4; voice++) {
cursor.rewind(1); // sets voice to 0
cursor.voice = voice; //voice has to be set after goTo
cursor.staffIdx = staff;

if (fullScore)
cursor.rewind(0) // if no selection, beginning of score

while (cursor.segment && (fullScore || cursor.tick < endTick)) {
if (cursor.element && cursor.element.type == Element.CHORD) {
func(cursor.element);
}
cursor.next();
}
}
}
}

function topNote(chord) {
// Delete all notes but the last
console.log(chord.notes.length);

while ( chord.notes.length > 1) {
chord.remove(chord.notes[0]);
}
}

onRun: {
console.log("hello topnote");

if (typeof curScore === 'undefined')
Qt.quit();

applyToNotesInSelection(topNote)

Qt.quit();
}
}

Do you still have an unanswered question? Please log in first to post your question.