Quantcast
Channel: mel wiki
Viewing all articles
Browse latest Browse all 610

API: How can I convert from linear internal units to ui units?

$
0
0
When dealing with the API, all linear 'internal units' are held in cm (unless overridden, but I've never seen this happen). But if you're passing that data to mel/Python commands, they expect the values to be in whatever 'ui unit' has been set. By default, the ui unit is cm as well, but many studios will change this value to something else. In my case, it's currently inches...
If these two values line up you won't notice any problems in your code. But if you author code that expects the ui units to be cm and they're not, chaos will ensue.

In the below example, we grab the center-point of the bounding-box of a node, then convert that position into the current ui-units:
import maya.OpenMaya as om

node = 'pSphere1'

# Get the MDagPath for a node:
selList = om.MSelectionList() # MSelectionList
selList.add(node)
mDagPath = om.MDagPath() # MDagPath
selList.getDagPath(0, mDagPath)

# Find the centerpoint based on bounding box, this will be in cm:
dagNodeFunc = om.MFnDagNode(mDagPath) # MFnDagNode
boundingBox = dagNodeFunc.boundingBox() # MBoundingBox
centerPoint = boundingBox.center() # MPoint

#-----------------------
# Now that we have some data, convert it from internal to ui units:

# Convert from cm to current units:
center = []
unitType = om.MDistance.uiUnit() # Get the current UI unit:
for i in range(3):
    distance = om.MDistance(centerPoint[i]) # MDistance, as cm
    converted = distance.asUnits(unitType) # double, converted
    center.append(converted)

print center

Also see:

Viewing all articles
Browse latest Browse all 610

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>