Materials | Script to move Materials from Sub Classes to under Root 3.0

Move Materials from Sub-Classes

You have created (too) many Sub Classes under Root to hold your Material Definitions or too many Materials under Root
or under any Node (Class/Sub Class).

Note We recommend that the Classes or Sub Classes hold no more than 20 Materials.

Because each time an Object is modified, that Object and Objects that belong in the same Class hierarchy have a version field incremented, this is now considerably slowing down editing

Warning It is not advised to start any new runs during the Script execution time.

This Script will move all classes to be under the root while still retaining a similar name to the previous path.

This does leave some empty classes.

All the names were able to fit the existing name limit. 

  • It is recommended to execute this Script in a Gateway Script on Tag Change for example to avoid Time Out 
    if executed in the Script Console
    as always : backup your GW and your MES DB and/or test on a copy of your Production

    Python
    mes_filter = system.mes.object.filter.createFilter()
    mes_filter.setMESObjectTypeName('MaterialClass')
    material_classes = system.mes.loadMESObjects(mes_filter)
    mes_filter.setMESObjectTypeName('MaterialRoot')
    material_root = system.mes.loadMESObjects(mes_filter).get(0)
    save_list = system.mes.object.list.createList()
    materials = {}
    for material_class in material_classes:
    materials[material_class.getUUID()] = material_class
    sep = '-'
    def flatten_name(obj):
    parent = obj.getParentCollection().getList().get(0)
    if parent.getMESObjectUUID() != material_root.getUUID():
    print materials[parent.getMESObjectUUID()]
    prefix = flatten_name(materials[parent.getMESObjectUUID()])
    name = obj.getName()
    if prefix != '':
    name = prefix + sep + name
    return name
    else:
    return ''
    for material_class in material_classes:
    parents = material_class.getParentCollection()
    if parents.size() == 1:
    parent = parents.getList().get(0)
    if parent.getMESObjectUUID() != material_root.getUUID():
    material_class.setPropertyValue('Name', flatten_name(material_class))
    material_class.removeParent(materials[parent.getMESObjectUUID()])
    material_class.addParent(material_root)
    save_list.add(material_class)
    system.mes.saveMESObjects(save_list)

Related articles

Related issues