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

Updated NURBS curve export for widths and knots attributes #2326

Merged
merged 4 commits into from
May 11, 2022
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
63 changes: 56 additions & 7 deletions lib/usd/translators/nurbsCurveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

#include <maya/MDoubleArray.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnDoubleArrayData.h>
#include <maya/MFnFloatArrayData.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MPointArray.h>

Expand Down Expand Up @@ -139,7 +142,45 @@ bool PxrUsdTranslators_NurbsCurveWriter::writeNurbsCurveAttrs(
if (!TF_VERIFY(curveOrder[0] <= curveVertexCounts[0])) {
return false;
}
curveWidths[0] = 1.0; // TODO: Retrieve from custom attr

// Find the curve width attribute
MObject widthObj;
MPlug widthPlug = curveFn.findPlug("widths", true, &status);
if (!status) {
TF_WARN(
"No NURBS curves width(s) attribute found for path: %s",
GetDagPath().fullPathName().asChar());
} else {
widthPlug.getValue(widthObj);
}
if (!widthObj.isNull() && widthObj.apiType() != MFn::kInvalid) {
// Copy the widths from the found data
if (widthObj.apiType() == MFn::kDoubleArrayData) {
MFnDoubleArrayData widthArray;
widthArray.setObject(widthObj);
const uint32_t numElements = widthArray.length();
curveWidths.resize(numElements);
for (uint32_t i = 0; i < numElements; ++i) {
curveWidths[i] = widthArray[i];
}
} else if (widthObj.apiType() == MFn::kFloatArrayData) {
MFnFloatArrayData widthArray;
widthArray.setObject(widthObj);
const uint32_t numElements = widthArray.length();
curveWidths.resize(numElements);
for (uint32_t i = 0; i < numElements; ++i) {
curveWidths[i] = widthArray[i];
}
}
} else if (
MFnNumericAttribute(widthPlug.attribute()).unitType() == MFnNumericData::kDouble
|| MFnNumericAttribute(widthPlug.attribute()).unitType() == MFnNumericData::kFloat) {
// Copy the widths from the plug value
curveWidths.push_back(widthPlug.asFloat());
} else {
// Default to a contant width of 1.0f
curveWidths[0] = 1;
}

double mayaKnotDomainMin;
double mayaKnotDomainMax;
Expand All @@ -159,18 +200,26 @@ bool PxrUsdTranslators_NurbsCurveWriter::writeNurbsCurveAttrs(
MDoubleArray mayaCurveKnots;
status = curveFn.getKnots(mayaCurveKnots);
CHECK_MSTATUS_AND_RETURN(status, false);
VtDoubleArray curveKnots(mayaCurveKnots.length() + 2); // all knots batched together
for (unsigned int i = 0; i < mayaCurveKnots.length(); i++) {
curveKnots[i + 1] = mayaCurveKnots[i];
}
const uint32_t mayaKnotsCount = mayaCurveKnots.length();
VtDoubleArray curveKnots;
auto copyKnotsFromIdx = [&mayaCurveKnots, &curveKnots, mayaKnotsCount](size_t fromIdx) {
memcpy(
((double*)curveKnots.cdata()) + fromIdx,
(const double*)&mayaCurveKnots[0],
sizeof(double) * mayaKnotsCount);
};
if (wrap) {
// Insert wrapping knots at either end of the vector
curveKnots.resize(mayaKnotsCount + 2);
copyKnotsFromIdx(1);
curveKnots[0] = curveKnots[1]
- (curveKnots[curveKnots.size() - 2] - curveKnots[curveKnots.size() - 3]);
curveKnots[curveKnots.size() - 1]
= curveKnots[curveKnots.size() - 2] + (curveKnots[2] - curveKnots[1]);
} else {
curveKnots[0] = curveKnots[1];
curveKnots[curveKnots.size() - 1] = curveKnots[curveKnots.size() - 2];
// Copy across the knots as-is, don't insert extra knots
curveKnots.resize(mayaKnotsCount);
copyKnotsFromIdx(0);
}

// Gprim
Expand Down
29 changes: 29 additions & 0 deletions test/lib/usd/translators/testUsdExportNurbsCurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,39 @@ def testExport(self):
nc = UsdGeom.NurbsCurves.Get(stage, '/curve1')
self.assertEqual(nc.GetWidthsAttr().Get(), Vt.FloatArray([1.0]))
self.assertEqual(nc.GetWidthsInterpolation(), UsdGeom.Tokens.constant)
self.assertEqual(nc.GetKnotsAttr().Get(), Vt.DoubleArray([0, 0, 0, 1, 2, 3, 4, 5, 5, 5]))
self.assertEqual(nc.GetRangesAttr().Get(), Vt.Vec2dArray([Gf.Vec2d(0, 5)]))
self.assertEqual(nc.GetOrderAttr().Get(), Vt.IntArray([4]))
self.assertEqual(nc.GetCurveVertexCountsAttr().Get(), Vt.IntArray([8]))

def testExportWidths(self):
'''
Test export with manually authored widths on the curve.
'''
expectedWidths = [1., 2., 3., 4., 5., 6., 7., 8.]
cmds.select("curveShape1")
cmds.addAttr(longName="widths", dt="floatArray")
cmds.setAttr("curveShape1.widths", expectedWidths ,type="floatArray")
self.assertEqual(cmds.getAttr("curveShape1.widths"), expectedWidths)

usdFile = os.path.abspath('UsdExportNurbsCurveTest_Widths.usda')
cmds.usdExport(mergeTransformAndShape=True, file=usdFile,
shadingMode='none')

stage = Usd.Stage.Open(usdFile)

nc = UsdGeom.NurbsCurves.Get(stage, '/curve1')
self.assertEqual(nc.GetWidthsAttr().Get(), Vt.FloatArray(expectedWidths))
self.assertEqual(nc.GetWidthsInterpolation(), UsdGeom.Tokens.vertex)
self.assertEqual(nc.GetKnotsAttr().Get(), Vt.DoubleArray([0, 0, 0, 1, 2, 3, 4, 5, 5, 5]))
self.assertEqual(nc.GetRangesAttr().Get(), Vt.Vec2dArray([Gf.Vec2d(0, 5)]))
self.assertEqual(nc.GetOrderAttr().Get(), Vt.IntArray([4]))
self.assertEqual(nc.GetCurveVertexCountsAttr().Get(), Vt.IntArray([8]))

with self.assertRaises(ValueError):
cmds.deleteAttr("widths")
cmds.getAttr("curveShape1.widths")

def testNotExportViaPython(self):
'''
Test that filtering curves works when invoked via Python:
Expand Down