形状を指定のY位置で接地させる (スクリプト)

物体の接地をスクリプトで計算」では、Standard/Professional版のみで使用できる「物理アシスタント」を使用して接地を行っていました。
Basic版ではこの方法は使えないため、接地を行うスクリプトを別途作成しました。
指定の高さ(Y)の位置に設置するスクリプトです。
移動のみ行い、回転は行いません。


import numpy
import math

scene = xshade.scene()

# -----------------------------------------------.
# 形状を囲むワールド座標でのバウンディングボックスを取得.
# -----------------------------------------------.
def calcShapeBoundingBox (shape):
    # ワールド座標でのバウンディングボックスサイズと中心.
    bbSize   = shape.bounding_box_size
    bbCenter = shape.center_position

    bbSizeXH = bbSize[0] * 0.5
    bbSizeYH = bbSize[1] * 0.5
    bbSizeZH = bbSize[2] * 0.5
    bbox = [[bbCenter[0] - bbSizeXH, bbCenter[1] - bbSizeYH, bbCenter[2] - bbSizeZH],
            [bbCenter[0] + bbSizeXH, bbCenter[1] + bbSizeYH, bbCenter[2] + bbSizeZH]]
    return bbox

# -----------------------------------------------.
# 地面のY位置.
groundY = 0.0

# ダイアログボックスの作成と表示.
dlg = xshade.create_dialog_with_uuid('85fa349c-9be6-467e-a014-15b81b9e05e5')
groundY_id = dlg.append_float('地面(Y)の高さ', 'mm')
dlg.set_value(groundY_id, 0.0)
dlg.set_default_value(groundY_id, 0.0)
dlg.append_default_button()

if dlg.ask("選択形状を接地"):
    groundY = dlg.get_value(groundY_id)
    shapesList = []
    for shape in scene.active_shapes:
        shapesList.append(shape)

    for shape in shapesList:
        # 選択形状のバウンディングボックスを取得.
        bbox = calcShapeBoundingBox(shape)
        if bbox == None:
            continue
        wA = numpy.array([(bbox[0][0] + bbox[1][0]) * 0.5, bbox[0][1], (bbox[0][2] + bbox[1][2]) * 0.5, 1.0])
        bboxMinY = bbox[0][1]

        if bboxMinY > groundY:
            wlMat = numpy.matrix(shape.world_to_local_matrix)
            wB = numpy.array([(bbox[0][0] + bbox[1][0]) * 0.5, groundY, (bbox[0][2] + bbox[1][2]) * 0.5, 1.0])

            # wA/wBをローカル座標に変換.
            retA = numpy.dot(wA, wlMat)
            retB = numpy.dot(wB, wlMat)
            lA = [retA[0,0], retA[0,1], retA[0,2]]
            lB = [retB[0,0], retB[0,1], retB[0,2]]

            # ローカル座標で、groundYに接する位置に移動.
            shape.move_object(((1,0,0,0), (0,1,0,0), (0,0,1,0), (lB[0] - lA[0], lB[1] - lA[1], lB[2] - lA[2], 1.0)))

使い方

接地の高さをあらかじめ調べておきます。
以下の画像の場合はテーブルの上に設置します。
接地の高さはY=415としました。

ブラウザで接地したい形状を選択します。

パートの場合は、パート全体で移動対象になります。
リンクの使用も可能です。
上記のスクリプトをスクリプトウィンドウにコピー&ペーストし、実行します。
「選択形状を接地」ダイアログボックスで「地面(Y)の高さ」を415と入力してOKボタンを押します。

以下のように接地されました。

この記事のURLとタイトルをコピーする
Translate »