Removed the GetLabelByAssetId function since now we can use the display name that comes with the ModelMaterialSlot.
Updated OpenMaterialExporter() to account for the fact that multiple material slots can have the same default material asset. Updated the material inspector to sort material slots by name to match the order in the Material Component. Updated ExportItem to protect its data members, which makes it more clear that assetId and materialSlotName are readonly inputs. Signed-off-by: santorac <55155825+santorac@users.noreply.github.com>
This commit is contained in:
+25
-65
@@ -37,47 +37,7 @@ namespace AZ
|
||||
{
|
||||
namespace EditorMaterialComponentExporter
|
||||
{
|
||||
AZStd::string GetLabelByAssetId(const AZ::Data::AssetId& assetId)
|
||||
{
|
||||
AZStd::string label;
|
||||
if (assetId.IsValid())
|
||||
{
|
||||
// Material assets that are exported through the scene pipeline have their filenames generated by adding
|
||||
// the DCC material name as a prefix and a unique number to the end of the source file name.
|
||||
// Rather than storing the DCC material name inside of the material asset we can reproduce it by removing
|
||||
// the prefix and suffix from the product file name.
|
||||
|
||||
// We need the material product path as the initial string that will be stripped down
|
||||
const AZStd::string& productPath = AZ::RPI::AssetUtils::GetProductPathByAssetId(assetId);
|
||||
if (!productPath.empty() && AzFramework::StringFunc::Path::GetFileName(productPath.c_str(), label))
|
||||
{
|
||||
// If there is a source file, typically an FBX or other model file, we must get its filename to remove the prefix from the label
|
||||
AZStd::string prefix;
|
||||
const AZStd::string& sourcePath = AZ::RPI::AssetUtils::GetSourcePathByAssetId(assetId);
|
||||
if (!sourcePath.empty() && AZ::StringFunc::Path::GetFileName(sourcePath.c_str(), prefix))
|
||||
{
|
||||
if (!prefix.empty() && prefix.size() < label.size())
|
||||
{
|
||||
if (AZ::StringFunc::StartsWith(label, prefix, false))
|
||||
{
|
||||
// All of the product filename's tokens are separated by underscores so we must also remove the first underscore after the prefix
|
||||
label = label.substr(prefix.size() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We can remove the numeric suffix by stripping the label of everything after the last underscore
|
||||
const auto iter = label.find_last_of("_");
|
||||
if (iter != AZStd::string::npos)
|
||||
{
|
||||
label = label.substr(0, iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
AZStd::string GetExportPathByAssetId(const AZ::Data::AssetId& assetId)
|
||||
AZStd::string GetExportPathByAssetId(const AZ::Data::AssetId& assetId, const AZStd::string& materialSlotName)
|
||||
{
|
||||
AZStd::string exportPath;
|
||||
if (assetId.IsValid())
|
||||
@@ -85,7 +45,7 @@ namespace AZ
|
||||
exportPath = AZ::RPI::AssetUtils::GetSourcePathByAssetId(assetId);
|
||||
AZ::StringFunc::Path::StripExtension(exportPath);
|
||||
exportPath += "_";
|
||||
exportPath += GetLabelByAssetId(assetId);
|
||||
exportPath += materialSlotName;
|
||||
exportPath += ".";
|
||||
exportPath += AZ::RPI::MaterialSourceData::Extension;
|
||||
AZ::StringFunc::Path::Normalize(exportPath);
|
||||
@@ -132,12 +92,12 @@ namespace AZ
|
||||
int row = 0;
|
||||
for (ExportItem& exportItem : exportItems)
|
||||
{
|
||||
QFileInfo fileInfo(GetExportPathByAssetId(exportItem.m_originalAssetId).c_str());
|
||||
QFileInfo fileInfo(GetExportPathByAssetId(exportItem.GetOriginalAssetId(), exportItem.GetMaterialSlotName()).c_str());
|
||||
|
||||
// Configuring initial settings based on whether or not the target file already exists
|
||||
exportItem.m_exportPath = fileInfo.absoluteFilePath().toUtf8().constData();
|
||||
exportItem.m_exists = fileInfo.exists();
|
||||
exportItem.m_overwrite = false;
|
||||
exportItem.SetExportPath(fileInfo.absoluteFilePath().toUtf8().constData());
|
||||
exportItem.SetExists(fileInfo.exists());
|
||||
exportItem.SetOverwrite(false);
|
||||
|
||||
// Populate the table with data for every column
|
||||
tableWidget->setItem(row, MaterialSlotColumn, new QTableWidgetItem());
|
||||
@@ -146,23 +106,23 @@ namespace AZ
|
||||
|
||||
// Create a check box for toggling the enabled state of this item
|
||||
QCheckBox* materialSlotCheckBox = new QCheckBox(tableWidget);
|
||||
materialSlotCheckBox->setChecked(exportItem.m_enabled);
|
||||
materialSlotCheckBox->setText(GetLabelByAssetId(exportItem.m_originalAssetId).c_str());
|
||||
materialSlotCheckBox->setChecked(exportItem.GetEnabled());
|
||||
materialSlotCheckBox->setText(exportItem.GetMaterialSlotName().c_str());
|
||||
tableWidget->setCellWidget(row, MaterialSlotColumn, materialSlotCheckBox);
|
||||
|
||||
// Create a file picker widget for selecting the save path for the exported material
|
||||
AzQtComponents::BrowseEdit* materialFileWidget = new AzQtComponents::BrowseEdit(tableWidget);
|
||||
materialFileWidget->setLineEditReadOnly(true);
|
||||
materialFileWidget->setClearButtonEnabled(false);
|
||||
materialFileWidget->setEnabled(exportItem.m_enabled);
|
||||
materialFileWidget->setEnabled(exportItem.GetEnabled());
|
||||
materialFileWidget->setText(fileInfo.fileName());
|
||||
tableWidget->setCellWidget(row, MaterialFileColumn, materialFileWidget);
|
||||
|
||||
// Create a check box for toggling the overwrite state of this item
|
||||
QWidget* overwriteCheckBoxContainer = new QWidget(tableWidget);
|
||||
QCheckBox* overwriteCheckBox = new QCheckBox(overwriteCheckBoxContainer);
|
||||
overwriteCheckBox->setChecked(exportItem.m_overwrite);
|
||||
overwriteCheckBox->setEnabled(exportItem.m_enabled && exportItem.m_exists);
|
||||
overwriteCheckBox->setChecked(exportItem.GetOverwrite());
|
||||
overwriteCheckBox->setEnabled(exportItem.GetEnabled() && exportItem.GetExists());
|
||||
|
||||
overwriteCheckBoxContainer->setLayout(new QHBoxLayout(overwriteCheckBoxContainer));
|
||||
overwriteCheckBoxContainer->layout()->addWidget(overwriteCheckBox);
|
||||
@@ -173,21 +133,21 @@ namespace AZ
|
||||
|
||||
// Whenever the selection is updated, automatically apply the change to the export item
|
||||
QObject::connect(materialSlotCheckBox, &QCheckBox::stateChanged, materialSlotCheckBox, [&exportItem, materialFileWidget, materialSlotCheckBox, overwriteCheckBox]([[maybe_unused]] int state) {
|
||||
exportItem.m_enabled = materialSlotCheckBox->isChecked();
|
||||
materialFileWidget->setEnabled(exportItem.m_enabled);
|
||||
overwriteCheckBox->setEnabled(exportItem.m_enabled && exportItem.m_exists);
|
||||
exportItem.SetEnabled(materialSlotCheckBox->isChecked());
|
||||
materialFileWidget->setEnabled(exportItem.GetEnabled());
|
||||
overwriteCheckBox->setEnabled(exportItem.GetEnabled() && exportItem.GetExists());
|
||||
});
|
||||
|
||||
// Whenever the overwrite check box is updated, automatically apply the change to the export item
|
||||
QObject::connect(overwriteCheckBox, &QCheckBox::stateChanged, overwriteCheckBox, [&exportItem, overwriteCheckBox]([[maybe_unused]] int state) {
|
||||
exportItem.m_overwrite = overwriteCheckBox->isChecked();
|
||||
exportItem.SetOverwrite(overwriteCheckBox->isChecked());
|
||||
});
|
||||
|
||||
// Whenever the browse button is clicked, open a save file dialog in the same location as the current export file setting
|
||||
QObject::connect(materialFileWidget, &AzQtComponents::BrowseEdit::attachedButtonTriggered, materialFileWidget, [&dialog, &exportItem, materialFileWidget, overwriteCheckBox]() {
|
||||
QFileInfo fileInfo = QFileDialog::getSaveFileName(&dialog,
|
||||
QString("Select Material Filename"),
|
||||
exportItem.m_exportPath.c_str(),
|
||||
exportItem.GetExportPath().c_str(),
|
||||
QString("Material (*.material)"),
|
||||
nullptr,
|
||||
QFileDialog::DontConfirmOverwrite);
|
||||
@@ -195,14 +155,14 @@ namespace AZ
|
||||
// Only update the export data if a valid path and filename was selected
|
||||
if (!fileInfo.absoluteFilePath().isEmpty())
|
||||
{
|
||||
exportItem.m_exportPath = fileInfo.absoluteFilePath().toUtf8().constData();
|
||||
exportItem.m_exists = fileInfo.exists();
|
||||
exportItem.m_overwrite = fileInfo.exists();
|
||||
exportItem.SetExportPath(fileInfo.absoluteFilePath().toUtf8().constData());
|
||||
exportItem.SetExists(fileInfo.exists());
|
||||
exportItem.SetOverwrite(fileInfo.exists());
|
||||
|
||||
// Update the controls to display the new state
|
||||
materialFileWidget->setText(fileInfo.fileName());
|
||||
overwriteCheckBox->setChecked(exportItem.m_overwrite);
|
||||
overwriteCheckBox->setEnabled(exportItem.m_enabled && exportItem.m_exists);
|
||||
overwriteCheckBox->setChecked(exportItem.GetOverwrite());
|
||||
overwriteCheckBox->setEnabled(exportItem.GetEnabled() && exportItem.GetExists());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -245,24 +205,24 @@ namespace AZ
|
||||
|
||||
bool ExportMaterialSourceData(const ExportItem& exportItem)
|
||||
{
|
||||
if (!exportItem.m_enabled || exportItem.m_exportPath.empty())
|
||||
if (!exportItem.GetEnabled() || exportItem.GetExportPath().empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (exportItem.m_exists && !exportItem.m_overwrite)
|
||||
if (exportItem.GetExists() && !exportItem.GetOverwrite())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
EditorMaterialComponentUtil::MaterialEditData editData;
|
||||
if (!EditorMaterialComponentUtil::LoadMaterialEditDataFromAssetId(exportItem.m_originalAssetId, editData))
|
||||
if (!EditorMaterialComponentUtil::LoadMaterialEditDataFromAssetId(exportItem.GetOriginalAssetId(), editData))
|
||||
{
|
||||
AZ_Warning("AZ::Render::EditorMaterialComponentExporter", false, "Failed to load material data.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EditorMaterialComponentUtil::SaveSourceMaterialFromEditData(exportItem.m_exportPath, editData))
|
||||
if (!EditorMaterialComponentUtil::SaveSourceMaterialFromEditData(exportItem.GetExportPath(), editData))
|
||||
{
|
||||
AZ_Warning("AZ::Render::EditorMaterialComponentExporter", false, "Failed to save material data.");
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user