I’ve been scripting a while but am an artist with no formal training.
I’ve been struggling with the docs on bpy and mesh and so am still using nmesh…
here’s a code example that works for my needs but I’d love to know how to do it properly!
can anyone give me some pointers on how it could be done without nmesh?
I just can’t seem to understand how to do it otherwise…
(this script is to clean up un-used material indices’s…)
import bpy import Blender from Blender import * from bpy import* #------------------------------------------------- def main(): scn = data.scenes.active for ob in scn.objects.context: if not ob.lib and ob.type == 'Mesh': # object isn't from a library and is a mesh #get number of materials on mesh me = ob.getData(mesh=1) nme=NMesh.GetRaw(me.name) mats = nme.materials matcount = len(mats) #check the faces on the mesh to build a list of used materials usedMatIndex = #we'll store materials indices here for f in me.faces: #get the material index for this face... faceindex = f.mat #check if it's already listed or not found = 0 for m in usedMatIndex: if m == faceindex: found = 1 break if found == 0: #add this index to the list usedMatIndex.append(faceindex) #find the un-used materials: highest index first! index = matcount -1 unused = while index>= 0: found = 0 #check if index is not used for i in usedMatIndex: if index == i: #used found = 1 if found == 0: #not used unused.append(index) index -= 1 print"used material index are:" print usedMatIndex print"material index to delete are:" print unused #delete them! for u in unused: del nme.materials[u] nme.update() return 1 done = main() if done == 1: print "done" Draw.PupMenu("Done%t|OK") else: print "Nothing Done"