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

EMSUSD-1771 remove schema from prims #4008

Merged
merged 1 commit into from
Nov 26, 2024
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
1 change: 1 addition & 0 deletions lib/mayaUsd/commands/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ It takes as its main arguments a list of UFE paths.
| `-appliedSchemas` | `-app` | noarg | Query which schemas the prims have in common |
| `-schema` | `-sch` | string | The schema type name to apply to the prims |
| `-instanceName` | `-in` | string | The instance name for multi-apply schema |
| `-removeSchema` | `-rem` | noarg | Remove the schema instead of applying it |
| `-singleApplicationSchemas` | `-sas` | noarg | Query the list of known single-apply schemas |
| `-multiApplicationSchemas` | `-mas` | noarg | Query the list of known multi-apply schemas |

Expand Down
47 changes: 35 additions & 12 deletions lib/mayaUsd/commands/schemaCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ static MString formatMessage(const char* format, const std::string& text)
return PXR_NS::TfStringPrintf(format, text.c_str()).c_str();
}

static MString formatMessage(const char* format, PXR_NS::UsdPrim& prim, const std::string& text)
static MString formatMessage(
const char* format,
const char* action,
PXR_NS::UsdPrim& prim,
const std::string& text)
{
return PXR_NS::TfStringPrintf(format, prim.GetPath().GetString().c_str(), text.c_str()).c_str();
return PXR_NS::TfStringPrintf(format, action, prim.GetPath().GetString().c_str(), text.c_str())
.c_str();
}

static std::string formatMessage(const char* format, const Ufe::Path& ufePath)
Expand All @@ -67,6 +72,8 @@ static const char kSchemaFlag[] = "sch";
static const char kSchemaLongFlag[] = "schema";
static const char kInstanceNameFlag[] = "in";
static const char kInstanceNameLongFlag[] = "instanceName";
static const char kRemoveSchemaFlag[] = "rem";
static const char kRemoveSchemaLongFlag[] = "removeSchema";

static const char kSingleApplicationFlag[] = "sas";
static const char kSingleApplicationLongFlag[] = "singleApplicationSchemas";
Expand Down Expand Up @@ -94,6 +101,9 @@ class SchemaCommand::Data
const std::string& getSchema() const { return _schema; }
const std::string& getInstanceName() const { return _instanceName; }

// Check if the command is removing a schema.
bool isRemovingSchema() const { return _isRemovingSchema; }

// Check if the command is a query or which specific type of query.
bool isQuerying() const { return isQueryingAppliedSchemas() || isQueryingKnownSchemas(); }
bool isQueryingKnownSchemas() const
Expand All @@ -114,6 +124,7 @@ class SchemaCommand::Data
std::string parseStringArg(MArgDatabase& argData, const char* argFlag);

std::vector<Ufe::Path> _primPaths;
bool _isRemovingSchema { false };
bool _isQueryingAppliedSchemas { false };
bool _singleApplicationSchemas { false };
bool _multiApplicationSchemas { false };
Expand All @@ -133,6 +144,7 @@ MStatus SchemaCommand::Data::parseArgs(const MArgList& argList)
_isQueryingAppliedSchemas = argData.isFlagSet(kAppliedSchemasFlag);
_schema = parseStringArg(argData, kSchemaFlag);
_instanceName = parseStringArg(argData, kInstanceNameFlag);
_isRemovingSchema = argData.isFlagSet(kRemoveSchemaFlag);
_singleApplicationSchemas = argData.isFlagSet(kSingleApplicationFlag);
_multiApplicationSchemas = argData.isFlagSet(kMultiApplicationFlag);

Expand Down Expand Up @@ -202,6 +214,8 @@ MSyntax SchemaCommand::createSyntax()
syntax.addFlag(kSchemaFlag, kSchemaLongFlag, MSyntax::kString);
syntax.addFlag(kInstanceNameFlag, kInstanceNameLongFlag, MSyntax::kString);

syntax.addFlag(kRemoveSchemaFlag, kRemoveSchemaLongFlag);

syntax.addFlag(kSingleApplicationFlag, kSingleApplicationLongFlag);
syntax.addFlag(kMultiApplicationFlag, kMultiApplicationLongFlag);

Expand Down Expand Up @@ -240,13 +254,13 @@ MStatus SchemaCommand::handleKnownSchemas()
return MS::kSuccess;
}

MStatus SchemaCommand::handleApplySchema()
MStatus SchemaCommand::handleApplyOrRemoveSchema()
{
UsdUfe::UsdUndoBlock undoBlock(&_data->getUsdUndoItem());

const std::string& schemaName = _data->getSchema();
if (schemaName.empty()) {
displayError("No schema given to apply to the prims");
displayError("No schema given to modify the prims");
return MS::kInvalidParameter;
}

Expand All @@ -265,18 +279,27 @@ MStatus SchemaCommand::handleApplySchema()
return MS::kInvalidParameter;
}

PXR_NS::TfToken instanceName(_data->getInstanceName());

auto func = _data->isRemovingSchema() ? &UsdUfe::removeMultiSchemaFromPrim
: &UsdUfe::applyMultiSchemaToPrim;
const char* action = _data->isRemovingSchema() ? "remove" : "apply";

for (PXR_NS::UsdPrim& prim : _data->getPrims()) {
if (!UsdUfe::applyMultiSchemaToPrim(
prim, schemaType, PXR_NS::TfToken(_data->getInstanceName()))) {
displayWarning(
formatMessage("Could no apply schema \"%s\" to prim \"%s\"", prim, schemaName));
if (!func(prim, schemaType, instanceName)) {
displayWarning(formatMessage(
"Could not %s schema \"%s\" to prim \"%s\"", action, prim, schemaName));
}
}
} else {
auto func = _data->isRemovingSchema() ? &UsdUfe::removeSchemaFromPrim
: &UsdUfe::applySchemaToPrim;
const char* action = _data->isRemovingSchema() ? "remove" : "apply";

for (PXR_NS::UsdPrim& prim : _data->getPrims()) {
if (!UsdUfe::applySchemaToPrim(prim, schemaType)) {
displayWarning(
formatMessage("Could no apply schema \"%s\" to prim \"%s\"", prim, schemaName));
if (!func(prim, schemaType)) {
displayWarning(formatMessage(
"Could not %s schema \"%s\" to prim \"%s\"", action, prim, schemaName));
}
}
}
Expand All @@ -302,7 +325,7 @@ MStatus SchemaCommand::doIt(const MArgList& argList)
if (_data->isQueryingKnownSchemas())
return handleKnownSchemas();

return handleApplySchema();
return handleApplyOrRemoveSchema();
} catch (const std::exception& exc) {
displayError(exc.what());
return MS::kFailure;
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/commands/schemaCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SchemaCommand : public MPxCommand

MStatus handleAppliedSchemas();
MStatus handleKnownSchemas();
MStatus handleApplySchema();
MStatus handleApplyOrRemoveSchema();

class Data;
std::unique_ptr<Data> _data;
Expand Down
27 changes: 27 additions & 0 deletions lib/usdUfe/python/wrapUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,29 @@ bool _applyMultiSchemaToPrim(
return UsdUfe::applyMultiSchemaToPrim(prim, schemaType, instanceName);
}

bool _removeSchemaFromPrim(PXR_NS::UsdPrim& prim, const PXR_NS::TfType& schemaType)
{
return UsdUfe::removeSchemaFromPrim(prim, schemaType);
}

bool _removeMultiSchemaFromPrim(
PXR_NS::UsdPrim& prim,
const PXR_NS::TfType& schemaType,
const PXR_NS::TfToken& instanceName)
{
return UsdUfe::removeMultiSchemaFromPrim(prim, schemaType, instanceName);
}

std::vector<PXR_NS::TfToken> _getPrimAppliedSchemas(const PXR_NS::UsdPrim& prim)
{
return UsdUfe::getPrimAppliedSchemas(prim);
}

std::set<PXR_NS::TfToken> _getPrimsAppliedSchemas(const std::vector<PXR_NS::UsdPrim>& prims)
{
return UsdUfe::getPrimsAppliedSchemas(prims);
}

void wrapUtils()
{
// Because UsdUfe and UFE have incompatible Python bindings that do not
Expand All @@ -173,5 +196,9 @@ void wrapUtils()
def("getKnownApplicableSchemas", _getKnownApplicableSchemas);
def("applySchemaToPrim", _applySchemaToPrim);
def("applyMultiSchemaToPrim", _applyMultiSchemaToPrim);
def("removeSchemaFromPrim", _removeSchemaFromPrim);
def("removeMultiSchemaFromPrim", _removeMultiSchemaFromPrim);
def("getPrimAppliedSchemas", _getPrimAppliedSchemas);
def("getPrimsAppliedSchemas", _getPrimsAppliedSchemas);
def("findSchemasByTypeName", _findSchemasByTypeName);
}
48 changes: 43 additions & 5 deletions lib/usdUfe/utils/schemas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

namespace USDUFE_NS_DEF {

namespace {

} // namespace
////////////////////////////////////////////////////////////////////////////
//
// Known schemas

KnownSchemas getKnownApplicableSchemas()
{
Expand Down Expand Up @@ -69,12 +69,16 @@ std::shared_ptr<SchemaInfo> findSchemasByTypeName(const PXR_NS::TfToken& schemaT
return findSchemasByTypeName(schemaTypeName, UsdUfe::getKnownApplicableSchemas());
}

////////////////////////////////////////////////////////////////////////////
//
// Schema application

bool applySchemaToPrim(PXR_NS::UsdPrim& prim, const PXR_NS::TfType& schemaType)
{
return prim.ApplyAPI(schemaType);
}

bool applySchemaToPrim(PXR_NS::UsdPrim& prim, const SchemaInfo& info)
bool applySchemaInfoToPrim(PXR_NS::UsdPrim& prim, const SchemaInfo& info)
{
return applySchemaToPrim(prim, info.schemaType);
}
Expand All @@ -87,14 +91,48 @@ bool applyMultiSchemaToPrim(
return prim.ApplyAPI(schemaType, instanceName);
}

bool applyMultiSchemaToPrim(
bool applyMultiSchemaInfoToPrim(
PXR_NS::UsdPrim& prim,
const SchemaInfo& info,
const PXR_NS::TfToken& instanceName)
{
return applyMultiSchemaToPrim(prim, info.schemaType, instanceName);
}

////////////////////////////////////////////////////////////////////////////
//
// Schema removal

bool removeSchemaFromPrim(PXR_NS::UsdPrim& prim, const PXR_NS::TfType& schemaType)
{
return prim.RemoveAPI(schemaType);
}

bool removeSchemaInfoFromPrim(PXR_NS::UsdPrim& prim, const SchemaInfo& info)
{
return removeSchemaFromPrim(prim, info.schemaType);
}

bool removeMultiSchemaFromPrim(
PXR_NS::UsdPrim& prim,
const PXR_NS::TfType& schemaType,
const PXR_NS::TfToken& instanceName)
{
return prim.RemoveAPI(schemaType, instanceName);
}

bool removeMultiSchemaInfoFromPrim(
PXR_NS::UsdPrim& prim,
const SchemaInfo& info,
const PXR_NS::TfToken& instanceName)
{
return removeMultiSchemaFromPrim(prim, info.schemaType, instanceName);
}

////////////////////////////////////////////////////////////////////////////
//
// Schema query

std::vector<PXR_NS::TfToken> getPrimAppliedSchemas(const PXR_NS::UsdPrim& prim)
{
const PXR_NS::UsdPrimTypeInfo& info = prim.GetPrimTypeInfo();
Expand Down
40 changes: 38 additions & 2 deletions lib/usdUfe/utils/schemas.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool applySchemaToPrim(PXR_NS::UsdPrim& prim, const PXR_NS::TfType& schemaType);
/// @param info the schema info to apply.
/// @return true if the application succeeded.
USDUFE_PUBLIC
bool applySchemaToPrim(PXR_NS::UsdPrim& prim, const SchemaInfo& info);
bool applySchemaInfoToPrim(PXR_NS::UsdPrim& prim, const SchemaInfo& info);

/// @brief apply the given multi-apply schema type to the given prim.
/// @param prim the prim to receive the schema.
Expand All @@ -92,7 +92,43 @@ bool applyMultiSchemaToPrim(
/// @param instanceName the unique name of the new schema application.
/// @return true if the application succeeded.
USDUFE_PUBLIC
bool applyMultiSchemaToPrim(
bool applyMultiSchemaInfoToPrim(
PXR_NS::UsdPrim& prim,
const SchemaInfo& info,
const PXR_NS::TfToken& instanceName);

/// @brief remove the given single-apply schema type from the given prim.
/// @param prim the prim to lose the schema.
/// @param schemaType the schema type to remove.
/// @return true if the removal succeeded.
USDUFE_PUBLIC
bool removeSchemaFromPrim(PXR_NS::UsdPrim& prim, const PXR_NS::TfType& schemaType);

/// @brief remove the given single-apply schema type from the given prim.
/// @param prim the prim to lose the schema.
/// @param info the schema info to remove.
/// @return true if the removal succeeded.
USDUFE_PUBLIC
bool removeSchemaInfoFromPrim(PXR_NS::UsdPrim& prim, const SchemaInfo& info);

/// @brief remove the given single-apply schema type from the given prim.
/// @param prim the prim to lose the schema.
/// @param schemaType the schema type to remove.
/// @param instanceName the unique name of the new schema application.
/// @return true if the removal succeeded.
USDUFE_PUBLIC
bool removeMultiSchemaFromPrim(
PXR_NS::UsdPrim& prim,
const PXR_NS::TfType& schemaType,
const PXR_NS::TfToken& instanceName);

/// @brief remove the given single-apply schema type from the given prim.
/// @param prim the prim to lose the schema.
/// @param info the schema info to remove.
/// @param instanceName the unique name of the new schema application.
/// @return true if the removal succeeded.
USDUFE_PUBLIC
bool removeMultiSchemaInfoFromPrim(
PXR_NS::UsdPrim& prim,
const SchemaInfo& info,
const PXR_NS::TfToken& instanceName);
Expand Down
4 changes: 3 additions & 1 deletion plugin/adsk/scripts/mayaUSDRegisterStrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@
"kShareStageAnn": "Toggle sharable on and off to sandbox your changes and create a new stage",
"kShowArrayAttributes": "Show Array Attributes",
"kAddSchemaMenuItem": "Add Schema",
"kRemoveSchemaMenuItem": "Remove Schema",
"kAddSchemaInstanceTitle": "Add Schema Instance",
"kAddSchemaInstanceMesage": "%s Schema Instance Name",
"kAddSchemaInstanceMessage": "%s Schema Instance Name",
"kCannotApplySchemaWarning": 'Cannot apply schema "%s": no USD prim are currently selected.',
"kCannotRemoveSchemaWarning": 'Cannot remove schema "%s": no USD prim are currently selected.',
"kUSDPointInstancesPickMode_PointInstancer": "Point Instancer",
"kUSDPointInstancesPickMode_PointInstancerAnn": "Selection mode for all prims set as point instances.",
"kUSDPointInstancesPickMode_Instances": "Instances",
Expand Down
Loading