deleting instances in maya 2018

35 views
Skip to first unread message

vilkdage

unread,
Nov 16, 2017, 6:38:17 PM11/16/17
to Python Programming for Autodesk Maya


Hi, can someone explain me 5th line? I get path to GUI class name from "<class '{}.{}'>".format(os.path.splitext(os.path.basename("__file__")[0]),self.__class__.__name__) and all maya window Pyside2 instances in str(type(obj)). It seems because maya cannot find my widget among mainWindow widgets it cannot move on and remove it causing widget name to be not unique.

How come my widget is not in a list of maya window widgets?
   
def deleteInstances(self):
        mayaMainWindowPtr
= omui.MQtUtil.mainWindow()
        mayaMainWindow
= wrapInstance(long(mayaMainWindowPtr), QtWidgets.QMainWindow)
       
for obj in mayaMainWindow.children():
           
if str(type(obj)) == "<class '{}.{}'>".format(os.path.splitext(os.path.basename("__file__")[0]),self.__class__.__name__):
               
if obj.__class__.__name__ == self.__class__.__name__:
                   
print 'Deleting instance {0}'.format(obj)
                    mayaMainWindow
.removeDockWidget(obj)
                    obj
.setParent(None)
                    obj
.deleteLater()        

Justin Israel

unread,
Nov 16, 2017, 7:01:23 PM11/16/17
to python_in...@googlegroups.com
    if str(type(obj)) == "<class '{}.{}'>".format(os.path.splitext(os.path.basename("__file__")[0]),self.__class__.__name__):

I find this line you mentioned quite confusing, for a few reasons. First, getting the basename of a string literal "__file__" is not doing what you probably expect. It was probably meant to be used without the quotes, as the magic variable for the current file name.

Why was this particular approach used with the goal of finding out if the object is an instance of the current class? Do these other options not work?
  1. isinstance()
  2. obj.__class__ is self.__class__
  3. hasattr() duck typing
Relying on the string representation comparison seems like it would easily fail.

Justin

On Fri, Nov 17, 2017 at 12:38 PM vilkdage <vilk...@gmail.com> wrote:

vilkdage

unread,
Nov 19, 2017, 3:10:09 PM11/19/17
to Python Programming for Autodesk Maya
This approach seemed like the easiest way to find newly created widget objects. I could not make any of your suggestion work, but what did work was querying and editing workspaceControl, and deleteUI. Thanks!
Reply all
Reply to author
Forward
0 new messages