Skip to content

Instantly share code, notes, and snippets.

@JFlynnXYZ
Last active August 29, 2015 13:58
Show Gist options
  • Save JFlynnXYZ/9940753 to your computer and use it in GitHub Desktop.
Save JFlynnXYZ/9940753 to your computer and use it in GitHub Desktop.
Fits an object to the size of a group/objects world bounding box (used for 3d Projections and Planar Mapping)
import maya.cmds as cmds
def fit_to_group_bbox(obj, group):
'''Used to scale and translate a 3d projection node used for planar
mapping onto the size of a group or object. I haven't found any
other use for it as of yet, but I have kept the variables open
in-case someone does.
obj : is the name of the object or projection you want to
resize
group : is the name of the object or group (usually Shading
Group) from which the bounding box will be retrieved and
that will be used to fit to.
On Exit : the "obj" is resized to the exact world bounding box of
"group"
'''
bbox = cmds.exactWorldBoundingBox(group)
scl = []
trn = []
for i in range(3):
scl.append(float( (bbox[i+3] - bbox[i]) ) / 2)
trn.append(float( (bbox[i+3] + bbox[i]) ) / 2)
cmds.setAttr(*['%s.scale' % obj]+scl, type='double3')
cmds.setAttr(*['%s.translate' % obj]+trn, type='double3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment