Checking variable existence before assigning it

179 views
Skip to first unread message

Piotr Makal

unread,
Nov 18, 2013, 8:58:39 AM11/18/13
to python_in...@googlegroups.com
Ok. firstly - I'm new to Python in Maya and my English is not so great either so pardon me that ^^

While I was learning MEL and Python I came across this problem which I can't figure out. So to be more clear I'll show my MEL code first and then it's Python equivalent.

string $objAligner;

if (`window -ex $objAligner`) {

   deleteUI -window $objAligner;

}


string $objAligner = `window -t "Object Alignment Tool" -s false -wh 300 100 -ret`;

columnLayout -adj true;

text -l "Instructions: select targets then affected object";

button -l "Execute" -w 300 -h 40 -c "aligner";

showWindow $objAligner;


proc aligner() {

   string $prntCnstr = `parentConstraint`;

   delete $prntCnstr;


}


import maya.cmds as mc

if mc.window(objAligner, ex=True) :

   mc.deleteUI(objAligner, window=True)


objAligner = mc.window(title='Object Alignment Tool', s=False, wh=(300, 100), ret=True)

mc.columnLayout(adj=True)

mc.text(l='Instructions: select targets then affected object')

mc.button(l='Execute', w=300, h=40, c='aligner()')

mc.showWindow(objAligner)


def aligner() :

   prntCnstr = mc.parentConstraint()

   mc.delete(prntCnstr)


So what I want to achieve is checking if window I have created is open and if it is then I want to close it and create new one in the same place.
In MEL everything is ok because I can execute the first line of code without anny errors: string $objAligner;
In Python though, we create variable when we assign value to a name so if I open Maya and execute script shown above I get an error of undefined name "objAligner".
Is there any way to solve this?

Joe Weidenbach

unread,
Nov 18, 2013, 11:18:18 AM11/18/13
to python_in...@googlegroups.com
Hi Piotr!

Welcome!  What I like to do for this is initialize the variable to whatever I want to call the window.  It's one of the few UI names I will set explicitly, and you only need to do this for the initial parent object.  Like so:

objAligner = "myWindowName"

if cmds.window(objAligner, exists=True):
	cmds.deleteUI(objAligner)

objAligner = mc.window(objAligner, title='Object Alignment Tool', s=False, wh=(300, 100), ret=True)

It's just a matter of declaring the variable first.

Hope that helps!

Joe


On 11/18/2013 5:58 AM, Piotr Makal wrote:
Ok. firstly - I'm new to Python in Maya and my English is not so great either so pardon me that ^^

While I was learning MEL and Python I came across this problem which I can't figure out. So to be more clear I'll show my MEL code first and then it's Python equivalent.

string $objAligner;

if (`window -ex $objAligner`) {

   deleteUI -window $objAligner;

}

string $objAligner = `window -t "Object Alignment Tool" -s false -wh 300 100 -ret`;

columnLayout -adj true;

text -l "Instructions: select targets then affected object";

button -l "Execute" -w 300 -h 40 -c "aligner";

showWindow $objAligner;

< span style="color: #000;" class="styled-by-prettify">

proc aligner() {

   string $prntCnstr = `parentConstraint`;

   delete $prntCnstr;

}

import maya.cmds as mc

if mc.window(objAligner, ex=True) :

   mc.deleteUI(objAligner, window=True)

objAligner = mc.window(title='Object Alignment Tool', s=False, wh=(300, 100), ret=True)

mc.columnLayout(adj=True)

mc.text(l='Instructions: select targets then affected object')

mc.button(l='Execute', w=300, h=40, c='aligner()')

mc.showWindow(objAligner)

def aligner() :

   prntCnstr = mc.parentConstraint()

   mc.delete(prntCnstr)

So what I want to achieve is checking if window I have created is open and if it is then I want to close it and create new one in the same place. In MEL everything is ok because I can execute the first line of code without anny errors: string $objAligner; In Python though, we create variable when we assign value to a name so if I open Maya and execute script shown above I get an error of undefined name "objAligner". Is there any way to solve this?

      
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2a6f3bd4-a4e2-487b-96af-29b6fdae18f7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Joe Weidenbach

unread,
Nov 18, 2013, 11:22:41 AM11/18/13
to python_in...@googlegroups.com
A quick note, I use cmds where you use mc.  So the code for you would be:

objAligner = "myWindowName"

if mc.window(objAligner, exists=True):
	mc.deleteUI(objAligner)

objAligner = mc.window(objAligner, title='Object Alignment Tool', s=False, wh=(300, 100), ret=True)


      
Just so that you don't run into problems with my half-awake code :)


On 11/18/2013 8:18 AM, Joe Weidenbach wrote:
Hi Piotr!

Welcome!  What I like to do for this is initialize the variable to whatever I want to call the window.  It's one of the few UI names I will set explicitly, and you only need to do this for the initial parent object.  Like so:

objAligner = "myWindowName"

if cmds.window(objAligner, exists=True):
	cmds.deleteUI(objAligner)

objAligner = mc.window(objAligner, title='Object Alignment Tool', s=False, wh=(300, 100), ret=True)

It's just a matter of declaring the variable first.

Hope that helps!

Joe

On 11/18/2013 5:58 AM, Piotr Makal wrote:
Ok. firstly - I'm new to Python in Maya and my English is not so great either so pardon me that ^^

While I was learning MEL and Python I came across this problem which I can't figure out. So to be more clear I'll show my MEL code first and then it's Python equivalent.

string $objAligner;

if (`window -ex $objAligner`) {

   deleteUI -window $objAligner;

}

string $objAligner = `window -t "Object Alignment Tool" -s false -wh 300 100 -ret`;

columnLayout -adj true;

text -l "Instructions: select targets then affected object";

button -l "Execute" -w 300 -h 40 -c "aligner";

showWindow $objAligner;

& lt; span style="color: #000;" class="styled-by-prettify">

proc aligner() {

   string $prntCnstr = `parentConstraint`;

   delete $prntCnstr;

}

import maya.cmds as mc

if mc.window(objAligner, ex=True) :

   mc.deleteUI(objAligner, window=True)

objAligner = mc.window(title='Object Alignment Tool', s=False, wh=(300, 100), ret=True)

mc.columnLayout(adj=True)

mc.text(l='Instructions: select targets then affected object')

mc.button(l='Execute', w=300, h=40, c='aligner()')

mc.showWindow(objAligner)

def aligner() :

   prntCnstr = mc.parentConstraint()

   mc.delete(prntCnstr)

So what I want to achieve is checking if window I have created is open and if it is then I want to close it and create new one in the same place. In MEL everything is ok because I can execute the first line of code without anny errors: string $objAligner; In Python though, we create variable when we assign value to a name so if I open Maya and execute script shown above I get an error of undefined name "objAligner". Is there any way to solve this?

Piotr Makal

unread,
Nov 18, 2013, 11:52:16 AM11/18/13
to python_in...@googlegroups.com
Ok I think I get it. I looked up on window() command in Maya Python documentation and I found optional [string] at the begining, so I'm guessing that is the name of the window and return value also giving me that tip. So many thanks to you Joe for you answer! :)
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Joe Weidenbach

unread,
Nov 19, 2013, 1:21:44 AM11/19/13
to python_in...@googlegroups.com
Yeah, 100% right.  Maya's Python cmds module treats every Maya object as a string (just like MEL).  Most commands take a string as their optional first argument, and if it is provided they operate on the object named in that string.  They also usually return either a string or a list of the resulting operation.  In the case of UI, it will return the new (or old) object's name.  This is very useful when it comes to renaming objects, as you can then capture the new name in the old variable.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/c28006a6-d1e8-4fb6-affc-831f9f241d9f%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages