QML plugin and importing

• Jul 30, 2012 - 04:58

Docs at nokia show an "Import" statement...I'm trying it in one of my scripts, and it's acting like it *wants* to work, but I can't figure out how to get the feature to work...

Is the "Import" feature a part of the MuseScore plugin architecture?

If it is, what are the rules? Are there any examples?

Thanks!


Comments

The import (note the lower case 'i') is used to..., well..., import modules available to the framework.

The MuseScore QML plugin framework has two modules available (they have to be 'created' by the program before dealing with plugins):

*) QtQuick 1.0
*) MuseScore 1.0

You may import them at the beginning of your plugin with:

import QtQuick 1.0
import MuseScore 1.0

In fact, you must import the first or the plugin would not run and almost certainly you will import the second or your plugin will not have access to any MuseScore object. End result: all MuseScore QML plugins start with the two lines quoted above.

No other module is available for import to MuseScore QML plugins.

If you refer to some other kind of import, please add some other detail.

HTH,

M.

In reply to by Miwarre

The examples I see online show imporing javascript files...this would be a FANTASTIC feature for making reusable components...like an informational library (I have an object to map durations to duration names, it would great to be able to use the same code in multiple projects without copy-and-paste.

How do I add "Javascript imports" to the plugin feature requests list?

In reply to by theGleep

Ah, that "import". I saw the documentation too, but I assumed it would be rather difficult to make it work and even more to distribute, as the imported file path should either be absolute or relative to whatever the MuseScore current working directory is.

I think it is a feature not intended for scripting, but for stand-alone QML applications (as most of QML seems to be, unfortunately).

If you can get any result, please let us know!

Thanks,

M.

In reply to by Miwarre

I played a little bit with the new plugin framework and came to the same question.

import QtQuick 1.0
import MuseScore 1.0
import "/usr/local/share/mscore-2.0/plugins/externJavaScript.js" as MyTest

MuseScore {
      menuPath: "Plugins.pluginName"
      onRun: {
           MyTest.sayHello(curScore)

            Qt.quit()
            }
      }

using this javascript code (file externJavaScript.js):

function sayHello(score) {
    var cursor = score.newCursor();
    cursor.rewind(1);
    console.log(cursor.tick);
}

Well, using some different selections, it gave reasonable tick values.
So I think the remaining point is: Is there a way to use a relative path in the import statement?

In reply to by Miwarre

http://doc.qt.nokia.com/qt-maemo/qdeclarativemodules.html says:

The QML import path

The QML engine will search the import path for a requested installed module. The default import path includes:

    The directory of the current file
    The location specified by QLibraryInfo::ImportsPath
    Paths specified by the QML_IMPORT_PATH environment variable

The import path can be queried using QDeclarativeEngine::importPathList() and modified using QDeclarativeEngine::addImportPath().

I hope that's what we're looking for.

This works for me:
--------------- .qml file -------------
import QtQuick 1.0
import MuseScore 1.0
import "../plugins/musescorelib.js" as MScoreLib;

MuseScore {
version : "1.0";
description : "Identifies chords";
menuPath : "Plugins.Chord Identifier";

onRun : {

MScoreLib.sayHello(curScore);
Qt.quit();
}
}

--------------- musescorelib.js -------------
function sayHello(score) {

console.log(10);
}

=======================================

Apparently, the "current" folder when running is the "bin" folder. Once you know that, relative path works.

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