>
> import maya.mel as mel
> charRenderDir = "depts/rig/Beau/Character_Builds/ROM_Renders/" +  
> curChar + "/"
> mel.eval('string $dir = python("charRenderDir");' 'setProject $dir')

it should be pointed out that this not a very reliable technique.   
mel's python command executes in maya's global scope, which is the  
same scope that the script editor uses.  the code posted above will  
only work if executed in the script editor.  from a module it would  
have to be modified to ensure that the current module is imported and  
the value gotten as an attribute from that module:

########
import maya.mel as mel

charRenderDir = 'somevalue'
mel.eval('string $dir = python("import %s; %s.charRenderDir");' %  
( __name__, __name__) )

########

but this technique does not work at all if your current scope is  
inside a function when you use it, because variables defined inside a  
function are not accessible as attributes of the module or function.  
the following code, while it seems that it might work, will in fact  
raise an error:

########
import maya.mel as mel

def myfunc():
        charRenderDir = 'somevalue'
        mel.eval('string $dir = python("import %s;  
%s.myfunc.charRenderDir");' % ( __name__, __name__) )

myfunc()

########

and while you may find workarounds by using global variables or  
defining variables at the module level, these will lead you into a  
scoping nightmare.

long story short, if you want to use your python variable in a  
mel.eval statement, you're going to have to convert it to a string  
representing some mel code. in the given example, its quite simple:

charRenderDir = 'somevalue'
mel.eval('string $dir = "%s";' % charRenderDir )

it gets a bit more complicated when you want to use arrays:

values = [ 'one', 'two', 'three' ]
mel.eval('string $dir[] = {%s};' % ','.join( [ '"' + x + '"' for x in  
values] ) )

so, you can get used to doing that or use something like pymel to do  
it for you:

 >>> import pymel as pm
 >>> pm.pythonToMel( [ 'one', 'two', 'three' ] )
'{"one","two","three"}

and, as i mentioned before, if what you're calling is a mel procedure,  
then you can just use it as if it were a function, with no need to  
string format at all:

pm.mel.myProcedure( [ 'one', 'two', 'three' ] )

and getting mel globals:

pm.melGlobals['myGlobalVar']

-chad


>
> Cheers,
> Beau
>
> >


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to