Deleting empty measures in a score
This weekend I was trying to implement the automatic deletion of an empty measure creating a new plugin. I am able to select each one of the measures in a score but I'm not able to find the function to know if it's empty and for sure I was not able to delete one measure. Is possible to do that?
The original discussion began here
https://musescore.org/en/node/284185
Thank you very much in advance.
Marta.
Comments
The function checks the presence of notes in the measure. It works only with the first track. as an example ...
In reply to The function checks the… by bazhenoff
Thank you very much!!
I was thinking about trying to automate something similar just last night. However, instead of just removing a single measure, I wanted to automatically strip off all empty measures the ends of scores (e.g. for use when doing batch exports of all the scores in a directory, so that I can more easily keep track of which file is which, etc. using some dedicated asset/content manager)
I'm currently stuck trying to figure out if there is any way to use the API to remove measures. There are methods to append/add things to scores or modify existing elements, but hardly ever anything to remove stuff that already exists in the score.
It's increasingly looking like I may have to either hack this into the C++ code myself (once I get the CMake generation to work on Windows) or by putting together an external tool to fiddle with the MuseScore files directly.
In reply to I was thinking about trying… by Aligorith
There is a command to do this already, in Tools menu, is there a way to use or leverage that?
In reply to There is a command to do… by Marc Sabatella
Can Tools/Commands be called from plugins in any way? I hope there's an official way/api, otherwise I'll try to see if we can inject some fake events to get the desired effect...
In reply to There is a command to do… by Marc Sabatella
Ah, so upon further investigation, it turns out that the "remove empty trailing measures" functionality is built into the new 3.x series, but isn't in 2.x (which I'm currently using, hence why I was trying to write a plugin to do so :)
Now, in response to my own question above:
* Simply calling
cmd("command-name")
should trigger the command, as if you'd executed it from the UI (e.g. I confirmed usingcmd("pitch-up")
to replicate the up-arrow behaviour on a selected note).* Available commands can be found in
libmscore/cmd.cpp - Score::cmd()
In reply to Ah, so upon further… by Aligorith
Sorry Aligorith, I'm not sure if I had understood you well. Is it possible to call a command through a plugin line? If the answer is YES, can you provide a brief example of doing that?
Thank you in advance!!
In reply to Sorry Aligorith, I'm not… by Marta Morticia
Hi again,
With the "measureIsEmpty" function provided by bazhenoff I have implemented this plugin. The function detects the empty measures but I cannot find the cmd order to delete them. In the meantime I tried to insert a note in the empty measures but I think I'm doing something wrong.
Please, find the the code below. Thank you for your help.
for (var track = 0; track < 1; ++track) {
}
In reply to Hi again, With the … by Marta Morticia
Add note...
In reply to function getCursor(segment){… by bazhenoff
Thank you very much.
I'm quite new at programing musescore plugins and I couldn't find yet how to delete a measure. I'll keep trying.
Thank you again for this piece of code.
In reply to Thank you very much. I'm… by Marta Morticia
Use this function: https://github.com/musescore/MuseScore/blob/773481cbe468d71271d3fb11dea…
In reply to Use this function: https:/… by ecstrema
Thank you very much for your suggestion but it seems that calling the "removeElement(aux_mea);" inside the plugin code is not possible.
Can you give me a hint of the usage of this function?
Thank you very much in advance.
Marta.
In reply to Thank you very much for your… by Marta Morticia
To remove empty trailing measures, call
cmd ("remove-empty-trailing-measures")
(this is not the exact name of the command, check in the plugin utilities plugin to see a list af available commands.As of how to delete empty measures i'll investigate, i can't tell you now.
In reply to To remove empty trailing… by ecstrema
I have this at this moment, but nothing happens
onRun:
{
var cursor = curScore.newCursor();
cursor.rewind(0);
function measureIsEmpty(measure) {
var segment = measure.firstSegment;
while (segment) {
var element = segment.elementAt(0);
if (element && element._name() == "Chord") {
return false;
}
segment = segment.nextInMeasure;
}
console.log('===== Empty measure =====');
return true;
}
The code detects the empty measures but does nothing.
Thank you so much in advance!!
In reply to I have this at this moment,… by Marta Morticia
Wow, you have a shitload of things that don't make sense in there.
Do you work in the plugin creator?
You should have a lot of errors in the console. Correct them first.
In reply to Wow, you have a shitload of… by ecstrema
Yes I did and nothing appears in the console. Maybe I have been too ambitious to do a plugin without knowing the code in depth. I enclose the file with the plugin code but I don't want you to loose much time. Is not so important.
Thank you very much in advance and for your time.
Marta.
In reply to Yes I did and nothing… by Marta Morticia
Haven't tried it, but here's how i would do it:
import QtQuick 2.1
import MuseScore 3.0
MuseScore {
version: "1.0"
description: "Plugin que borra compases vacíos"
menuPath: "Plugins.Borra vacio"
In reply to Haven't tried it, but here's… by ecstrema
Thank you so much!! I'll try it soon and I will let you know.
Best.
Marta.
In reply to Thank you so much!! I'll try… by Marta Morticia
It's been a long time since this, but I think these minor changes work:
Invoking Qt.Quit() in a plugin neither a dialog nor a dock plugin (in which case it will destroy them) is a bad idea, because if are any dock plugins active, it will kill them -- QtQuit() destroys the single plugin environment, closing all plugins. Bet you didn't know that there can be plugins active when yours is invoked --- I think this needs fixing in the core (close my plugin only), but for a non-dialog, non-dock plugin like yours, it is unnecessary.
Other stakeholders feel free to discuss.
In reply to Invoking Qt.Quit() in a… by [DELETED] 1831606
i do agree: something like
Ms.Quit()
would be really handy (even though for now, your plugins are the only one using that newonSelectionChanged
thing ;)In reply to i do agree: something like… by ecstrema
This has nothing to do with onSelectionChanged -- ir concerns being a dock plugin, and there is nothing the poor dock plugins can do to prevent themselves from being closed by other plugins. This is about coding the latter, not the former.
In reply to This has nothing to do with… by [DELETED] 1831606
You are right about that, and using
Qt.quit
can actually get dangerous if things in other dock plugins haven't been saved. Take the comments plugin. It only saves on window close, so ifQt.quit()
is called by some other plugin while running. it will loose all changes.Guess it would be worth a new issue thread...
In reply to You are right about that,… by ecstrema
There's yer three kindsa plugins -- one-shot, dialog, and dock. One-shot, which have no UI, don't need to call Qt.Quit(); they can just return and all is forgiven. But dialog-type now have no other choice. It's bad, I tell ya...
I usually remove empty measures by simply highlighting the empty measures, and therefore pressing ctrl and delete key on the keyboard.
In reply to I usually remove empty… by Solomon Esemuze
The topic at hand here though is how to do this from within a plugin :-)
In reply to The topic at hand here… by Jojo-Schmitz
Alright, I think now understand. Thanks sir