C++ API Reference
fileDialog/fileDialog.cpp
// ===========================================================================
// Copyright 2025 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include "fileDialog.h"
#include <QtWidgets/QDialog>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QCheckBox>
#include <map>
#include <array>
#include <utility>
#include <filesystem>
MString ExampleFileDialog::fLoadPath;
void* ExampleFileDialog::widget()
{
// Return a QWidget representing the file dialog.
// Create a QWidget to hold the file dialog UI.
QWidget* displayWidget = new QWidget();
QVBoxLayout* mainLayout = new QVBoxLayout(displayWidget);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
displayWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
fSelectedFiles.clear();
// File selection
QVBoxLayout* fileSelectionLayout = new QVBoxLayout();
fileSelectionLayout->setContentsMargins(0, 0, 0, 0);
fileSelectionLayout->setSpacing(5);
const char* file1 = "cone.ma";
const char* file2 = "cylinder.abc";
const char* file3 = "script.py";
std::filesystem::path pathFile1 = std::filesystem::path(ExampleFileDialog::fLoadPath.asChar()) / file1;
std::filesystem::path pathFile2 = std::filesystem::path(ExampleFileDialog::fLoadPath.asChar()) / file2;
std::filesystem::path pathFile3 = std::filesystem::path(ExampleFileDialog::fLoadPath.asChar()) / file3;
QCheckBox* file1Checkbox = new QCheckBox(file1);
QCheckBox* file2Checkbox = new QCheckBox(file2);
QCheckBox* file3Checkbox = new QCheckBox(file3);
fileSelectionLayout->addWidget(file1Checkbox);
fileSelectionLayout->addWidget(file2Checkbox);
fileSelectionLayout->addWidget(file3Checkbox);
mainLayout->addLayout(fileSelectionLayout);
auto buttonAction = [this](const auto* filepath, QCheckBox* checkbox) {
if (checkbox->isChecked()) {
fSelectedFiles.append(filepath);
} else {
fSelectedFiles.remove(fSelectedFiles.indexOf(filepath));
}
};
QObject::connect(file1Checkbox, &QCheckBox::checkStateChanged, [file1Checkbox, pathFile1, buttonAction]() {
buttonAction(pathFile1.c_str(), file1Checkbox);
});
QObject::connect(file2Checkbox, &QCheckBox::checkStateChanged, [file2Checkbox, pathFile2, buttonAction]() {
buttonAction(pathFile2.c_str(), file2Checkbox);
});
QObject::connect(file3Checkbox, &QCheckBox::checkStateChanged, [file3Checkbox, pathFile3, buttonAction]() {
buttonAction(pathFile3.c_str(), file3Checkbox);
});
QHBoxLayout* buttonLayout = new QHBoxLayout();
buttonLayout->setContentsMargins(0, 0, 0, 0);
buttonLayout->setSpacing(5);
mainLayout->addLayout(buttonLayout);
// Add a cancel button
QPushButton* cancelButton = new QPushButton("Cancel");
buttonLayout->addWidget(cancelButton);
// Connect the cancel button to a slot that handles the cancel action
QObject::connect(cancelButton, &QPushButton::clicked, [this]() {
// Handle the cancel action here
this->done(QDialog::Rejected);
this->reject();
});
// Add Open button
QPushButton* openButton = new QPushButton("Open");
buttonLayout->addWidget(openButton);
// Connect the button to a slot that handles the open action
QObject::connect(openButton, &QPushButton::clicked, [this]() {
// Handle the open action here
this->done(QDialog::Accepted);
this->accept();
});
return displayWidget;
}
MStringArray ExampleFileDialog::selectedFiles() const
{
// Return the list of selected files.
return fSelectedFiles;
}
// ==========================================================================
//
// Plugin load/unload
//
// ==========================================================================
MStatus initializePlugin(MObject plugin)
{
MStatus st;
MFnPlugin pluginFn(plugin, "Autodesk, Inc.", "1.0", "Any", &st);
if (!st) {
MString("fileDialog - could not initialize plugin: ")
+ st.errorString()
);
return st;
}
// Register the file dialog.
st = pluginFn.registerFileDialog(
"fileDialog",
ExampleFileDialog::creator
);
if (!st) {
MString("fileDialog - could not register '")
+ st.errorString()
);
return st;
}
ExampleFileDialog::fLoadPath = pluginFn.loadPath();
return st;
}
MStatus uninitializePlugin(MObject plugin)
{
MStatus st;
MFnPlugin pluginFn(plugin, "Autodesk, Inc.", "1.0", "Any", &st);
if (!st) {
MString("fileDialog - could not uninitialize plugin: ")
+ st.errorString()
);
return st;
}
// Deregister the file dialog.
st = pluginFn.deregisterFileDialog(
"fileDialog"
);
if (!st) {
MString("qtForms - could not deregister '")
+ st.errorString()
);
return st;
}
return st;
}