Materials | How to Delete all Materials in a Material Class via Script

Deleting all Materials in a Material Class

First, check the properties for the Material Object(s):

There is no option to actually delete a Material, in the MES world we rarely delete objects, instead we disable them and this is exactly what you need to do here using

setEnable(disable)

so first you will load the MES Object as described in the Material Object function link and next, you will disable it.

This Script will work to Disable All Material under a Class and Sub Classes in the Class

Python
# The name of the Material Class that you want to disable all children for:
materialClassName = "<change to the target Class Name>"

def disableMaterials(materialClass):
saveList = system.mes.object.list.createList()
for childLink in materialClass.getChildCollection().getList():
if "MaterialDef" == childLink.getMESObjectType().getName():
childObject = childLink.getMESObject()
childObject.setEnabled(False)
saveList.add(childObject)
system.mes.saveMESObjects(saveList)
print "Disabled " + str(saveList.size()) + " Material Definitions for " + materialClass.getName()
for childLink in materialClass.getChildCollection().getList():
if "MaterialClass" == childLink.getMESObjectType().getName():
childObject = childLink.getMESObject()
childObject.setEnabled(False)
disableMaterials(childObject)
childObject.getChildCollection().clear()
system.mes.saveMESObject(childObject)
print "Disabled Nested Material Class: " + childObject.getName()
targetMatClass = system.mes.loadMESObject(materialClassName, "MaterialClass")

disableMaterials(targetMatClass)