How to resize or set sizes of an object

Does rhinoscriptsyntax provide a method to set an object’s X, Y and Z size, as one would do interactively with BoxEdit?

I’m looking for something like this:

aObjects = rs.AllObjects()

for object in aObjects:
    name = rs.ObjectName(object)
    
    if name == "somename":
        rs.ObjectSetSize(object, newX, newY, newZ)
  • Get the object’s bounding box and from it get the current x, y, and z dimensions of the object.
  • Create ratios between the current sizes and the desired sizes in all three axes.
  • Then apply a non-uniform scale to the object using those ratios.

Thanks. That was my “last resort” solution. Here’s what I came up with (not claiming perfection here):

def SetSize(object, newX, newY, newZ):
    boundingBox = rs.BoundingBox(object)
    sizeX = boundingBox[1][0] - boundingBox[0][0]
    sizeY = boundingBox[2][1] - boundingBox[0][1]
    sizeZ = boundingBox[4][2] - boundingBox[0][2]
    
    originX = boundingBox[1][0] - sizeX / 2.0
    originY = boundingBox[2][1] - sizeY / 2.0
    originZ = boundingBox[4][2] - sizeZ / 2.0

    scaleX = newX / sizeX
    scaleY = newY / sizeY
    scaleZ = newZ / sizeZ
    
    rs.ScaleObject(object, [originX, originY, originZ], [scaleX, scaleY, scaleZ])
    
def AdjustSize(object, deltaX, deltaY, deltaZ):
    boundingBox = rs.BoundingBox(object)
    sizeX = boundingBox[1][0] - boundingBox[0][0]
    sizeY = boundingBox[2][1] - boundingBox[0][1]
    sizeZ = boundingBox[4][2] - boundingBox[0][2]
    
    originX = boundingBox[1][0] - sizeX / 2.0
    originY = boundingBox[2][1] - sizeY / 2.0
    originZ = boundingBox[4][2] - sizeZ / 2.0

    scaleX = (sizeX + deltaX) / sizeX
    scaleY = (sizeY + deltaY) / sizeY
    scaleZ = (sizeZ + deltaZ) / sizeZ
    
    rs.ScaleObject(object, [originX, originY, originZ], [scaleX, scaleY, scaleZ])

Yep, that looks fine. Now you can use a trick to compact your code a bit if you wish - it relies on the fact that in python/RhinoCommon, point3d objects have properties and methods associated with them. For example, to get the X coordinate of a point3d, you can simply type pt.X Also, you can perform operations with points, such as adding and subtracting them. Subtracting one point from another results in a vector from the first to the second, adding them results in the addition of the two points’ x, y and z coordinates to produce a new point.

So you can get the X, Y, and Z extents of the bounding box by subtracting bb[0] from bb[6], the x, y and z components of the resulting vector will be the lengths of the bb sides. Similarly, you can get the bb center by adding the two diagonal points and dividing by 2 (i.e. getting the average/mid point between two points).

So your code could look like this:

def SetSize(object, newX, newY, newZ):
    bb = rs.BoundingBox(object)
    dvec=bb[6]-bb[0]
    ctr=(bb[0]+bb[6])/2
    rs.ScaleObject(object,ctr,[newX/dvec.X,newY/dvec.Y,newZ/dvec.Z])
    
def AdjustSize(object, dX, dY, dZ):
    bb = rs.BoundingBox(object)
    dvec=bb[6]-bb[0]
    ctr=(bb[0]+bb[6])/2
    rs.ScaleObject(object,ctr,[(dvec.X+dX)/dvec.X,(dvec.Y+dY)/dvec.Y,(dvec.Z+dZ)/dvec.Z])

That’s great. Thanks.