Deleting Unlinked Materials

Hi
I am trying to modify the VRML-97 import script so that objects with a common material in VRML have a common material in Blender. The original script automatically created new materials for each object. I am now able to have common materials but I am left with a no. of materials unlinked to any object(and hence wasting memory). Is there a way to delete these unlinked materials through Python?
Thanks
Ishan

Shouldn’t the unlinked materials be purged out of the .blend when you exit?

By the way, I’d be interested to hear how you got the common materials to work. I’m doing something similar to your project where I’d rather not create multiple copies of the same material when bringing in objects that share the same ones.

You can create vertex groups to share the same material

I’m working with svg and i had the same problem when i imported so i made this script

import Blender  
list = Blender.Object.GetSelected()
while list!=[] :
 elemento = list.pop()
 for i in list :
  col1 = elemento.getData().getMaterials()[0]
  for j in i.getData().getMaterials():
   if col1.getRGBCol()==j.getRGBCol() :
    i.getData().materials=elemento.getData().getMaterials()
    list.remove(i)

its simple but it works fine, you just select all the object and run the script, then you have to save the file and close blender. Open the file again an all the duplicated materials should be gone.
This just checks the rgb
(be careful not to select objects without material or better add an if to my script)

@Juako, do you work with vectex? (i’m having troubles with this image plugin).
Edit: Oh! I guess you meant for modeling, not texturing

Yes i use it, but i found its easier to import everything as curves, because its faster at rendering time. vectex works, but you have to be careful with the memory

@juako: I switched to Ubuntu Studio and cannot make it work! :frowning:
@ish90an:
if the “F” button is pressed, besides the materials, there is a Fake user to prevent them from being deleted after you close the program and start it again (they should be deleted if there is no user (check from the OOPS shematic) linked to it and if the F is unchecked

I had problems with blender and ubuntu, i couldn t solve some problems for example every time i touch shift and the mouse middle wheel it pick the window and i couldn’t find the blender console. I didn’t new about ubuntu studio it looks nice

By the way, I’d be interested to hear how you got the common materials to work. I’m doing something similar to your project where I’d rather not create multiple copies of the same material when bringing in objects that share the same ones.

I got the script to first create a new material. While parsing the script loses the original material names:(, so I store the properties(and name) of each material that has already been linked in a dictionary and compare the properties of the material to be linked with the materials already linked. If I get a match with all the properties I link the object to the used material and and unlink the material I temporarily created. I know its not the most effective way, and I would like it to just compare material names, but its a problem with the parser being used and I really dont want to change that, since its being used in converting from some other formats too.

The unused materials are purged after I exit once. But I am looking for a way to not have to exit to get rid of those materials. Isnt there any way to do this without exiting?
Thanks
Ishan

@juako no Shift click Panning problem at all with Ubuntu Studio
and to have console output Blender needs to be started from a terminal (check the option “start from a terminal” in its launcher)
@ish90an
If you can save and reopen file (no need to close blender), that would do it, though there might be a python command or workaround to clear unused datablocks. If you actually find one, it would fit well in this thread