using * as variable value

44 views
Skip to first unread message

NAS18

unread,
Apr 7, 2017, 10:24:50 PM4/7/17
to Python Programming for Autodesk Maya
Hello,

May I know is it suitable to use * as variable value


The purpose function is to delete an selected object that prefix with myobj*  and delete it. 


thank you so much 

regards 

Nurul

Justin Israel

unread,
Apr 7, 2017, 10:49:58 PM4/7/17
to Python Programming for Autodesk Maya

From a python standpoint, yes any value is allowed. From a Maya standpoint, as you expected Maya will use * as a wildcard to match objects.

Also, you have a bug in your example. obj is being shadowed by each value in your loop, so the assignment to "object*" has no effect.

Justin


--
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/308b9f75-c0d0-4fd4-b48c-0a63921ec08d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

NAS18

unread,
Apr 7, 2017, 11:02:06 PM4/7/17
to Python Programming for Autodesk Maya
http://pastebin.ubuntu.com/24337966/

This is the original snippet,

It does the job but I still doubt with it..

I made a UI interface that generate sphere and cube . I wrote this snippet so it will act only on cube and only will delete the selected cube as I have a list of cube that start with myCube,myCube1 and etc.

I use the bug to ignore the error warning that i received .. like this  ## Error: ValueError: file <maya console> line 14: No object matches name: myCube1Shape # 

not sure whether I did the right thing or misuse the handling exceptions.

Thanks


On Saturday, April 8, 2017 at 3:49:58 AM UTC+1, Justin Israel wrote:

From a python standpoint, yes any value is allowed. From a Maya standpoint, as you expected Maya will use * as a wildcard to match objects.

Also, you have a bug in your example. obj is being shadowed by each value in your loop, so the assignment to "object*" has no effect.

Justin


On Sat, Apr 8, 2017, 2:24 PM NAS18 <nurulm...@gmail.com> wrote:
Hello,

May I know is it suitable to use * as variable value


The purpose function is to delete an selected object that prefix with myobj*  and delete it. 


thank you so much 

regards 

Nurul

--
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_maya+unsub...@googlegroups.com.

Alok Gandhi

unread,
Apr 7, 2017, 11:24:59 PM4/7/17
to python_in...@googlegroups.com

I use the bug to ignore the error warning that i received .. like this  ## Error: ValueError: file <maya console> line 14: No object matches name: myCube1Shape # 
not sure whether I did the right thing or misuse the handling exceptions.
It is perfectly ok to `pass` exceptions if you know what you are doing. This is a known technique in python, sometimes referred to as EAFP (stands for `It's easier to ask forgiveness than permission`). You can find more information here.
However, it is strongly advised not to use a bare `except`, which causes every exception to pass silently and not just the one you are anticipating. You should only except what you can handle. In your case, just use except ValueError:
def delcube(self, *args):
    sel = cmds.ls("myCube*", sl=True, dag=True, v=True, ud=False)
    myobj = "myCube*"
    for myobj in sel:
        if myobj == myobj:
            try:
                cmds.delete(myobj)
            except ValueError:
                # Shape Node already deleted
                pass

Alok Gandhi

unread,
Apr 7, 2017, 11:32:08 PM4/7/17
to python_in...@googlegroups.com
Ah and I forgot to mention, you should not use `myobj` for both the pattern matching and object in selection. It makes the code less clear. Better to use some other name. Here's what I suggest:
def delcube(self, *args):
    sel = cmds.ls("myCube*", sl=True, dag=True, v=True, ud=False)
    todelete = "myCube*"
    for myobj in sel:
        if myobj == todelete:
            try:
                cmds.delete(myobj)
            except ValueError:
                # Shape Node already deleted
                pass
also notice that the try, except should be inside the loop otherwise the for loop will run only once.

Justin Israel

unread,
Apr 7, 2017, 11:43:52 PM4/7/17
to python_in...@googlegroups.com

I still don't understand comparison between the current item from the selection and "myObj*". Can objects actually have a literal * in their name in Maya? When would this check actually pass?


--
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/CAPaTLMRcHUhvyCagcMLrKEDqrFEMWUav7PQrXzv0jpL%2BsOMzQQ%40mail.gmail.com.

Alok Gandhi

unread,
Apr 8, 2017, 12:41:08 AM4/8/17
to python_in...@googlegroups.com
I still don't understand comparison between the current item from the selection and "myObj*". Can objects actually have a literal * in their name in Maya? When would this check actually pass?
You are right. `*` will have no effect in python. There's a bug.
using:
myobj = 'myCube*'

and then:
if myobj == myobj:
    # delete ...
works, becuase `sel('myobj*', ....) already preselects all the object starting with 'myCube' and then myobj == myobj always evaluates to True (as you pointed the myobj = 'myCube*' is overridden by myobj in sel) thus deleting all the cubes only.

My code, with todelete = 'myCube*' will NOT work but myobj = 'myCube*' will work.

I think the best option would be a simpler:
def delcube(self, *args):
    todelete = cmds.ls("myCube*", sl=True, dag=True, v=True, ud=False)
    for obj in todelete:
        try:
           cmds.delete(obj)

NAS18

unread,
Apr 8, 2017, 1:20:31 AM4/8/17
to Python Programming for Autodesk Maya
thanks for the answers guys.

I tried to use this snippet but it does not work..It does not give any error but it does not pass the functionality to the button .but if I use myobj==myobj like old one .it works.even though it is working but still not satisfied. i'm curious. 

Alok Gandhi

unread,
Apr 8, 2017, 1:46:50 AM4/8/17
to python_in...@googlegroups.com
I tried to use this snippet but it does not work..It does not give any error but it does not pass the functionality to the button .but if I use myobj==myobj like old one .it works.even though it is working but still not satisfied. i'm curious. 
Look at my last post above, it exactly explains what you are looking for. 


Alok Gandhi

unread,
Apr 8, 2017, 1:47:54 AM4/8/17
to python_in...@googlegroups.com
Just to reiterate, use this, it will work and is much simpler:
def delcube(self, *args):
    todelete = cmds.ls("myCube*", sl=True, dag=True, v=True, ud=False)
    for obj in todelete:
        try:
           cmds.delete(obj)

NAS18

unread,
Apr 8, 2017, 2:25:56 AM4/8/17
to Python Programming for Autodesk Maya

it does work.. thank you so much  :)

Justin Israel

unread,
Apr 8, 2017, 3:15:14 AM4/8/17
to Python Programming for Autodesk Maya

And to explain again why your snippet didn't work and the myObject==myObject always passes, consider this..

1==1

And then

x=1
x==x

Basically comparing a value to itself is always going to pass.

Justin


--
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/1049c7e3-6afa-48e7-8579-7792713975a6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages