I'm trying to build a gui for an asset-browser for maya. Im using pyqt and trying to learn how models and qListviews work. Now I have come to the loading of thumbnails which is a bit troublesome.
I have something that works like this:
- A list of files, in this case just thumbnail paths.
- I then put these paths into a custom object class which stores all my data in a hierarchical tree structure.
- I then put this nodeTree into a QAbstractItemModel and using the DecorationRole to display my thumbnails in the connected qListview.
The problem is that I load these images from a network location so this process of populating the thumbnails in the listview takes awhile. I also scale my thumbnails which also takes a fair amount of time.
To speed thing up I decided to have a look at QThread.
So instead of loading and manipulating my images in the decorationRole I do so in a separate thread. I then pass the finished loaded and scaled image as a qImage into my hierarchical tree structure. As the nodeTree updates the model gets updated and my decorationRole catches my newly created qImages and displays them.
So everything works fine, the thumbnails loads quick and everything updates as expected.
The only problem I have is that Maya allocates almost 500mb of memory when all thumbnails are loaded, there are like 100 of them for now. I can live with that memory footprint but what I'm more concerned with is that it doesn't release this memory when I close my window. And if I run my script again and open a new window I get another 500mb allocated. For every time I start my application it increases the ram-usage by roughly the same amount.
How can I make maya release all objects and thumbnails from memory when I close my script? Is this a memory leak or is the this behaviour expected?
If you have any clue of whats going on or what to do it would be highly appreciated!
thanks
/Erik
As for the memory issue, are you using the same instance of your UI class or are you instantiating it every time? It should share most of the same memory if you use the same instance.
Also, if you are creating an instance, python will keep the memory alive I believe until that instance is nulled. You can also remove widgets in the pyqt classes, to make sure Qt marks it for garbage collection too.
so if you have x= MyClass(), once you run x=None, Python will put it up for garbage collection.
I setup an http server to host images on and used a QThreadPool as a downloader. It worked quite well, but I had call on two 3rd party libraries: httplib2 and poster.
Closing the window won't destroy the actual object by default. There are window flags that can make that happen. But all you need to do is call myWindow.deleteLater() to destroy the Qt object. It will delete all of the child objects in turn. So make sure you properly set the parent attribute of child widgets. It would be a mistake to make anything except top level windows a child of the Maya window. Most of your objects should be descendants of your own window.
QPixmapCache is a Singleton with access via static methods. So you just call stuff like insert and find to set and get pixmaps from the cache. You can change the limit of the storage probably near the top of your module. Though being that Maya is a long running python session, be aware that more than one script all share the same cache.