Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAYA-127737 allow loading relative sub layers #2902

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 21 additions & 54 deletions lib/usd/ui/layerEditor/loadLayersDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "qtUtils.h"
#include "stringResources.h"

#include <mayaUsd/utils/utilFileSystem.h>

#include <maya/MQtUtil.h>

#include <QtCore/QDir>
Expand All @@ -41,12 +43,6 @@
namespace {
using namespace UsdLayerEditor;

bool isAbsolutePath(const std::string& in_path)
{
QFileInfo fileInfo(QString::fromStdString(in_path));
return fileInfo.isAbsolute();
}

class MyLineEdit : public QLineEdit
{
public:
Expand All @@ -72,14 +68,12 @@ LayerPathRow::LayerPathRow(LoadLayersDialog* in_parent)
{
auto gridLayout = new QGridLayout();
QtUtils::initLayoutMargins(gridLayout);
_absolutePath = "";
_parentPath = _parent->findDirectoryToUse("");

_label = new QLabel(StringResources::getAsQString(StringResources::kLayerPath));
gridLayout->addWidget(_label, 0, 0);

_pathEdit = new MyLineEdit(this);
connect(_pathEdit, &QLineEdit::textChanged, this, &LayerPathRow::onTextChanged);
gridLayout->addWidget(_pathEdit, 0, 1);

QIcon icon;
Expand All @@ -102,56 +96,17 @@ LayerPathRow::LayerPathRow(LoadLayersDialog* in_parent)
connect(_addPathIcon, &QAbstractButton::clicked, in_parent, &LoadLayersDialog::onAddRow);
gridLayout->addWidget(_addPathIcon, 0, 3);

_convertToRel
= new QCheckBox(StringResources::getAsQString(StringResources::kConvertToRelativePath));
connect(_convertToRel, &QAbstractButton::clicked, this, &LayerPathRow::onRelativeButtonChecked);
_convertToRel->setEnabled(false);
gridLayout->addWidget(_convertToRel, 1, 1);

setLayout(gridLayout);

setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
}

void LayerPathRow::onTextChanged(const QString& text)
{
if (!_convertToRel->isChecked()) {
_absolutePath = text.toStdString();
_convertToRel->setEnabled(isAbsolutePath(_absolutePath));
}
}

void LayerPathRow::onRelativeButtonChecked(bool checked)
{
if (checked) {
QDir dir(_parentPath.c_str());

QString relativePath = dir.relativeFilePath(_absolutePath.c_str());
_pathEdit->setText(relativePath);
_pathEdit->setEnabled(false);
} else {
_pathEdit->setEnabled(true);
_pathEdit->setText(_absolutePath.c_str());
}
}
std::string LayerPathRow::pathToUse() const { return _pathEdit->text().toStdString(); }

std::string LayerPathRow::pathToUse() const
void LayerPathRow::setPathToUse(const std::string& path)
{
if (_convertToRel->isChecked())
return _pathEdit->text().toStdString();
else
return _absolutePath;
}

void LayerPathRow::setAbsolutePath(const std::string& path)
{

_absolutePath = path;
_pathEdit->setText(path.c_str());
_pathEdit->setEnabled(true);

_convertToRel->setChecked(false);
_convertToRel->setEnabled(isAbsolutePath(_absolutePath));
}

void LayerPathRow::setAsRowInserter(bool setIt)
Expand All @@ -160,7 +115,6 @@ void LayerPathRow::setAsRowInserter(bool setIt)
_label->setEnabled(enabled);
_pathEdit->setEnabled(enabled);
_openBrowser->setEnabled(enabled);
_convertToRel->setEnabled(enabled);

_trashIcon->setVisible(!setIt);
_addPathIcon->setVisible(setIt);
Expand Down Expand Up @@ -334,14 +288,27 @@ std::string LoadLayersDialog::findDirectoryToUse(const std::string& rowText) con

void LoadLayersDialog::onOpenBrowser()
{
auto row = dynamic_cast<LayerPathRow*>(sender()->parent());
auto defaultPath = findDirectoryToUse(row->absolutePath());
using namespace PXR_NS::UsdMayaUtilFileSystem;

const bool useSceneFileForRoot = false;
const auto parentLayer = _treeItem->layer();
prepareLayerSaveUILayer(parentLayer, useSceneFileForRoot);

LayerPathRow* row = dynamic_cast<LayerPathRow*>(sender()->parent());
const std::string defaultPath = findDirectoryToUse(row->pathToUse());

auto files = _treeItem->parentModel()->sessionState()->loadLayersUI(windowTitle(), defaultPath);
if (files.size() == 0)
return;

row->setAbsolutePath(files[0]);
// Replace selected filenames with relative ones if enabled.
if (requireUsdPathsRelativeToParentLayer()) {
for (std::string& fileName : files) {
fileName = getPathRelativeToLayerFile(fileName, parentLayer);
}
Comment on lines +306 to +308
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't immediately obvious to me here that you were replacing the paths in the files array with relative paths. Maybe add a comment saying that is what is happening.

}

row->setPathToUse(files[0]);

// insert new rows if more than one file selected
if (files.size() > 1) {
Expand All @@ -361,7 +328,7 @@ void LoadLayersDialog::onOpenBrowser()
for (size_t i = 1; i < files.size(); i++) {
auto newRow = new LayerPathRow(this);
_rowsLayout->insertWidget(index, newRow);
newRow->setAbsolutePath(files[i]);
newRow->setPathToUse(files[i]);
++index;
}

Expand Down
10 changes: 1 addition & 9 deletions lib/usd/ui/layerEditor/loadLayersDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,17 @@ class LayerPathRow : public QWidget

// returns the path to store, either the rel or abs path
std::string pathToUse() const;
// returns the absolute path, always
std::string absolutePath() const { return _absolutePath; }
// sets the absolute path
void setAbsolutePath(const std::string& path);
void setPathToUse(const std::string& path);

protected:
void onTextChanged(const QString& text);
void onRelativeButtonChecked(bool checked);

protected:
std::string _absolutePath;
std::string _parentPath;
LoadLayersDialog* _parent;
QLabel* _label;
QLineEdit* _pathEdit;
QAbstractButton* _openBrowser;
QAbstractButton* _trashIcon;
QAbstractButton* _addPathIcon;
QCheckBox* _convertToRel;
};

}; // namespace UsdLayerEditor
Expand Down
22 changes: 19 additions & 3 deletions plugin/adsk/scripts/mayaUsd_layerEditorFileDialogs.mel
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,30 @@ global proc string UsdLayerEditor_SaveLayerFileDialog(int $isRootLayer)
}
}

global proc string UsdLayerEditor_LoadLayersFileDialogOptions_Create(string $parent)
global proc UsdLayerEditor_LoadLayersFileDialogOptions_UICreate(string $parent)
{
// First create the scroll layout here and then call the python
// helper to add the rest of the UI.
setParent $parent;

string $layout = `scrollLayout -childResizable true`;

frameLayout -collapsable false -labelVisible false -marginHeight 20 -marginWidth 20;

text -label `getMayaUsdString("kTipYouCanChooseMultipleFiles")` -align "left";
return $layout;

string $layout = `scrollLayout -childResizable true`;
python("import mayaUsd_USDRootFileRelative as murel\nmurel.usdSubLayerFileRelative.uiCreate('" + $layout + "')");
}

global proc UsdLayerEditor_LoadLayersFileDialogOptions_UIInit(string $parent, string $filterType)
{
python("import mayaUsd_USDRootFileRelative as murel\nmurel.usdSubLayerFileRelative.uiInit('" + $parent + "', '" + $filterType + "')");
}

global proc UsdLayerEditor_LoadLayersFileDialogOptions_UICommit(string $parent, string $selectedFile)
{
python("import mayaUsd_USDRootFileRelative as murel\nmurel.usdSubLayerFileRelative.uiCommit('" + $parent + "', '" + $selectedFile + "')");
}

global proc string[] UsdLayerEditor_LoadLayersFileDialog(string $title, string $folder)
Expand All @@ -112,7 +126,9 @@ global proc string[] UsdLayerEditor_LoadLayersFileDialog(string $title, string $
-fileMode 4
-okCaption $okCaption
-fileFilter $fileFilter
-optionsUICreate "UsdLayerEditor_LoadLayersFileDialogOptions_Create"
-optionsUICreate "UsdLayerEditor_LoadLayersFileDialogOptions_UICreate"
-optionsUIInit "UsdLayerEditor_LoadLayersFileDialogOptions_UIInit"
-optionsUICommit2 "UsdLayerEditor_LoadLayersFileDialogOptions_UICommit"
-startingDirectory $folder
`;

Expand Down