/* virtual */ UsdPrim MayaCameraWriter::write(const UsdTimeCode &usdTime) { // == Write UsdGeomCamera primSchema = UsdGeomCamera::Define(getUsdStage(), getUsdPath()); TF_AXIOM(primSchema); UsdPrim prim = primSchema.GetPrim(); TF_AXIOM(prim); // Write parent class attrs writeTransformAttrs(usdTime, primSchema); // Write the attrs writeCameraAttrs(usdTime, primSchema); return prim; }
/* static */ bool UsdMayaTranslatorCamera::ReadToCamera( const UsdGeomCamera& usdCamera, MFnCamera& cameraObject) { UsdMayaJobImportArgs defaultJobArgs = UsdMayaJobImportArgs::CreateFromDictionary( UsdMayaJobImportArgs::GetDefaultDictionary()); UsdMayaPrimReaderArgs args(usdCamera.GetPrim(), defaultJobArgs); return _ReadToCamera(usdCamera, cameraObject, args, nullptr); }
/* static */ bool UsdMayaTranslatorCamera::Read( const UsdGeomCamera& usdCamera, MObject parentNode, const UsdMayaPrimReaderArgs& args, UsdMayaPrimReaderContext* context) { if (!usdCamera) { return false; } const UsdPrim& prim = usdCamera.GetPrim(); const SdfPath primPath = prim.GetPath(); MStatus status; // Create the transform node for the camera. MObject transformObj; if (!UsdMayaTranslatorUtil::CreateTransformNode(prim, parentNode, args, context, &status, &transformObj)) { return false; } // Create the camera shape node. MDagModifier dagMod; MObject cameraObj = dagMod.createNode(_tokens->MayaCameraTypeName.GetText(), transformObj, &status); CHECK_MSTATUS_AND_RETURN(status, false); status = dagMod.doIt(); CHECK_MSTATUS_AND_RETURN(status, false); TF_VERIFY(!cameraObj.isNull()); MFnCamera cameraFn(cameraObj, &status); CHECK_MSTATUS_AND_RETURN(status, false); const std::string cameraShapeName = prim.GetName().GetString() + _tokens->MayaCameraShapeNameSuffix.GetString(); cameraFn.setName(cameraShapeName.c_str(), &status); CHECK_MSTATUS_AND_RETURN(status, false); if (context) { const SdfPath shapePrimPath = primPath.AppendChild(TfToken(cameraShapeName)); context->RegisterNewMayaNode(shapePrimPath.GetString(), cameraObj); } return _ReadToCamera(usdCamera, cameraFn, args, context); }
/* static */ bool PxrUsdMayaTranslatorCamera::ReadToCamera( const UsdGeomCamera& usdCamera, MFnCamera& cameraObject) { JobImportArgs defaultJobArgs; PxrUsdMayaPrimReaderArgs args( usdCamera.GetPrim(), defaultJobArgs.shadingMode, defaultJobArgs.defaultMeshScheme, defaultJobArgs.readAnimData, defaultJobArgs.useCustomFrameRange, defaultJobArgs.startTime, defaultJobArgs.endTime); return _ReadToCamera(usdCamera, cameraObject, args, NULL); }
bool _ReadToCamera( const UsdGeomCamera& usdCamera, MFnCamera& cameraFn, const UsdMayaPrimReaderArgs& args, UsdMayaPrimReaderContext* context) { MStatus status; // Now translate all of the USD camera attributes over to plugs on the // Maya cameraFn. UsdTimeCode timeCode = UsdTimeCode::EarliestTime(); UsdAttribute usdAttr; TfToken plugName; // Set the type of projection. This is NOT keyable in Maya. TfToken projection; usdCamera.GetProjectionAttr().Get(&projection, timeCode); const bool isOrthographic = (projection == UsdGeomTokens->orthographic); status = cameraFn.setIsOrtho(isOrthographic); CHECK_MSTATUS_AND_RETURN(status, false); // Setup the aperture. usdAttr = usdCamera.GetHorizontalApertureAttr(); plugName = _tokens->MayaCameraAttrNameHorizontalAperture; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context, /* convertToUnit = */ MDistance::kInches)) { return false; } if (isOrthographic) { // For orthographic cameras, we'll re-use the horizontal aperture value // to fill in Maya's orthographicWidth. The film aperture and film // aperture offset plugs in Maya have no effect on orthographic cameras, // but we author them anyway so that the data is preserved. Note also // that Maya stores the orthographicWidth as centimeters. plugName = _tokens->MayaCameraAttrNameOrthographicWidth; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context, /* convertToUnit = */ MDistance::kCentimeters)) { return false; } } usdAttr = usdCamera.GetVerticalApertureAttr(); plugName = _tokens->MayaCameraAttrNameVerticalAperture; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context, /* convertToUnit = */ MDistance::kInches)) { return false; } // XXX: // Lens Squeeze Ratio is DEPRECATED on USD schema. // Writing it out here for backwards compatibility (see bug 123124). cameraFn.setLensSqueezeRatio(1.0); usdAttr = usdCamera.GetHorizontalApertureOffsetAttr(); plugName = _tokens->MayaCameraAttrNameHorizontalApertureOffset; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context, /* convertToUnit = */ MDistance::kInches)) { return false; } usdAttr = usdCamera.GetVerticalApertureOffsetAttr(); plugName = _tokens->MayaCameraAttrNameVerticalApertureOffset; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context, /* convertToUnit = */ MDistance::kInches)) { return false; } // Set the lens parameters. usdAttr = usdCamera.GetFocalLengthAttr(); plugName = _tokens->MayaCameraAttrNameFocalLength; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context)) { return false; } usdAttr = usdCamera.GetFocusDistanceAttr(); plugName = _tokens->MayaCameraAttrNameFocusDistance; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context)) { return false; } usdAttr = usdCamera.GetFStopAttr(); plugName = _tokens->MayaCameraAttrNameFStop; if (!_TranslateUsdAttributeToPlug(usdAttr, cameraFn, plugName, args, context)) { return false; } // Set the clipping planes. This one is a little different from the others // because it is stored in USD as a single GfVec2f value but in Maya as // separate nearClipPlane and farClipPlane attributes. usdAttr = usdCamera.GetClippingRangeAttr(); MPlug nearClipPlug = cameraFn.findPlug( _tokens->MayaCameraAttrNameNearClippingPlane.GetText(), true, &status); CHECK_MSTATUS_AND_RETURN(status, false); MPlug farClipPlug = cameraFn.findPlug( _tokens->MayaCameraAttrNameFarClippingPlane.GetText(), true, &status); CHECK_MSTATUS_AND_RETURN(status, false); if (!_TranslateAnimatedUsdAttributeToPlugs(usdAttr, nearClipPlug, farClipPlug, args, context)) { GfVec2f clippingRange; usdCamera.GetClippingRangeAttr().Get(&clippingRange, timeCode); status = cameraFn.setNearClippingPlane(clippingRange[0]); CHECK_MSTATUS_AND_RETURN(status, false); status = cameraFn.setFarClippingPlane(clippingRange[1]); CHECK_MSTATUS_AND_RETURN(status, false); } return true; }
/* virtual */ bool MayaCameraWriter::writeCameraAttrs(const UsdTimeCode &usdTime, UsdGeomCamera &primSchema) { // Since write() above will take care of any animation on the camera's // transform, we only want to proceed here if: // - We are at the default time and NO attributes on the shape are animated. // OR // - We are at a non-default time and some attribute on the shape IS animated. if (usdTime.IsDefault() == isShapeAnimated()) { return true; } MStatus status; MFnCamera camFn(getDagPath(), &status); CHECK_MSTATUS_AND_RETURN(status, false); // NOTE: We do not use a GfCamera and then call SetFromCamera() below // because we want the xformOps populated by the parent class to survive. // Using SetFromCamera() would stomp them with a single "transform" xformOp. // Set the type of projection. if (camFn.isOrtho()) { primSchema.GetProjectionAttr().Set(UsdGeomTokens->orthographic, usdTime); } else { primSchema.GetProjectionAttr().Set(UsdGeomTokens->perspective, usdTime); } // Setup the aperture. primSchema.GetHorizontalApertureAttr().Set( float(PxrUsdMayaUtil::ConvertInchesToMM( camFn.horizontalFilmAperture() * camFn.lensSqueezeRatio())), usdTime); primSchema.GetVerticalApertureAttr().Set( float(PxrUsdMayaUtil::ConvertInchesToMM( camFn.verticalFilmAperture() * camFn.lensSqueezeRatio())), usdTime); primSchema.GetHorizontalApertureOffsetAttr().Set( float(camFn.horizontalFilmOffset()), usdTime); primSchema.GetVerticalApertureOffsetAttr().Set( float(camFn.verticalFilmOffset()), usdTime); // Set the lens parameters. primSchema.GetFocalLengthAttr().Set( float(camFn.focalLength()), usdTime); // Always export focus distance and fStop regardless of what // camFn.isDepthOfField() says. Downstream tools can choose to ignore or // override them. primSchema.GetFocusDistanceAttr().Set( float(camFn.focusDistance()), usdTime); primSchema.GetFStopAttr().Set( float(camFn.fStop()), usdTime); // Set the clipping planes. GfVec2f clippingRange(camFn.nearClippingPlane(), camFn.farClippingPlane()); primSchema.GetClippingRangeAttr().Set(clippingRange, usdTime); return true; }