Passing Variables To Mel Inside An Imported Script

i'm using the script at the bottom to create some nurbsplanes with clusters to deform them.

i couldn't find a python equivalent to SearchReplaceNames so i decided to pass that command to mel. the only way i could figure how to pass the variable to mel involves using python to run mel to run python, which doesn't strike me as efficient.

but if i cut and paste this code into the script editor, it works fine.

however, if i try to import it i get this error

CODE
import looptest
# Error: name 'wvs' is not defined
# Traceback (most recent call last):
#   File "", line 1, in ?
# NameError: name 'wvs' is not defined #
# Error: Error occurred during execution of MEL script
# Traceback (most recent call last):
#   File "", line 1, in ?
#   File "C:\Documents and Settings\Administrator\My Documents\maya\8.5\pyscripts\looptest.py", line 36, in ?
#     maya.mel.eval ( "int $wvs = python \"wvs\"")
# RuntimeError: Error occurred during execution of MEL script #

if i run it from the script editor, then import it in the same maya session, i don't get that error, but the variable doesn't get updated for mel so it renames everything wrong. so the hitch is almost definitely in passing the variable from python to mel.

anyone know what to do so i can import this?

CODE
import maya.cmds as cmds

#loop to create 4 wave planes
for wvs in range ( 1 , 5 ) :    
    cmds.nurbsPlane( n='wv01_ZZ_XX', p=[ 0,0,0 ], ax=[ 0,1,0 ], w=1, lr=1, d=3, u=400, v=1, ch=1 )
    cmds.setAttr ( 'wv01_ZZ_XX.scaleX', 200 )
    cmds.setAttr ( 'wv01_ZZ_XX.scaleZ', 40 )
    cmds.setAttr ( 'wv01_ZZ_XX.rotateX', 90 )
    cmds.makeIdentity( 'wv01_ZZ_XX', apply=True, t=1, r=1, s=1, n=0 )

    #loop to create clusters for each plane
    for pnts in range ( 0,16 ):    
        cmds.select ( "wv01_ZZ_XX.cv[" + str(pnts+2) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+18) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+34) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+50) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+66) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+82) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+98) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+114) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+130) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+146) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+162) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+178) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+194) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+210) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+226) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+242) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+258) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+274) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+290) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+306) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+322) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+338) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+354) + "][3]",
    "wv01_ZZ_XX.cv[" + str(pnts+370) + "][3]", "wv01_ZZ_XX.cv[" + str(pnts+386) + "][3]",r=True )
        cmds.cluster ( n="wv01_ZZ_XXcls" + str(pnts),  en=1 )

    cmds.select ( 'wv01_ZZ_XX.cv[0:402][0:2]', 'wv01_ZZ_XX.cv[0:1][3]', 'wv01_ZZ_XX.cv[402][3]', r=True)
    cmds.cluster ( n='wv01_ZZ_XXclsBase', en=1)
    cmds.select ( "wv01_ZZ_XXcls*Handle", r=True )
    cmds.group ( n='wv01_ZZ_XXclsCRV' )
    cmds.group ( 'wv01_ZZ_XXclsCRV', n='wv01_ZZ_XXclsGRP' )

    cmds.setAttr ('wv01_ZZ_XXclsGRP.translateZ', ((wvs -1) * 2) )
    cmds.select( clear=True )

    #pass variable to mel and replace names
    import maya.mel
    maya.mel.eval ( "int $wvs = python \"wvs\";" )

    maya.mel.eval ( "searchReplaceNames \"XX\" (\"0\" + $wvs) \"all\";" )

QUOTE
i couldn't find a python equivalent to SearchReplaceNames so i decided to pass that command to mel.

Your not supposed to either SearchReplaceNames is a mel script NOT a command. Theres not much point in exporting mel scripts. If your interested in how its done open the script file and port it to python.

QUOTE
the only way i could figure how to pass the variable to mel involves using python to run mel to run python, which doesn't strike me as efficient.

Well yes but thats the only way. Anyway dont worry about the efficency as your scipt is missing out 50 % of all its effect on extra select clasues the one call is insignificant. for your perfomance

QUOTE
if i run it from the script editor, then import it in the same maya session, i don't get that error, but the variable doesn't get updated for mel so it renames everything wrong. so the hitch is almost definitely in passing the variable from python to mel.

Thets because the script editro is in global space so every single call to a item has global prepended to it unseen to you offcourse a local variable dies immediately when you run it.

Kill your loop its unmanageable. or make it aloop within a loop

Anyway if you itend to script like this stick to mel itll be easier on you. Or migrate to pymel. Mainly because your scripting just by cut and paste.

while i of course appreciate words of encouragement, can anyone tell me how to make the local variable work or how to make it global if that's what's needed?

thanks for the loop within a loop idea. i was able to clean up that loop quite a bit.

CODE
for pnts in range ( 0,16 ):
    cmds.select( clear=True )
    for cvs in range ( 0, 25):
        cmds.select ( "wv01_ZZ_XX.cv[" + str(pnts+(cvs*16 + 2)) + "][3]", add=True )
    cmds.cluster ( n="wv01_ZZ_XXcls" + str(pnts),  en=1 )

also, i got rid of the searchReplaceNames and therefore don't need to pass variables anymore, but it would be nice to know how.

QUOTE(will meyer @ 03/05/08, 12:13 AM) [snapback]282193[/snapback]
while i of course appreciate words of encouragement, can anyone tell me how to make the local variable work or how to make it global if that's what's needed?

Well the problem is that you shouldn't make global variables unless you really really need them. And certainly if you do you should NOT call them $wvs, because you can get into LOT of trouble with it. especially if somebody else uses the same name (wich for short names happens more easily), So if your going to use global variables put global in front.

Also note the global problem exists here 2 times since also the pythn vws must be global if you intend to call it like that. In general you do not pass stuff etween systems like this. Its the entire idea of vriables. (anyway this means it saner to call maya.mel.eval ( "global int $wvs = "+str(wvs)) if you must but the whole deal of efficency flies out of the dor the minute you design code that passes globals string conversion or not)

However please don't do this, its like trying to fit a square peg in a round hole. It might go trough but never fits very well. Instead either store the value inside some node you can fetch it form later or use optionVar, fileInfo etc.

calling mel form python is inefficient because they arent the same thing. Mixing python and mel like this makes very little sense. Its just like making a c api program that jsut calls mel evals They are intentionally different.

Anway code like this is nt about code efficiency if it was youd be writing c++ and designing algorithms;