12

With large projects that have hundreds of textures, it can be very difficult to deal with them in the UV image editor using the following little drop-down menu.
image data block menu

The best solution that I've come with so far is to modify the UV Image Editor code using some code from the Sculpt and Texture Paint section which is very helpful but it's still difficult to tell if something is in use and it has to be reopened every time something is selected and deleted.

(It now looks like the following in case anyone is interested. You can do this by menu-clicking the image icon next to New and Open then selecting edit source and changing the following line in space_image.py

layout.template_ID(sima, "image", new="image.new", open="image.open") 

to be

*layout.template_ID_preview(sima, "image", new="image.new", open="image.open", rows=8, cols=16*)    

changed image menu

This question deals with deleting textures and it's what I already do. But it's time consuming and very difficult to find the unused textures when there are a couple hundred of them, especially if the drop down menu has to be reopened and searched for everyone one of them.

So my question is the this:
Is there a way to delete all the unused textures using a Python script?

I have found one script that does not work very well. Sometimes it does and sometimes it does not.
This gives the impression that there is something going on behind the scenes when it comes to how textures are considered to be in use.

By the way, I don't use Fake User or pack images so this may simplify things a bit.

MarcClintDion
  • 9,641
  • 5
  • 32
  • 58
  • 2
    If the datablocks are unused, they should be cleared out when you reload the .blend.. – gandalf3 Jul 08 '14 at 09:08
  • The problem is having to reopen and search the menu over and over again since it closes when something is selected. Also, it's not always clear what needs to go just from looking. – MarcClintDion Jul 08 '14 at 09:26
  • I'm not sure I understand.. Couldn't you just save the .blend and re-open it? (Ctrl+S, then Ctrl+Shift+O 1) That should clear out all datablocks with no users. – gandalf3 Jul 08 '14 at 17:46
  • I do that but it does not work all the time. I've even tried deleting every last object from the scene, saving, then reloading it, but most of the textures were still there even though there were not any models. This works fine for removing materials but textures are really hard to get rid of. – MarcClintDion Jul 09 '14 at 02:18
  • I just tried what I said about deleting all the objects, then saving and reloading. Most, or all the textures were still there, but when I fully shutdown Blender and reloaded the empty scene then only 17 of the textures are still there. I guess this should be a bug report. (This is without using the posted script). When I ran the script, another 12 disappeared right away. 3 were loaded from Texture Paint and 2 were from a model that was not there. – MarcClintDion Jul 09 '14 at 23:57
  • Hm.. It sounds like something strange is going on, but I have no idea what. AFAIK reloading the .blend should be the same as restarting blender, and reloading the .blend should remove every datablock with 0 users (images with a little 0 next to them in the menu). – gandalf3 Jul 10 '14 at 00:22
  • I just decided to check back in so I can load the .blend for you to have a look at. When I reopen Blender and load this file, there are 41 textures listed in the UV Image Editor. I've attached it to my question as an Edit. – MarcClintDion Jul 10 '14 at 02:27
  • When opening the .blend here, there are ~28 textures, and no textures with 0 users. Which is what I would expect. – gandalf3 Jul 12 '14 at 00:42
  • Yeah, I wish I had looked at it more carefully. There were models that I missed. I even filed a bug report because I did not see your comment. I'm super embarrassed about this one. I was sure I deleted all the models. – MarcClintDion Jul 16 '14 at 09:04
  • No worries, I've reported my own silly mistakes as bugs often enough.. – gandalf3 Jul 16 '14 at 09:10
  • I was recently listening to a podcast interview with Kent Trammel and he said that he never reports bugs because he is worried about making a fool of himself. Now I really get it. Anyways, thanks for your help with this. – MarcClintDion Jul 16 '14 at 12:19
  • The reason for my confusion 2 years ago is a lot more clear now. The .blend file must be saved and reloaded twice before image data is finally gone for good. Now I know to press Ctrl+S then Ctrl+O twice in a row when images are stuck. – MarcClintDion Aug 03 '16 at 23:21
  • 1
    Happily that's one of the things that's being refactored for 2.8 (some of it might even be in 2.78) – gandalf3 Aug 04 '16 at 02:09

2 Answers2

19

Remove all images with zero users from the blendfile:

import bpy

for img in bpy.data.images:
    if not img.users:
        bpy.data.images.remove(img)
pink vertex
  • 9,896
  • 1
  • 25
  • 44
-2

fix as 0 use ...press ctrl and delete " texture , material and others" and after, save your work