mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-03 20:13:56 -06:00
Move createMaterial to MaterialManagementModel and simplify it a bit
We can reuse our duplicateMaterial function again but in a simpler way. Also finding the preferred material is simpler with our container tree. However there seems to be a problem with finding the preferred material; it's not finding generic_pla for UM3 and AA0.4 anyway, and then falls back on a random material. This needs to be fixed in the variant node class. Contributes to issue CURA-6600.
This commit is contained in:
parent
cb344f9dec
commit
3952366798
2 changed files with 37 additions and 2 deletions
|
@ -4,15 +4,20 @@
|
||||||
import copy # To duplicate materials.
|
import copy # To duplicate materials.
|
||||||
from PyQt5.QtCore import QObject, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page.
|
from PyQt5.QtCore import QObject, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page.
|
||||||
from typing import Any, Dict, Optional, TYPE_CHECKING
|
from typing import Any, Dict, Optional, TYPE_CHECKING
|
||||||
|
import uuid # To generate new GUIDs for new materials.
|
||||||
|
|
||||||
|
from UM.i18n import i18nCatalog
|
||||||
from UM.Logger import Logger
|
from UM.Logger import Logger
|
||||||
|
|
||||||
import cura.CuraApplication # Imported like this to prevent circular imports.
|
import cura.CuraApplication # Imported like this to prevent circular imports.
|
||||||
|
from cura.Machines.ContainerTree import ContainerTree
|
||||||
from cura.Settings.CuraContainerRegistry import CuraContainerRegistry # To find the sets of materials belonging to each other, and currently loaded extruder stacks.
|
from cura.Settings.CuraContainerRegistry import CuraContainerRegistry # To find the sets of materials belonging to each other, and currently loaded extruder stacks.
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from cura.Machines.MaterialNode import MaterialNode
|
from cura.Machines.MaterialNode import MaterialNode
|
||||||
|
|
||||||
|
catalog = i18nCatalog("cura")
|
||||||
|
|
||||||
## Proxy class to the materials page in the preferences.
|
## Proxy class to the materials page in the preferences.
|
||||||
#
|
#
|
||||||
# This class handles the actions in that page, such as creating new materials,
|
# This class handles the actions in that page, such as creating new materials,
|
||||||
|
@ -128,4 +133,34 @@ class MaterialManagementModel(QObject):
|
||||||
favorites_set.add(new_base_id)
|
favorites_set.add(new_base_id)
|
||||||
application.getPreferences().setValue("cura/favorite_materials", ";".join(favorites_set))
|
application.getPreferences().setValue("cura/favorite_materials", ";".join(favorites_set))
|
||||||
|
|
||||||
return new_base_id
|
return new_base_id
|
||||||
|
|
||||||
|
## Create a new material by cloning the preferred material for the current
|
||||||
|
# material diameter and generate a new GUID.
|
||||||
|
#
|
||||||
|
# The material type is explicitly left to be the one from the preferred
|
||||||
|
# material, since this allows the user to still have SOME profiles to work
|
||||||
|
# with.
|
||||||
|
# \return The ID of the newly created material.
|
||||||
|
@pyqtSlot(result = str)
|
||||||
|
def createMaterial(self) -> str:
|
||||||
|
# Ensure all settings are saved.
|
||||||
|
application = cura.CuraApplication.CuraApplication.getInstance()
|
||||||
|
application.saveSettings()
|
||||||
|
|
||||||
|
# Find the preferred material.
|
||||||
|
extruder_stack = application.getMachineManager().activeStack
|
||||||
|
active_variant_name = extruder_stack.variant.getName()
|
||||||
|
approximate_diameter = str(extruder_stack.approximateMaterialDiameter)
|
||||||
|
machine_node = ContainerTree.getInstance().machines[application.getGlobalContainerStack().definition.getId()]
|
||||||
|
preferred_material_node = machine_node.variants[active_variant_name].preferredMaterial(approximate_diameter)
|
||||||
|
|
||||||
|
# Create a new ID & new metadata for the new material.
|
||||||
|
new_id = CuraContainerRegistry.getInstance().uniqueName("custom_material")
|
||||||
|
new_metadata = {"name": catalog.i18nc("@label", "Custom Material"),
|
||||||
|
"brand": catalog.i18nc("@label", "Custom"),
|
||||||
|
"GUID": str(uuid.uuid4()),
|
||||||
|
}
|
||||||
|
|
||||||
|
self.duplicateMaterial(preferred_material_node, new_base_id = new_id, new_metadata = new_metadata)
|
||||||
|
return new_id
|
|
@ -122,7 +122,7 @@ Item
|
||||||
onClicked:
|
onClicked:
|
||||||
{
|
{
|
||||||
forceActiveFocus();
|
forceActiveFocus();
|
||||||
base.newRootMaterialIdToSwitchTo = base.materialManager.createMaterial();
|
base.newRootMaterialIdToSwitchTo = base.materialManagementModel.createMaterial();
|
||||||
base.toActivateNewMaterial = true;
|
base.toActivateNewMaterial = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue