If you change the units in the preferences settings window, for example to meters, and then execute the “save preferences” command, the settings will be valid only for the current scene opened in Maya. That is, when you restart Maya or create a new scene, the units settings will return to the previous ones, saved in the preferences file userPref.mel.
If you change the unit preferences (for example, to meters) and save the scene file, these settings will be saved in the scene file as a MEL command:
currentUnit -l meter;
Also, using the “scriptNode” command, it is possible to write a script to the scene file (in the section of script commands executed when opening the file), changing the units when opening the scene file.
So it is obvious that the current units in Maya and the value of the optionVar 'workingUnitLinear'
parameter CAN HAVE DIFFERENT VALUES!
Python Way
# query scene unit:
maya_query_unit = cmds.currentUnit(query = True, linear = True)
# Result: 'cm'
# query scene unit (full name):
maya_query_unit = cmds.currentUnit(query = True, linear = True, fullName = True)
# Result: 'centimeter'
# set scene unit (full name):
maya_set_unit = cmds.currentUnit(linear = 'meter')
# Result: 'meter'
# or:
maya_set_unit = cmds.currentUnit(linear = 'm')
# Result: 'meter'
# get option var string value for 'workingUnitLinear':
maya_option_var_get_unit = cmds.optionVar(query = 'workingUnitLinear')
# Result: 'cm'
# set option var string value for 'workingUnitLinear':
cmds.optionVar(category = 'Settings', stringValue = ('workingUnitLinear', 'm'))
# and seved in pref file userPref.mel (-general: Save the general prefs to disk (optionVars)):
cmds.savePrefs(general = True)
# // Result: 1
# Saving preferences to : C:/Users/UserName/Documents/maya/2025/prefs/userPrefs.mel
# Result in file userPref.mel:
# ...
# // Settings
# optionVar -cat "Settings"
# -fv "positionalTolerance" 1e-08
# -sv "workingUnitLinear" "m"
# ;
# ...
MEL Way
// query scene unit:
$maya_query_unit = `currentUnit -query -linear`;
// Result: cm
// query scene unit (full name):
$maya_query_unit = `currentUnit -query -linear -fullName`;
// Result: centimeter
// set scene unit (full name):
$maya_set_unit = `currentUnit -linear "meter"`;
// Result: 'meter'
// or:
$maya_set_unit = `currentUnit -linear "m"`;
// Result: 'meter'
// get option var string value for 'workingUnitLinear':
$maya_option_var_get_unit = `optionVar -query "workingUnitLinear"`;
// Result: cm
// set option var string value for 'workingUnitLinear':
optionVar -category "Settings" -stringValue "workingUnitLinear" "m";
// and seved in pref file userPref.mel (-general: Save the general prefs to disk (optionVars)):
savePrefs -general;
// Result: 1
// Saving preferences to : C:/Users/UserName/Documents/maya/2025/prefs/userPrefs.mel
// Result in file userPref.mel:
// ...
// Settings
//optionVar -cat "Settings"
// -fv "positionalTolerance" 1e-08
// -sv "workingUnitLinear" "m"
// ;
//...
N.B.
Keep in mind that if you change the current units to centimeters, the line -sv "workingUnitLinear" "cm"
will not appear/change in the preference file userPref.mel
!
This is because centimeters are the default unit in Maya. And default values are usually not written to the userPref.mel
preference file.
Maya Units:
default_unit = 'cm' or 'centimeter' (default)
'mm' or 'millimeter' - default_unit*0.1
'm' or 'meter' - default_unit*100.0
'km' or 'kilometer' - default_unit*100000.0
'in' or 'inch' - default_unit*2.54
'ft' or 'foot' - default_unit*30.48
'yd' or 'yard' - default_unit*91.44
'mi' or 'mile' - default_unit*160934.0
Regarding the bounding box:
Note the command: exactWorldBoundingBox
import maya.api.OpenMaya as om2
input_obj = cmds.polyCube(w=25, h=7, d=12, sx=1, sy=1, sz=1, ax=(0, 1, 0), cuv=4, ch=0)
exWBB = cmds.exactWorldBoundingBox(input_obj,
ignoreInvisible = True,
calculateExactly = True)
# Result: [-12.5, -3.5, -6.0, 12.5, 3.5, 6.0]
bbox = om2.MBoundingBox(om2.MPoint(exWBB[0], exWBB[1], exWBB[2], 1),
om2.MPoint(exWBB[3], exWBB[4], exWBB[5], 1))
# Result: maya.api.OpenMaya.MBoundingBox(maya.api.OpenMaya.MPoint(-12.5, -3.5, -6, 1), maya.api.OpenMaya.MPoint(12.5, 3.5, 6, 1))
bbox_width = bbox.width
# Result: 25.0
bbox_height = bbox.height
# Result: 7.0
bbox_depth = bbox.depth
# Result: 12.0
bbox_max_xyz = list(bbox.max)[:3]
# Result: [12.5, 3.5, 6.0]
bbox_min_xyz = list(bbox.min)[:3]
# Result: [-12.5, -3.5, -6.0]
out_bbox = 1.0
scale_factor = out_bbox/max(bbox_width, bbox_height, bbox_depth)
# Result: 0.04
cmds.scale(scale_factor, scale_factor, scale_factor, input_obj, relative = True)
exactWorldBoundingBox:
exactWorldBoundingBox command (Python)
exactWorldBoundingBox command (MEL)
optionVar:
optionVar command (Python)
optionVar command (MEL)
currentUnit:
currentUnit command (Python)
currentUnit command (MEL)
MBoundingBox
OpenMaya.MBoundingBox Class Reference
Harmony, positivity and good luck!