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

[al] Add support for default rgba values and color threshold for exporting #2250

Merged
Show file tree
Hide file tree
Changes from 5 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
71 changes: 64 additions & 7 deletions plugin/al/mayautils/AL/maya/utils/PluginTranslatorOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,26 @@ bool PluginTranslatorOptions::addInt(const char* optionName, int defaultValue)
//----------------------------------------------------------------------------------------------------------------------
AL_MAYA_UTILS_PUBLIC
bool PluginTranslatorOptions::addFloat(const char* optionName, float defaultValue)
{
return addFloat(optionName, defaultValue, 1, "", true);
}

//----------------------------------------------------------------------------------------------------------------------
bool PluginTranslatorOptions::addFloat(const char* optionName, float value, int precision)
{
return addFloat(optionName, value, precision, "", true);
}

bool PluginTranslatorOptions::addFloat(
const char* optionName,
float value,
int precision,
const char* controller,
bool enableState)
{
if (isOption(optionName))
return false;
m_options.emplace_back(optionName, defaultValue);
m_options.emplace_back(optionName, value, precision, controller, enableState);
return true;
}

Expand Down Expand Up @@ -548,17 +564,50 @@ void PluginTranslatorOptions::generateFloatGlobals(
const MString& niceName,
const MString& optionName,
MString& code,
float value)
float value,
int precision,
const MString& controller,
bool enableState)
{
MString valueStr;
valueStr = value;
MString controlName = MString(prefix) + "_" + makeName(optionName);
// Check and add on/off command if any
MString onOffCmd;
MString postUpdateCmd;
MString deferredPostUpdateCmd;
if (controller.length()) {
MString prefixedCtrl(MString(prefix) + "_" + makeName(controller));
if (enableState) {
onOffCmd = "checkBox -e "
"-onCommand \"floatFieldGrp -e -en 1 "
+ controlName
+ "\" "
"-offCommand \"floatFieldGrp -e -en 0 "
+ controlName + "\" " + prefixedCtrl + "; ";
// And update the default state according to the checkbox
postUpdateCmd = "floatFieldGrp -e -en `checkBox -q -v " + prefixedCtrl + "` "
+ controlName + "; ";
} else {
onOffCmd = "checkBox -e "
"-onCommand \"floatFieldGrp -e -en 0 "
+ controlName
+ "\" "
"-offCommand \"floatFieldGrp -e -en 1 "
+ controlName + "\" " + prefixedCtrl + "; ";
postUpdateCmd = "floatFieldGrp -e -en (`checkBox -q -v " + prefixedCtrl + "` ? 0: 1) "
+ controlName + "; ";
}
onOffCmd += postUpdateCmd;
deferredPostUpdateCmd = MString() + "eval(\"" + postUpdateCmd + "\");";
}

MString createCommand = MString("global proc create_") + controlName + "() {"
+ MString("floatFieldGrp -l \"") + niceName + "\" -v1 " + valueStr + " " + controlName
+ ";}\n";
+ MString("floatFieldGrp -l \"") + niceName + "\" -v1 " + valueStr + " -pre " + precision
+ " " + controlName + ";" + onOffCmd + "}\n";
MString postCommand = MString("global proc post_") + controlName
+ "(string $value){ eval (\"floatFieldGrp -e -v1 \" + $value + \" " + controlName
+ "\");}\n";
+ "(string $value){ eval (\"floatFieldGrp -e -v1 \" + $value + \" " + controlName + "\");"
+ deferredPostUpdateCmd + "}\n";
MString buildCommand = MString("global proc string build_") + controlName
+ "(){ string $str = \"" + makeName(optionName) + "=\" + `floatFieldGrp -q -v1 "
+ controlName + "` + \";\"; return $str;}\n";
Expand Down Expand Up @@ -644,7 +693,15 @@ MString PluginTranslatorOptions::generateGUI(const char* const prefix, MString&
generateIntGlobals(prefix, opt->name, opt->name, guiCode, opt->defInt);
break;
case OptionType::kFloat:
generateFloatGlobals(prefix, opt->name, opt->name, guiCode, opt->defFloat);
generateFloatGlobals(
prefix,
opt->name,
opt->name,
guiCode,
opt->defFloat,
opt->precision,
opt->controller,
opt->enableState);
break;
case OptionType::kString:
generateStringGlobals(prefix, opt->name, opt->name, guiCode, opt->defString.asChar());
Expand Down
44 changes: 42 additions & 2 deletions plugin/al/mayautils/AL/maya/utils/PluginTranslatorOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@ class PluginTranslatorOptions
AL_MAYA_UTILS_PUBLIC
bool addFloat(const char* optionName, float defaultValue = 0.0f);

/// \brief Add a float value with precision to the translator options
/// \param optionName the name of the option
/// \param value the default value for the option
/// \param precision the default precision for the option
/// \return true if the option was successfully added. False if the option is a duplicate
AL_MAYA_UTILS_PUBLIC
bool addFloat(const char* optionName, float value, int precision);

/// \brief Add a float value with precision and controller state to the translator options
/// \param optionName the name of the option
/// \param value the default value for the option
/// \param precision the default precision for the option
/// \param controller the controller UI name that triggers enabling
/// \param enableState the state for enabling this field
/// \return true if the option was successfully added. False if the option is a duplicate
AL_MAYA_UTILS_PUBLIC
bool addFloat(
const char* optionName,
float value,
int precision,
const char* controller,
bool enableState);

/// \brief Add a string value to the translator options
/// \param optionName the name of the option
/// \param defaultValue the default value for the option
Expand Down Expand Up @@ -387,6 +410,9 @@ class PluginTranslatorOptions
MString defString; ///< default string value
std::vector<MString> enumStrings; ///< the text values for the enums
OptionType type; ///< the type of the option
int precision { 1 };
MString controller {};
bool enableState { true };

/// \brief ctor
/// \param name the name of the option
Expand All @@ -411,9 +437,20 @@ class PluginTranslatorOptions
/// \brief ctor
/// \param name the name of the option
/// \param defVal the default value
Option(const char* const name, const float& defVal)
/// \param precision the default precision for the option
/// \param controller the controller UI name that triggers enabling
/// \param enableState the state for enabling this field
Option(
const char* const name,
const float& defVal,
int precision,
const char* controller,
bool enableState)
: name(name)
, type(OptionType::kFloat)
, precision(precision)
, controller(controller ? controller : "")
, enableState(enableState)
{
defFloat = defVal;
}
Expand Down Expand Up @@ -480,7 +517,10 @@ class PluginTranslatorOptions
const MString& niceName,
const MString& optionName,
MString& code,
float);
float value,
int precision,
const MString& controller,
bool enableState);
static void generateStringGlobals(
const char* const prefix,
const MString& niceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,70 @@ TEST(DiffPrimVar, diffGeomExtent)
}
}

TEST(DiffPrimVar, diffGeomRGBA)
{
MFileIO::newFile(true);
MStringArray result;
ASSERT_TRUE(
MGlobal::executeCommand(
"polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;;", result)
== MS::kSuccess);
ASSERT_TRUE(
MGlobal::executeCommand("setAttr \"pCubeShape1.displayColors\" 1;") == MS::kSuccess);

MSelectionList mSel;
EXPECT_TRUE(mSel.add("pCube1") == MS::kSuccess);

MDagPath mDagPath;
mSel.getDagPath(0, mDagPath);
MFnMesh mFnMesh(mDagPath);
mFnMesh.createColorSetWithName("displayColor");

// Set one polyCube face color to blue and the alpha to 1
MColor blueColour(0.0f, 0.0f, 1.0f, 1.0f);
mFnMesh.setFaceColor(blueColour, 4);

// Run export comand
auto tempPath = buildTempPath("AL_USDMayaTests_diffRGBA.usda");
std::string exportCommand = R"(
file -force -options "Dynamic_Attributes=1;Duplicate_Instances=1;Merge_Transforms=1;Animation=0;Use_Timeline_Range=0;Frame_Min=0;Frame_Max=1;Sub_Samples=1;Filter_Sample=0;Export_At_Which_Time=0;Export_In_World_Space=0;Activate_all_Plugin_Translators=1;Active_Translator_List=;Inactive_Translator_List=;Nurbs_Curves=1;Meshes=1;Mesh_Face_Connects=1;Mesh_Points=1;Mesh_Extents=1;Mesh_Normals=1;Mesh_Vertex_Creases=1;Mesh_Edge_Creases=1;Mesh_UVs=1;Mesh_UV_Only=0;Mesh_Points_as_PRef=0;Mesh_Colours=1;Default_RGB=0.2;Default_Alpha=1;Mesh_Holes=1;Write_Normals_as_Primvars=0;Reverse_Opposite_Normals=0;Subdivision_scheme=0;Compaction_Level=3;" -type "AL usdmaya export" -pr -ea
)";
exportCommand += "\"";
exportCommand += tempPath;
exportCommand += "\"";
ASSERT_TRUE(MGlobal::executeCommand(exportCommand.c_str()) == MS::kSuccess);

// Validate export
UsdStageRefPtr stage = UsdStage::Open(tempPath);

MString path = MString("/pCube1");
SdfPath primPath(path.asChar());
UsdPrim geomPrim = stage->GetPrimAtPath(primPath);
ASSERT_TRUE(geomPrim.IsValid());
UsdGeomMesh geom(geomPrim);

// Confirm displayOpacity is 1.0
const TfToken displayOpacityToken("primvars:displayOpacity");
UsdAttribute opacityAttribute = geomPrim.GetAttribute(displayOpacityToken);

VtFloatArray opacity;
opacityAttribute.Get(&opacity);
VtFloatArray expectedOpacity { 1.f, 1.f, 1.f, 1.f, 1.f, 1.f };
EXPECT_EQ(opacity, expectedOpacity);

// Confirm displayColor has been applied
const TfToken displayColorToken("primvars:displayColor");
UsdAttribute colorAttribute = geomPrim.GetAttribute(displayColorToken);

VtVec3fArray color;
colorAttribute.Get(&color);
VtVec3fArray expectedColor {
GfVec3f(0.2f), GfVec3f(0.2f), GfVec3f(0.2f),
GfVec3f(0.2f), GfVec3f(0.f, 0.f, 1.f), GfVec3f(0.2f),
};
EXPECT_EQ(color, expectedColor);
}

TEST(DiffPrimVar, diffGeomNormals)
{
MFileIO::newFile(true);
Expand Down
Loading