cleaning up blender
Unused images can be removed with a script.

A Little Script for Cleaning up the Image Stack in Your Blendfiles

Recently I had to clean up a lot of my blendfiles. Especially when you append objects that share textures with your existing objects, you wind up with a lot of duplicates.

So I came up with a little script to unlink every image in the file. You can manually link the images to your textures again, and they will not get deleted next time you open your blendfile.

  1. will store all images' names that are used in any texture in an array.
The for loop will be repeated as often as there are image names in imgs.
user_clear() is a built-in method, which will set the number of users of an image, texture, datablock etc. to 0.

Some people may ask: what if I want to keep some of my images? Of course this is a good question. You could "tag" the images you want to keep by hand, by inserting a string at the beginning, let's say "keep".
Then use an if statement, so we only delete those images whose names do not start with "keep".


 The line if name[:4] != "keep": checks the first 4 characters in a string and whether they spell "keep". [:n] extracts the first n characters of a string [n:] the last n.
!= means not equal to so if the 4 characters of the name are not "keep", the image will be unlinked.

Still we have to do stuff by hand, which is not the goal when scripting. How about we write a script that removes all images that are not used in a texture slot?


Careful: all reference and background images will be removed as well.

So there you have it: a script that will unlink every image that is not used in a texture, making the cleanup so much easier.


OK, that was for Blender Internal materials, since Cycles works with nodes and not texture slots, the script is a bit more complicated.


So here is a little more explanation: nodes = mat.node_tree.nodes stores all nodes of the current material in a list. So the for loop will check every node whether it is a texture node or not. If it is a texture node, its image name will be stored in the array. I used try, because nam = raised an error in some cycles of the loop. If you are not sure if all texture nodes actually contain an image, you can use the try method. If your code causes an error, Python will do whatever comes after the except, ignoring the error. In our case it will just notify us that something did not go according to plan, but the script will not be aborted.
Warning: This script will not work if there are materials present that do not have "use Nodes" enabled.

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.