0

I have some trouble in finding ways to call python functions inside of a mel script.

Basically, I have a few Mel scipts in which one creates a new shelf in Maya and inside it, are a few lighting tool button(spotLight, areaLights etc.)

As I am still a noob in scripting, not to mention that so far I have only been studying in python, as for mel I know a little to none. This is quite a big obstacle for me. I am trying not to edit any of the scripts too much, so as to reduce confusion and any errors that my occur.

  1. In my startup code, there are a few button which uses .xpm and .png for its icon. In the last few lines, the ext initially is .xpm and I change it to .png which is considered to be wrong after I realised that I am still in need of some .xpm icons. So is there a OR function in MEL?

    global proc shelf_lighting() {
    
        if (Dpublishmel("objGetMultiConfigs \"objkind\" \""+$show+"\" \"\" \"\" \"maya\" 1", $result)) {
            string $kindRows[] = stringToStringArray($result[0], ";;");
            $objkinds = $kindRows;
        }
    
        string $aboutString = `about -v`;
        string $aboutStringArr[] = stringToStringArray($aboutString, " ");
    
        string $versionStringTemp = $aboutStringArr[0];
        string $versionString = match("[0-9]+[\.]*[0-9]+", $versionStringTemp);
        float $version = $versionString;
    
        string $pycmd = "evalDeferred \"python(\\\"^1s\\\")\"";
    
        string $ext = ".png";
            if ( int($version) > 2009 ) {
                $ext = ".png";
            }
    }
    
  2. I am trying to change the command from "source \"rexLightFuncs.mel\";\nrexSpotLightCreate \"\";"`; to the one as shown in the code below. It is a python function and I am trying to incorporate it into mel

    import DLightBuild.DLightBuild as build
    light=build.DLightBuild();light.createLight('spotLight');
    

    The file of this python commands comes from /tools/utils/maya/python/DLightBuild, containing the .py file - DLightBuild, and for some reason, it is not working as I derived this from an example I have seen somewhere in my google search.

    $spotLightButton = `shelfButton
        -enableCommandRepeat 1
        -enable 1
        -width 34
        -height 34
        -manage 1
        -visible 1
        -label "Create a Spot Light"
        -image1 "spotLight.png"
        -style "iconOnly"
        -command "import sys;sys.path.insert(0, \"/tools/utils/maya/python/DLightBuild/\");import DLightBuild.DLightBuild as build;reload(build);light=build.DLightBuild(); light.createLight('spotLight')" 
    -sourceType "python";
    
  3. Lastly, whenever when I tried to do a test run of my code, to see if the icon image are right and if it is performing the correct function... If I run it just by the shelfButton as seen in the second part of the code (in Mel by the way), it creates a button at the end of my scriptEditor (it is like another docking window below) instead of in my existing shelf or in a new shelf. Any advices on this?

Appreciate for any advices given for any of the questions raised. Many thanks in advance

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
dissidia
  • 1,531
  • 3
  • 23
  • 53

1 Answers1

0

Mel does have a logical or operator ||, but it only does logical OR tests on numbers and only returns 1 or 0, so it's not too helpful; To check for img or png extensions it would be something like

 int $is_valid = gmatch($filename, "*.png") || gmatch($filename, "*.xbm");

you can always wrap mel in python like this:

import maya.mel as mel
mel.eval("your mel command here as a string");

to call python from mel you can do

python ("python goes here as a string");

Mixing python and mel like this is always annoying, you might want to consider biting the bullet and converting old Mel scripts to python so you don't have to live with this forever. Once you've done a few the conversions are pretty straightforward, the python version is simpler and cleaner in 99% of cases.

so in your example you could wrap your current command in "python()"; Working out the correct escaping is an annoyance.

theodox
  • 12,028
  • 3
  • 23
  • 36
  • 1
    Currently when I run my Mel code as a whole, I keep getting the syntax errors `Error: Line 852.140: Syntax error //` and I can't figure the hell out of me where it is wrong.. If i remove or add a `;`, it will deem my next line as an undeclared variable It usually happens somewhere between this 2 lines: `-command "python(\"import DLightBuild.DLightBuild as build; light=build.DLightBuild();light.createLight('spotLight')\")";` and `$sunLightBtn = shelfButton` – dissidia Mar 14 '14 at 08:36
  • Are there returns in your string? – theodox Mar 14 '14 at 17:45
  • turns out the syntax error is due to the missing of ` symbol. Problem solved – dissidia Mar 17 '14 at 02:42