0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【MAYA】あるインスタンスの親を選択状態にする

Last updated at Posted at 2018-01-03

前の投稿の調査を元にインスタンスを選択して実行すると親インスタンスを選択し直すコードを作ってみたので貼っておきます。(実際はインスタンスは親も子もなくて全て兄弟ぽいのですが、わかりやすく表現しています。)
スクリプトエディタにペーストする用です。シェルフに登録すると楽に使えると思います。
MAYA2018 + MacOS Sierraで動作確認しています。

使い方

  1. ノードを選択状態にします。対象としてはインスタンスメッシュそのもの、またはその親トランスフォームとなります。
  2. Scriptを実行します。
  3. 親インスタンスノードがあれば選択状態になります。なければエラーメッセージが表示されます。
 2018-01-04 0.22.07.png 最初にインスタンスの親トランスフォームが選択されていれば、  2018-01-04 0.22.15.png 親インスタンスの親トランスフォームが選択されます。  2018-01-04 0.22.25.png 最初にインスタンスそのものが選択されていれば、  2018-01-04 0.22.30.png 親インスタンスそのものが選択されます。  2018-01-04 0.23.26.png _自身が親の場合もメッセージが表示されます。_  2018-01-04 1.39.05.png _グループのインスタンス状態などにも対応します。_  2018-01-04 1.34.15.png _インスタンスがない場合のエラー。_

対応していないパターン

選択ノードの直接の子供のうちで1つもインスタンスがない場合。ツリーを再帰的に探索はしません。

クイック実行用コード

import pymel.core as pm


def get_first_instance(node):
    """
    インスタンスの親(=0番目のインスタンス)を得る

    :param node: 親を探すインスタンスノード参照
    :type node: pymel.nodetypes.DagNode
    :return: 見つかった親ノードとエラー情報ストリング
    :rtype: tuple of DagNode and str
    """
    org_node = node
    target_is_children = False
    is_instanced = node.isInstanced()
    if not is_instanced:
        # 選択がインスタンスでない場合は子供を探す
        target_is_children = True
        children = pm.listRelatives(node)
        node = None
        for child_node in children:
            if child_node.isInstanced():
                node = child_node
                break
        if node is None:
            return None, '対象インスタンスが見つかりません。'
    instances = node.getOtherInstances()
    instance_number = node.instanceNumber()
    if instance_number == 0:
        return org_node, 'ノード自身が親です。'
    found = None
    for ins_node in instances:
        if ins_node.instanceNumber() == 0:
            found = ins_node
            break
    if found is not None:
        if target_is_children:
            parents = pm.listRelatives(found, parent=True)
            return parents[0], None
        else:
            return found, None
    return None, '親インスタンスが見つかりません。'


def select_first_instance():
    selected = pm.selected()
    if len(selected) == 0:
        pm.confirmDialog(title='Info', message='ノードを選択して下さい。', button=['Ok'])
        return
    node = selected[0]
    first_node, error_info = get_first_instance(node)
    if error_info is not None:
        pm.confirmDialog(title='Info', message=error_info, button=['Ok'])
    if first_node is not None:
        print first_node
        pm.select(first_node)


select_first_instance()
del select_first_instance
del get_first_instance
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?