LoginSignup
0

More than 5 years have passed since last update.

【MAYA】選択されたオブジェクトの座標値とピボット値を持ったグループを作る

Posted at

MAYAでの編集時に、グループ化した時にオブジェクトの持っていた座標値とピボット値をグループ側に引き継ぎたかったのだけど、デフォルトでそんな機能が見当たらなかった(あったらいいな。。)ので作りました。

他の人が使って便利かどうか知りませんが、せっかくなので投稿しておきます。

※ 座標値とピボット値は最後に選択したオブジェクトのものが使われます。

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
import pymel.core as pm


def hryk_group(last_selection_position_based=True):
    selection = [x for x in pm.selected() if isinstance(x, pm.nodetypes.DagNode)]
    if len(selection) == 0:
        pm.displayError('no selection')
        return
    if last_selection_position_based:
        # use last selection position
        new_pos = selection[-1].translate.get()
    else:
        # use average position
        new_pos = [0, 0, 0]
        for node in selection:
            translate = node.translate.get()
            new_pos[0] += translate[0]
            new_pos[1] += translate[1]
            new_pos[2] += translate[2]
        new_pos = [x / float(len(selection)) for x in new_pos]
    grp = pm.group()
    grp.translate.set(new_pos)
    for node in selection:
        translate = node.translate.get()
        translate[0] -= new_pos[0]
        translate[1] -= new_pos[1]
        translate[2] -= new_pos[2]
        node.translate.set(translate)
    # move pivot
    if last_selection_position_based:
        grp.scalePivot.set(selection[-1].scalePivot.get())
        grp.rotatePivot.set(selection[-1].rotatePivot.get())
    else:
        grp.scalePivot.set([0, 0, 0])
        grp.rotatePivot.set([0, 0, 0])


if __name__ == '__main__':
    hryk_group()

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