How to call MEL script from Python

How to call MEL script from Python

rafGCQG8
Contributor Contributor
7,099 Views
2 Replies
Message 1 of 3

How to call MEL script from Python

rafGCQG8
Contributor
Contributor

Hi there,

 

I have made a python script which performs functions on a mesh that has had a toon outline assigned to them, but I want to skip the manual creation of the toon outline and handle that from my script also. So instead of navigating to Toon > Assign Outline > Add New Toon Outline the Python script would do this on the selected mesh.

 

Looking through program files ( C:\Program Files\Autodesk\Maya2023\scripts\paintEffects ) I see there is a .mel script called assignPfxToon.mel which I think is called when 'Add New Toon Outline' is navigated to and clicked, but I have not been able to call that from Python. Is there another way to go about handling the assignment of the new toon outline, or can anyone help me in the way I have described? I'd be very grateful for any help, thank you. 🙂

 

for extra details in case needed, the python script is a drag and drop plugin install into maya, which when assigned in the hotkey editor to appear has a series of buttons on it that perform functions on the mesh when clicked 

0 Likes
Accepted solutions (2)
7,100 Views
2 Replies
Replies (2)
Message 2 of 3

rafGCQG8
Contributor
Contributor
Accepted solution

The solution I found is importing maya.mel in python, and then proceed to call whatever script you wanted. For example, the below snippet solved the problem described:

 

import maya.mel as Mm

Mm.eval("assignNewPfxToon;")

 

Message 3 of 3

jmreinhart
Advisor
Advisor
Accepted solution

Step 0. Locating the proc

If you already know the mel proc you want to call then skip this step.

Turn on "echo all commands" in the script editor (it's under history at the top of the window).

Perform the action manually through the UI.

Take a look at the script editor contents.

For assigning a new outline I get this:

editMenuUpdate MayaWindow|mainEditMenu;
buildCartoonMenu MayaWindow|mainCartoonMenu;
ToonCreatePfxOutlineMenu MayaWindow|mainCartoonMenu|toonCreatePfxOutlineItem;
AssignNewPfxToon;
assignNewPfxToon;
ls -selection -type "transform";
ls -selection -type "transform";
ls -selection -type "transform";
ls -selection -type "transform";
ls -selection -type "transform";
ls -selection -type "transform";
setFilterScript "initialShadingGroup";
// Result: 0 // 
setFilterScript "initialParticleSE";
// Result: 0 // 
setFilterScript "defaultLightSet";
// Result: 1 // 
setFilterScript "defaultObjectSet";
// Result: 1 // 
setFilterScript "cluster1Set";
// Result: 0 // 
setFilterScript "initialShadingGroup";
// Result: 0 // 
setFilterScript "initialParticleSE";
// Result: 0 // 
editMenuUpdate MayaWindow|mainEditMenu;
autoUpdateAttrEd;
CBselectionChanged;
dR_updateCommandPanel;
hikControlRigSelectionChangedCallback;
$gHIKneedSyncOnSetKeyframe=1;
// Result: 1 // 
updateAnimLayerEditor("AnimLayerTab");
statusLineUpdateInputField;
if (!`exists polyNormalSizeMenuUpdate`) {eval "source buildDisplayMenu";} polyNormalSizeMenuUpdate;
dR_updateCounter;

Look for a line or group of lines that look relevant to what you are doing, and you can narrow it down to 

AssignNewPfxToon;
assignNewPfxToon;

Test out these commands to see what they do, or look in the mel files they come from. You can also use

mel.eval('whatIs ...')

to find out more info.

In this particular case it looks like AssignNewPfxToon just calls assignNewPfxToon.

 

Step 1. Locating the .mel

You can skip this if you already know what mel file contains the proc you want to use.

If you don't then use the UI menus to run it once, then run this code

import maya.mel as mel
mel.eval('whatIs assignNewPfxToon')

which will return

// Result: Mel procedure found in: C:/Program Files/Autodesk/Maya2022/scripts/paintEffects/assignNewPfxToon.mel // 

So you can easily find the .mel file that you need to source

 

Step 2. Sourcing the .mel

If you are able to run your mel command from a fresh Maya then skip this step.

 

 

You'll notice that if you try to run certain MEL procs in a fresh Maya it will tell you it doesn't exist, but if you've run it once through the UI, then it will exist. This is because Maya only sources .mel functions as-needed.

import maya.mel as mel
mel.eval('source "theFileIWant.mel"')

If you want to detect if the file has already been sourced, you can use this code

import maya.mel as mel
if mel.eval('exists yourMelProc'):
    print 'IT EXISTS'
else:
    print 'IT DOES NOT EXIST')

 

Step 3. Run the proc

mel.eval('my my code')

 

 

Some of that is stuff you probably already know, I just wanted to be thorough since this is a pretty common problem people run into.