|
| 1 | +import maya.cmds as _cmds |
| 2 | +import maya.api.OpenMaya as _om |
| 3 | +import mayaUsd.ufe as _ufe |
| 4 | + |
| 5 | +from mayaUsdLibRegisterStrings import getMayaUsdLibString as _getMayaUsdLibString |
| 6 | + |
| 7 | +from pxr import UsdGeom as _UsdGeom |
| 8 | + |
| 9 | + |
| 10 | +def convertUpAxisAndUnit(shapeNode, convertUpAxis, convertUnit, conversionMethod): |
| 11 | + ''' |
| 12 | + Edit the USD stage associated with the given Maya stage proxy node to |
| 13 | + convert the up-axis or the units used to match what is in the USD file |
| 14 | + with what Maya is using. |
| 15 | + ''' |
| 16 | + # If neither up-axis nor unit are requested to be modified, return immediately. |
| 17 | + conversionInfo = StageConversionInfo(shapeNode, convertUpAxis, convertUnit) |
| 18 | + if not conversionInfo.needUnitsConversion and not conversionInfo.needUpAxisConversion: |
| 19 | + return |
| 20 | + |
| 21 | + resultMsg = _getMayaUsdLibString("kStageConversionSuccessful") |
| 22 | + |
| 23 | + if conversionMethod.lower() == 'rotatescale': |
| 24 | + convertUpAxisAndUnitByModifyingStage(conversionInfo) |
| 25 | + elif conversionMethod.lower() == 'overwriteprefs': |
| 26 | + convertUpAxisAndUnitByModifyingPrefs(conversionInfo) |
| 27 | + else: |
| 28 | + resultMsg = _getMayaUsdLibString("kStageConversionUnknownMethod") % conversionMethod |
| 29 | + |
| 30 | + print(resultMsg) |
| 31 | + |
| 32 | + |
| 33 | +class StageConversionInfo: |
| 34 | + ''' |
| 35 | + Analyze the contents of the USD file and compare it to the Maya settings |
| 36 | + to determine what actions need to be done to match them. |
| 37 | + ''' |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def _isMayaUpAxisZ(): |
| 41 | + return _cmds.upAxis(query=True, axis=True).lower() == 'z' |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def _isUsdUpAxisZ(stage): |
| 45 | + return _UsdGeom.GetStageUpAxis(stage).lower() == 'z' |
| 46 | + |
| 47 | + _mayaToMetersPerUnit = { |
| 48 | + _om.MDistance.kInches : _UsdGeom.LinearUnits.inches, |
| 49 | + _om.MDistance.kFeet : _UsdGeom.LinearUnits.feet, |
| 50 | + _om.MDistance.kYards : _UsdGeom.LinearUnits.yards, |
| 51 | + _om.MDistance.kMiles : _UsdGeom.LinearUnits.miles, |
| 52 | + _om.MDistance.kMillimeters : _UsdGeom.LinearUnits.millimeters, |
| 53 | + _om.MDistance.kCentimeters : _UsdGeom.LinearUnits.centimeters, |
| 54 | + _om.MDistance.kKilometers : _UsdGeom.LinearUnits.kilometers, |
| 55 | + _om.MDistance.kMeters : _UsdGeom.LinearUnits.meters, |
| 56 | + } |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def _convertMayaUnitToMetersPerUnit(mayaUnits): |
| 60 | + if mayaUnits not in StageConversionInfo._mayaToMetersPerUnit: |
| 61 | + return _UsdGeom.LinearUnits.centimeters |
| 62 | + return StageConversionInfo._mayaToMetersPerUnit[mayaUnits] |
| 63 | + |
| 64 | + _metersPerUnitToMayaUnitName = { |
| 65 | + _UsdGeom.LinearUnits.inches : "inch", |
| 66 | + _UsdGeom.LinearUnits.feet : "foot", |
| 67 | + _UsdGeom.LinearUnits.yards : "yard", |
| 68 | + _UsdGeom.LinearUnits.miles : "mile", |
| 69 | + _UsdGeom.LinearUnits.millimeters : "mm", |
| 70 | + _UsdGeom.LinearUnits.centimeters : "cn", |
| 71 | + _UsdGeom.LinearUnits.kilometers : "km", |
| 72 | + _UsdGeom.LinearUnits.meters : "m", |
| 73 | + } |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def _convertMetersPerUnitToMayaUnitName(metersPerUnit): |
| 77 | + if metersPerUnit not in StageConversionInfo._metersPerUnitToMayaUnitName: |
| 78 | + return "cm" |
| 79 | + return StageConversionInfo._metersPerUnitToMayaUnitName[metersPerUnit] |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def _getMayaMetersPerUnit(): |
| 83 | + mayaUnits = _om.MDistance.internalUnit() |
| 84 | + return StageConversionInfo._convertMayaUnitToMetersPerUnit(mayaUnits) |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def _getUsdMetersPerUnit(stage): |
| 88 | + return _UsdGeom.GetStageMetersPerUnit(stage) |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def _getStageFromShapeNode(shapeNode): |
| 92 | + res = _cmds.ls(shapeNode, l=True) |
| 93 | + fullStageName = res[0] |
| 94 | + return _ufe.getStage(fullStageName) |
| 95 | + |
| 96 | + def __init__(self, shapeNode, convertUpAxis, convertUnit): |
| 97 | + self.shapeNode = shapeNode |
| 98 | + self.stage = self._getStageFromShapeNode(shapeNode) |
| 99 | + |
| 100 | + self.isMayaUpAxisZ = self._isMayaUpAxisZ() |
| 101 | + self.isUsdUpAxisZ = self._isUsdUpAxisZ(self.stage) |
| 102 | + self.needUpAxisConversion = convertUpAxis and (self.isMayaUpAxisZ != self.isUsdUpAxisZ) |
| 103 | + |
| 104 | + self.mayaMetersPerUnit = self._getMayaMetersPerUnit() |
| 105 | + self.usdMetersPerUnit = self._getUsdMetersPerUnit(self.stage) |
| 106 | + self.needUnitsConversion = convertUnit and (self.mayaMetersPerUnit != self.usdMetersPerUnit) |
| 107 | + |
| 108 | + |
| 109 | +def convertUpAxisAndUnitByModifyingStage(conversionInfo): |
| 110 | + ''' |
| 111 | + Handle the differences of up-axis and units from the USD file by modifying |
| 112 | + the Maya proxy shape node transform to compensate for the differences. |
| 113 | + ''' |
| 114 | + if conversionInfo.needUpAxisConversion: |
| 115 | + angle = 90 if conversionInfo.isMayaUpAxisZ else -90 |
| 116 | + _cmds.rotate(angle, 0, 0, conversionInfo.shapeNode, relative=True, euler=True, pivot=(0, 0, 0), forceOrderXYZ=True) |
| 117 | + |
| 118 | + if conversionInfo.needUnitsConversion: |
| 119 | + factor = conversionInfo.usdMetersPerUnit / conversionInfo.mayaMetersPerUnit |
| 120 | + _cmds.scale(factor, factor, factor, conversionInfo.shapeNode, relative=True, pivot=(0, 0, 0), scaleXYZ=True) |
| 121 | + |
| 122 | + |
| 123 | +def convertUpAxisAndUnitByModifyingPrefs(conversionInfo): |
| 124 | + ''' |
| 125 | + Handle the differences of up-axis and units from the USD file by modifying |
| 126 | + the Maya preferences to match the USD file. |
| 127 | + ''' |
| 128 | + if conversionInfo.needUpAxisConversion: |
| 129 | + newAxis = 'z' if conversionInfo.isUsdUpAxisZ else 'y' |
| 130 | + _cmds.upAxis(axis=newAxis) |
| 131 | + |
| 132 | + if conversionInfo.needUnitsConversion: |
| 133 | + newUnit = conversionInfo._convertMetersPerUnitToMayaUnitName(conversionInfo.usdMetersPerUnit) |
| 134 | + _cmds.currentUnit(linear=newUnit) |
| 135 | + |
0 commit comments