[Bf-blender-cvs] [4e5ef649944] sybren-usd: USD: Export UV maps of meshes

Sybren A. Stüvel noreply at git.blender.org
Fri Jul 12 14:16:31 CEST 2019


Commit: 4e5ef6499440806d586e4aa5e21a2e3fb11b6a05
Author: Sybren A. Stüvel
Date:   Fri Jul 12 14:15:08 2019 +0200
Branches: sybren-usd
https://developer.blender.org/rB4e5ef6499440806d586e4aa5e21a2e3fb11b6a05

USD: Export UV maps of meshes

Each UV map is stored on the mesh in a separate primvar. Materials can
refer to these UV maps, but this is not yet exported by Blender.

===================================================================

M	source/blender/usd/intern/usd_writer_mesh.cc
M	source/blender/usd/intern/usd_writer_mesh.h

===================================================================

diff --git a/source/blender/usd/intern/usd_writer_mesh.cc b/source/blender/usd/intern/usd_writer_mesh.cc
index 14b5761ead9..d8807157f53 100644
--- a/source/blender/usd/intern/usd_writer_mesh.cc
+++ b/source/blender/usd/intern/usd_writer_mesh.cc
@@ -74,6 +74,31 @@ struct USDMeshData {
   pxr::VtFloatArray crease_sharpnesses;
 };
 
+void USDGenericMeshWriter::write_uv_maps(const Mesh *mesh, pxr::UsdGeomMesh usd_mesh)
+{
+  pxr::UsdTimeCode timecode = get_export_time_code();
+
+  const CustomData *ldata = &mesh->ldata;
+  for (int layer_idx = 0; layer_idx < ldata->totlayer; layer_idx++) {
+    const CustomDataLayer *layer = &ldata->layers[layer_idx];
+    if (layer->type != CD_MLOOPUV) {
+      continue;
+    }
+
+    // UV coordinates are stored in a Primvar on the Mesh, and can be referenced from materials.
+    pxr::TfToken primvar_name(pxr::TfMakeValidIdentifier(std::string("uv_") + layer->name));
+    pxr::UsdGeomPrimvar uv_coords_primvar = usd_mesh.CreatePrimvar(
+        primvar_name, pxr::SdfValueTypeNames->TexCoord2fArray, pxr::UsdGeomTokens->faceVarying);
+
+    MLoopUV *mloopuv = static_cast<MLoopUV *>(layer->data);
+    pxr::VtArray<pxr::GfVec2f> uv_coords;
+    for (int loop_idx = 0; loop_idx < mesh->totloop; loop_idx++) {
+      uv_coords.push_back(pxr::GfVec2f(mloopuv[loop_idx].uv));
+    }
+    uv_coords_primvar.Set(uv_coords, timecode);
+  }
+}
+
 void USDGenericMeshWriter::write_mesh(HierarchyContext &context, Mesh *mesh)
 {
   pxr::UsdTimeCode timecode = get_export_time_code();
@@ -97,6 +122,8 @@ void USDGenericMeshWriter::write_mesh(HierarchyContext &context, Mesh *mesh)
     usd_mesh.CreateCreaseSharpnessesAttr().Set(usd_mesh_data.crease_sharpnesses, timecode);
   }
 
+  write_uv_maps(mesh, usd_mesh);
+
   // TODO(Sybren): figure out what happens when the face groups change.
   if (frame_has_been_written_) {
     return;
diff --git a/source/blender/usd/intern/usd_writer_mesh.h b/source/blender/usd/intern/usd_writer_mesh.h
index b137b16a843..3fef22dd2b5 100644
--- a/source/blender/usd/intern/usd_writer_mesh.h
+++ b/source/blender/usd/intern/usd_writer_mesh.h
@@ -24,6 +24,7 @@ class USDGenericMeshWriter : public USDAbstractWriter {
   void assign_materials(const HierarchyContext &context,
                         pxr::UsdGeomMesh usd_mesh,
                         const std::map<short, pxr::VtIntArray> &usd_face_groups);
+  void write_uv_maps(const Mesh *mesh, pxr::UsdGeomMesh usd_mesh);
 };
 
 class USDMeshWriter : public USDGenericMeshWriter {



More information about the Bf-blender-cvs mailing list