USD export: convert active UV map to st.

Updated the logic of the convert_uv_to_st option to rename the
active UV set to 'st', to allow specifying the default UV set
when there are multiple UVs.  Previously, this option assumed
a single UV set.
This commit is contained in:
Michael Kowalski 2022-04-12 18:17:13 -04:00
parent 2b3f5cb965
commit d742007c8e
4 changed files with 20 additions and 9 deletions

View File

@ -698,8 +698,7 @@ void WM_OT_usd_export(struct wmOperatorType *ot)
"convert_uv_to_st",
false,
"Convert uv to st",
"When checked, the USD exporter will convert all uv map names to interchangeable 'st'"
"(Assumes one uv layout per mesh)");
"Export the active uv map as USD primvar named 'st'");
RNA_def_boolean(ot->srna,
"convert_orientation",

View File

@ -236,6 +236,9 @@ pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(const HierarchyCont
}
if (material->use_nodes && this->usd_export_context_.export_params.generate_preview_surface) {
std::string active_uv = get_mesh_active_uvlayer_name(context.object);
if (usd_export_context_.export_params.convert_uv_to_st && !active_uv.empty()) {
active_uv = "st";
}
create_usd_preview_surface_material(
this->usd_export_context_, material, usd_material, active_uv);
}

View File

@ -193,10 +193,18 @@ struct USDMeshData {
void USDGenericMeshWriter::write_custom_data(const Mesh *mesh, pxr::UsdGeomMesh usd_mesh)
{
const CustomData *ldata = &mesh->ldata;
/* Index of the UV layer to be renamed "st", set to the active UV layer index if
* the convert_uv_to_st option is enabled and set to -1 otherwise. */
const int st_layer_idx = usd_export_context_.export_params.convert_uv_to_st ?
CustomData_get_active_layer_index(ldata, CD_MLOOPUV) :
-1;
for (int layer_idx = 0; layer_idx < ldata->totlayer; layer_idx++) {
const CustomDataLayer *layer = &ldata->layers[layer_idx];
if (layer->type == CD_MLOOPUV && usd_export_context_.export_params.export_uvmaps) {
write_uv_maps(mesh, usd_mesh, layer);
const char *name_override = st_layer_idx == layer_idx ? "st" : nullptr;
write_uv_maps(mesh, usd_mesh, layer, name_override);
}
else if (layer->type == CD_MLOOPCOL &&
usd_export_context_.export_params.export_vertex_colors) {
@ -207,7 +215,8 @@ void USDGenericMeshWriter::write_custom_data(const Mesh *mesh, pxr::UsdGeomMesh
void USDGenericMeshWriter::write_uv_maps(const Mesh *mesh,
pxr::UsdGeomMesh usd_mesh,
const CustomDataLayer *layer)
const CustomDataLayer *layer,
const char *name_override)
{
pxr::UsdTimeCode timecode = get_export_time_code();
@ -215,7 +224,8 @@ void USDGenericMeshWriter::write_uv_maps(const Mesh *mesh,
* The primvar name is the same as the UV Map name. This is to allow the standard name "st"
* for texture coordinates by naming the UV Map as such, without having to guess which UV Map
* is the "standard" one. */
pxr::TfToken primvar_name(pxr::TfMakeValidIdentifier(layer->name));
pxr::TfToken primvar_name(name_override ? name_override :
pxr::TfMakeValidIdentifier(layer->name));
if (usd_export_context_.export_params.author_blender_name) {
// Store original layer name in blender
@ -228,9 +238,6 @@ void USDGenericMeshWriter::write_uv_maps(const Mesh *mesh,
.Set(std::string(layer->name), pxr::UsdTimeCode::Default());
}
if (usd_export_context_.export_params.convert_uv_to_st)
primvar_name = pxr::TfToken("st");
pxr::UsdGeomPrimvar uv_coords_primvar = usd_mesh.CreatePrimvar(
primvar_name, pxr::SdfValueTypeNames->TexCoord2fArray, pxr::UsdGeomTokens->faceVarying);

View File

@ -34,7 +34,9 @@ class USDGenericMeshWriter : public USDAbstractWriter {
pxr::UsdGeomMesh usd_mesh,
const MaterialFaceGroups &usd_face_groups);
void write_custom_data(const Mesh *mesh, pxr::UsdGeomMesh usd_mesh);
void write_uv_maps(const Mesh *mesh, pxr::UsdGeomMesh usd_mesh, const CustomDataLayer *layer);
void write_uv_maps(const Mesh *mesh, pxr::UsdGeomMesh usd_mesh,
const CustomDataLayer *layer,
const char *name_override = nullptr);
void write_vertex_colors(const Mesh *mesh,
pxr::UsdGeomMesh usd_mesh,
const CustomDataLayer *layer);