Hi all, I am trying to do a script drag-and-drop tooling in which when the script is dropped into the viewport, it will launch Script Editor, creates the appropriate tab, switch to the tab and paste the script contents into this “newly-created” tab (the input section where you type in maya-based commands)
eg. I drag and drop a .mel script into the viewport, if Script Editor is not present, it will launch Script editor, creates the Mel tab, switch to this Mel tab and paste the .mel script contents into the input section
I am stuck at the part where I am unable to paste the script contents into the tab. Have not been able to find any native mel commands that does this or perhaps I have missed it. Wondering if anyone have done something similar before/ shed some light for me?
I searched the mel documentation page for “scriptEditor”, and I found the scriptEditorInfo command.
That seemed like a good first place to check. And it had an -input flag that seemed to be what you’re looking for. But the documentation appears to say they updated the script editor, and that uses a new command. The documentation of the input flag said to look at the cmdScrollFieldExecuter command.
And in that command, I am seeing flags like -appendText, so it’s probably what you’re looking for.
This is called “Wish for the Strange.”
When you drag a file into the viewport, Maya will perform certain actions depending on the file type.
If it’s a *.mel file, the code it contains will be executed (subject to the restrictions set in the security settings).
If it’s a *.py file, the code contained in the file’s function named “onMayaDroppedPythonFile” will be executed (subject to the restrictions set in the security settings).
For other file types supported by the current Maya installation, an import will be performed.
This is a very important “default behavior” in Maya!
Many installers and configurators for Maya plugins and extensions rely on this “default behavior” (the installation file is simply dragged into the viewport). Not to mention exporting data into a scene using drag-n-drop.
Therefore, to implement the behavior you want, you’ll need to intercept Maya’s drag-n-drop handler.
And/or you’ll need to make some edits to Maya’s standard scripts.
And/or override a lot of global variables (and monitor their correct values).
You can unravel a chain of dependent files, starting with something like: performFileDropAction.mel, performFileImportAction.mel
Regarding working with the Script Editor and manipulating script code:
The optimal option is to create your own window with a formLayout on which to place the cmdScrollFieldReporter. cmdScrollFieldReporter is a very advanced window element specifically designed for working with output (both standard and custom).
And cmdScrollFieldExecuter is for working with input.
However, you can also access these elements in the standard Maya Script Editor.
I have a look around with the use of cmdScrollFieldReporter, unfortunately that is not exactly what I am looking for.
I am looking to do such changes in the actual Script Editor instead of creating a brand new window to cater for it.
In my tooling, there is a small gui that comes with 2-3 buttons, and one of them is to cater for what I am trying to achieve in this post
The script editor uses the cmdScrollFieldReporter and cmdScrollFieldExecuter components.
There are commands with the same names for working with them.
There is also another command for working with the script editor as a whole: scriptEditorInfo
You can find everything you need to work with the script editor (global procedures, global variables) in the file: .../scripts/startup/scripRditorPanel.mel
No need to reinvent the wheel; use these global procedures and global variables for your own purposes.
Below is a short demonstration:
// get cmdScrollFieldReporter path and name:
print $gCommandReporter;
// scriptEditorPanel1Window|scriptEditorPanel1|formLayout110|formLayout112|paneLayout2|cmdScrollFieldReporter1
// use command cmdScrollFieldReporter for clear history:
cmdScrollFieldReporter -e -clear $gCommandReporter;
// Result: scriptEditorPanel1Window|scriptEditorPanel1|formLayout110|formLayout112|paneLayout2|cmdScrollFieldReporter1
// or use command scriptEditorInfo:
scriptEditorInfo -clearHistory;
// or use global procedure:
handleScriptEditorAction("clearHistory")
// get ALL cmdScrollFieldExecuter(s) path(s) and name(s):
print $gCommandExecuter;
// scriptEditorPanel1Window|scriptEditorPanel1|formLayout110|formLayout112|paneLayout2|paneLayout3|tabLayout2|formLayout113|cmdScrollFieldExecuter1
// ...
// scriptEditorPanel1Window|scriptEditorPanel1|formLayout110|formLayout112|paneLayout2|paneLayout3|tabLayout2|formLayout132|cmdScrollFieldExecuter15
// get last cmdScrollFieldExecuter path and name:
print $gLastFocusedCommandExecuter;
// scriptEditorPanel1Window|scriptEditorPanel1|formLayout110|formLayout112|paneLayout2|paneLayout3|tabLayout2|formLayout132|cmdScrollFieldExecuter15
// get background color with use command cmdScrollFieldExecuter:
cmdScrollFieldExecuter -q -bgc $gLastFocusedCommandExecuter;
// Result: 0 0 0
// set space per tab 10, use command cmdScrollFieldExecuter (default = 4):
cmdScrollFieldExecuter -e -spt 10 $gLastFocusedCommandExecuter;
// set to default value:
cmdScrollFieldExecuter -e -spt 4 $gLastFocusedCommandExecuter;
Thanks! I actually did manage to get what I need after much searching and testing.
Looking at your example, it works in a similar fashion as mine as well
Here’s a snippet of it
cmds.tabLayout(gCommandExecuterTabs, edit=True, selectTabIndex=se_tabs_num)
mel.eval('selectCurrentExecuterControl;')
# This will paste the script file contents into the active tab
mel_cmd = f'delegateCommandToFocusedExecuterWindow("-e -loadFile \\"{scriptfile}\\"", -1)'
mel.eval(mel_cmd)
There’s no need to go the long and complicated route; just use existing global variables.
As I mentioned above, you can find the necessary global variables and procedures in the file .../scripts/startup/scripRditorPanel.mel.