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-105735 Clean up empty map1 texcoord #685

Merged
merged 3 commits into from
Jul 28, 2020
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
59 changes: 45 additions & 14 deletions lib/mayaUsd/fileio/utils/meshReadUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

PXR_NAMESPACE_OPEN_SCOPE

TF_DEFINE_PUBLIC_TOKENS(UsdMayaMeshColorSetTokens,
PXRUSDMAYA_MESH_COLOR_SET_TOKENS);
TF_DEFINE_PUBLIC_TOKENS(UsdMayaMeshPrimvarTokens,
PXRUSDMAYA_MESH_PRIMVAR_TOKENS);

// These tokens are supported Maya attributes used for Mesh surfaces
TF_DEFINE_PRIVATE_TOKENS(
Expand Down Expand Up @@ -181,7 +181,7 @@ namespace
}

bool
assignUVSetPrimvarToMesh(const UsdGeomPrimvar& primvar, MFnMesh& meshFn)
assignUVSetPrimvarToMesh(const UsdGeomPrimvar& primvar, MFnMesh& meshFn, bool hasDefaultUVSet)
{
const TfToken& primvarName = primvar.GetPrimvarName();

Expand Down Expand Up @@ -243,11 +243,25 @@ namespace

MStatus status{MS::kSuccess};
MString uvSetName(primvarName.GetText());
bool createUVSet = true;

if (primvarName == UsdUtilsGetPrimaryUVSetName()) {
// We assume that the primary USD UV set maps to Maya's default 'map1'
// set which always exists, so we shouldn't try to create it.
uvSetName = "map1";
} else {
uvSetName = UsdMayaMeshPrimvarTokens->DefaultMayaTexcoordName.GetText();
createUVSet = false;
} else if (!hasDefaultUVSet) {
// If map1 still exists, we rename and re-use it:
MStringArray uvSetNames;
meshFn.getUVSetNames(uvSetNames);
if (uvSetNames[0] == UsdMayaMeshPrimvarTokens->DefaultMayaTexcoordName.GetText()) {
meshFn.renameUVSet(
UsdMayaMeshPrimvarTokens->DefaultMayaTexcoordName.GetText(), uvSetName);
createUVSet = false;
}
}

if (createUVSet) {
status = meshFn.createUVSet(uvSetName);
if (status != MS::kSuccess) {
TF_WARN("Unable to create UV set '%s' for mesh: %s",
Expand Down Expand Up @@ -321,17 +335,17 @@ namespace
// Note that if BOTH displayColor and displayOpacity are authored, they will
// be imported as separate color sets. We do not attempt to combine them
// into a single color set.
if (primvarName == UsdMayaMeshColorSetTokens->DisplayOpacityColorSetName &&
if (primvarName == UsdMayaMeshPrimvarTokens->DisplayOpacityColorSetName &&
typeName == SdfValueTypeNames->FloatArray) {
if (!UsdMayaRoundTripUtil::IsAttributeUserAuthored(mesh.GetDisplayColorPrimvar())) {
colorSetName = UsdMayaMeshColorSetTokens->DisplayColorColorSetName.GetText();
colorSetName = UsdMayaMeshPrimvarTokens->DisplayColorColorSetName.GetText();
}
}

// We'll need to convert colors from linear to display if this color set is
// for display colors.
const bool isDisplayColor =
(colorSetName == UsdMayaMeshColorSetTokens->DisplayColorColorSetName.GetText());
(colorSetName == UsdMayaMeshPrimvarTokens->DisplayColorColorSetName.GetText());

// Get the raw data before applying any indexing. We'll only populate one
// of these arrays based on the primvar's typeName, and we'll also set the
Expand Down Expand Up @@ -505,7 +519,7 @@ namespace
const MString colorSetName = colorSetNames[i];

if (std::string(colorSetName.asChar())
== UsdMayaMeshColorSetTokens->DisplayColorColorSetName.GetString())
== UsdMayaMeshPrimvarTokens->DisplayColorColorSetName.GetString())
{
const auto csRep = meshFn.getColorRepresentation(colorSetName);

Expand Down Expand Up @@ -608,9 +622,26 @@ UsdMayaMeshReadUtils::assignPrimvarsToMesh(const UsdGeomMesh& mesh,

// GETTING PRIMVARS
const std::vector<UsdGeomPrimvar> primvars = mesh.GetPrimvars();
TF_FOR_ALL(iter, primvars)

// Maya always has a map1 UV set. We need to find out if there is any stream in the file that
// will use that slot. If not, the first texcoord stream to load will replace the default map1
// stream.
bool hasDefaultUVSet = false;
for (const UsdGeomPrimvar& primvar: primvars)
{
const SdfValueTypeName typeName = primvar.GetTypeName();
if (typeName == SdfValueTypeNames->TexCoord2fArray
|| (UsdMayaReadUtil::ReadFloat2AsUV() && typeName == SdfValueTypeNames->Float2Array)) {
const TfToken fullName = primvar.GetPrimvarName();
if (fullName == UsdMayaMeshPrimvarTokens->DefaultMayaTexcoordName
|| fullName == UsdUtilsGetPrimaryUVSetName()) {
hasDefaultUVSet = true;
}
}
}

for (const UsdGeomPrimvar& primvar: primvars)
{
const UsdGeomPrimvar& primvar = *iter;
const TfToken name = primvar.GetBaseName();
const TfToken fullName = primvar.GetPrimvarName();
const SdfValueTypeName typeName = primvar.GetTypeName();
Expand All @@ -628,8 +659,8 @@ UsdMayaMeshReadUtils::assignPrimvarsToMesh(const UsdGeomMesh& mesh,
// authored by the user, for example if it was generated by shader
// values and not an authored colorset/entity.
// If it was not really authored, we skip the primvar.
if (name == UsdMayaMeshColorSetTokens->DisplayColorColorSetName ||
name == UsdMayaMeshColorSetTokens->DisplayOpacityColorSetName) {
if (name == UsdMayaMeshPrimvarTokens->DisplayColorColorSetName ||
name == UsdMayaMeshPrimvarTokens->DisplayOpacityColorSetName) {
if (!UsdMayaRoundTripUtil::IsAttributeUserAuthored(primvar)) {
continue;
}
Expand All @@ -646,7 +677,7 @@ UsdMayaMeshReadUtils::assignPrimvarsToMesh(const UsdGeomMesh& mesh,
// Otherwise, if env variable for reading Float2
// as uv sets is turned on, we assume that Float2Array primvars
// are UV sets.
if (!assignUVSetPrimvarToMesh(primvar, meshFn)) {
if (!assignUVSetPrimvarToMesh(primvar, meshFn, hasDefaultUVSet)) {
TF_WARN("Unable to retrieve and assign data for UV set <%s> on "
"mesh <%s>",
name.GetText(),
Expand Down
10 changes: 6 additions & 4 deletions lib/mayaUsd/fileio/utils/meshReadUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ PXR_NAMESPACE_OPEN_SCOPE

class UsdGeomMesh;

#define PXRUSDMAYA_MESH_COLOR_SET_TOKENS \
#define PXRUSDMAYA_MESH_PRIMVAR_TOKENS \
((DisplayColorColorSetName, "displayColor")) \
((DisplayOpacityColorSetName, "displayOpacity"))
((DisplayOpacityColorSetName, "displayOpacity")) \
((DefaultMayaTexcoordName, "map1"))


TF_DECLARE_PUBLIC_TOKENS(UsdMayaMeshColorSetTokens,
TF_DECLARE_PUBLIC_TOKENS(UsdMayaMeshPrimvarTokens,
MAYAUSD_CORE_PUBLIC,
PXRUSDMAYA_MESH_COLOR_SET_TOKENS);
PXRUSDMAYA_MESH_PRIMVAR_TOKENS);

/// Utilities for dealing with USD and RenderMan for Maya mesh/subdiv tags.
namespace UsdMayaMeshReadUtils
Expand Down
3 changes: 2 additions & 1 deletion lib/mayaUsd/fileio/utils/meshWriteUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "meshWriteUtils.h"

#include <mayaUsd/fileio/utils/adaptor.h>
#include <mayaUsd/fileio/utils/meshReadUtils.h>
#include <mayaUsd/fileio/utils/roundTripUtil.h>
#include <mayaUsd/fileio/utils/writeUtil.h>

Expand Down Expand Up @@ -835,7 +836,7 @@ UsdMayaMeshWriteUtils::writeUVSetsAsVec2fPrimvars(const MFnMesh& meshFn,
// behavior, and the name to which it exports.
// The UV Set "map1" is renamed st. This is a Pixar/USD convention.
TfToken setName(uvSetNames[i].asChar());
if (setName == "map1") {
if (setName == UsdMayaMeshPrimvarTokens->DefaultMayaTexcoordName.GetText()) {
setName = UsdUtilsGetPrimaryUVSetName();
}

Expand Down
6 changes: 3 additions & 3 deletions lib/usd/translators/meshWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,18 @@ PxrUsdTranslators_MeshWriter::writeMeshAttrs(const UsdTimeCode& usdTime,

bool isDisplayColor = false;

if (colorSetName == UsdMayaMeshColorSetTokens->DisplayColorColorSetName.GetString()) {
if (colorSetName == UsdMayaMeshPrimvarTokens->DisplayColorColorSetName.GetString()) {
if (!_GetExportArgs().exportDisplayColor) {
continue;
}
isDisplayColor=true;
}

if (colorSetName == UsdMayaMeshColorSetTokens->DisplayOpacityColorSetName.GetString()) {
if (colorSetName == UsdMayaMeshPrimvarTokens->DisplayOpacityColorSetName.GetString()) {
TF_WARN("Mesh \"%s\" has a color set named \"%s\", "
"which is a reserved Primvar name in USD. Skipping...",
finalMesh.fullPathName().asChar(),
UsdMayaMeshColorSetTokens->DisplayOpacityColorSetName
UsdMayaMeshPrimvarTokens->DisplayOpacityColorSetName
.GetText());
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions test/lib/usd/translators/testUsdImportUVSets.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def testImportCompressibleUVSets(self):
"""
mayaCubeMesh = self._GetMayaMesh('CompressibleUVSetsCubeShape')

# We should not see the default "map1" UV set:
self.assertNotIn("map1", mayaCubeMesh.getUVSetNames())

# ALL face vertices should have the same value.
uvSetName = 'ConstantInterpSet'
expectedValues = {}
Expand Down