Maya Python:匹配两个不同物体的边界框

Maya Python: matching the bounding boxes of two different objects

我有两个对象,它们总是具有随机不同的边界框大小,尤其是高度。但是两者都将在原点处有枢轴。

我想通过缩放将一个对象的边界框高度与另一个对象的边界框高度相匹配。但我只能考虑以非常小的增量逐渐扩大较小的。这似乎有点乏味和次优。

在 Python 和 Maya 中是否有更好的方法来匹配两个边界框的高度?

您应该能够获取较大对象的高度并将其除以较小对象的高度以获得用于较小对象的比例因子。

非常非常简单的例子(伪代码):

objectA height = 20  
objectB height = 10  
20 / 10 = 2 (so, scale objectB by 2 to match the height)

您的实际代码将是从每个边界框提取高度,比较高度以确定较小的对象和比例因子,然后缩放较小的对象。希望对您有所帮助!

这是一个函数,它将给定的一组对象缩放到第二组对象的组合边界框大小。 (也适用于单个对象)

def matchScale(to, frm, scale=True):
    '''Scale each of the given objects to the combined bounding box of a second set of objects.

    :Parameters:
        to (str)(obj)(list) = The object(s) to scale.
        frm (str)(obj)(list) = The object(s) to get a bounding box size from.
        scale (bool) = Scale the objects. Else, just return the scale value.

    :Return:
        (list) scale values as [x,y,z,x,y,z...]
    '''
    to = pm.ls(to, flatten=True)
    frm = pm.ls(frm, flatten=True)

    xmin, ymin, zmin, xmax, ymax, zmax = pm.exactWorldBoundingBox(frm)
    ax, ay, az = aBoundBox = [xmax-xmin, ymax-ymin, zmax-zmin]

    result=[]
    for obj in to:

        xmin, ymin, zmin, xmax, ymax, zmax = pm.exactWorldBoundingBox(obj)
        bx, by, bz = bBoundBox = [xmax-xmin, ymax-ymin, zmax-zmin]

        oldx, oldy, oldz = bScaleOld = pm.xform(obj, q=1, s=1, r=1)
        diffx, diffy, diffz = boundDifference = [ax/bx, ay/by, az/bz]
        bScaleNew = [oldx*diffx, oldy*diffy, oldz*diffz]
        [result.append(i) for i in bScaleNew]

        if scale:
            pm.xform(obj, scale=bScaleNew)

    return result