選択形状の階層リストを取得 (スクリプト)

ブラウザで選択されている形状の階層リストを取得したい場合、以下のスクリプトを実行します。

# 階層リストを取得.
# @param[in]  depth        階層の深さ.
# @param[in]  shape        形状.
# @param[in]  targetShape  対象形状.
# @param[out] posA         階層リストが返る.
def search_shape_position(depth, shape, targetShape, posA):
  if shape.ordinal == targetShape.ordinal:
    return True

  ret = False
  index = 0
  if shape.has_son:
    s =  shape.son
    while s.has_bro:
      s = s.bro
      posA.append(index + 1)
      ret = search_shape_position(depth + 1, s, targetShape, posA)
      if ret:
        break
      posA.pop()
      index = index + 1

  return ret

scene = xshade.scene()

# 形状のブラウザ階層の位置を配列で返す.
activeShape = scene.active_shape()
posA = []
search_shape_position(0, scene.shape, activeShape, posA)
print posA

以下の階層構造では「球4」が選択されており、この場合は[2,3,1]が返されます。

また、「scene.get_shape_by_position」を使用することで、指定の階層リストの形状を取得できます。

shape2 = scene.get_shape_by_position(posA)
print shape2.name
この記事のURLとタイトルをコピーする
Translate »