GenericMaterialsModel.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Logger import Logger
  4. from cura.Machines.Models.BaseMaterialsModel import BaseMaterialsModel
  5. class GenericMaterialsModel(BaseMaterialsModel):
  6. def __init__(self, parent = None):
  7. super().__init__(parent)
  8. from cura.CuraApplication import CuraApplication
  9. self._machine_manager = CuraApplication.getInstance().getMachineManager()
  10. self._extruder_manager = CuraApplication.getInstance().getExtruderManager()
  11. self._material_manager = CuraApplication.getInstance().getMaterialManager()
  12. self._machine_manager.activeStackChanged.connect(self._update) #Update when switching machines.
  13. self._material_manager.materialsUpdated.connect(self._update) #Update when the list of materials changes.
  14. self._update()
  15. def _update(self):
  16. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  17. global_stack = self._machine_manager.activeMachine
  18. if global_stack is None:
  19. self.setItems([])
  20. return
  21. extruder_position = str(self._extruder_position)
  22. if extruder_position not in global_stack.extruders:
  23. self.setItems([])
  24. return
  25. extruder_stack = global_stack.extruders[extruder_position]
  26. available_material_dict = self._material_manager.getAvailableMaterialsForMachineExtruder(global_stack,
  27. extruder_stack)
  28. if available_material_dict is None:
  29. self.setItems([])
  30. return
  31. item_list = []
  32. for root_material_id, container_node in available_material_dict.items():
  33. metadata = container_node.metadata
  34. # Only add results for generic materials
  35. if metadata["brand"].lower() != "generic":
  36. continue
  37. item = {"root_material_id": root_material_id,
  38. "id": metadata["id"],
  39. "name": metadata["name"],
  40. "brand": metadata["brand"],
  41. "material": metadata["material"],
  42. "color_name": metadata["color_name"],
  43. "container_node": container_node
  44. }
  45. item_list.append(item)
  46. # Sort the item list by material name alphabetically
  47. item_list = sorted(item_list, key = lambda d: d["name"].upper())
  48. self.setItems(item_list)