1
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?

空っぽのグループを抽出したい

1
Last updated at Posted at 2025-12-06

データを整理していく過程で、コンバインしたり階層移動させたりすると
空のグループが出てきてしまいます。

まぁ不要なので削除したいのですがー

例えばこんなデータがあったとして

image.png

null1 は空っぽです。
なので削除します。

image.png

そうすると今度は group1 が空っぽになりました。なので削除せねばなりません。

といった具合に、空のグループを削除すると 新たに空のグループが生まれていくので
ツリー単位で空か否かを見る必要があります。

なので、
cmds.listRelatives(node,ad =True,type = "shape")
を使用してその階層下にshapeが格納されているか否かを確認し、
格納されていなければ 空っぽ または 空っぽのツリーの一部 として扱います。

実装

とりあえず 一気に階層内のtransformノードを取得して、全部評価してしまえばいいかなーと

import maya.cmds as cmds

def listEmptyGroups(topNode):
	allNodes = cmds.listRelatives(topNode,ad =True, type = "transform")
	
	emptyNodes = []
	
	for node in allNodes:
		shape = cmds.listRelatives(node,ad =True,type = "shape")
		
		if shape == None:
			emptyNodes.append(node)
			
	return emptyNodes

試してみるとー

listEmptyGroups("group3")
# Result: ['null1', 'group1', 'null2', 'group4']

image.png

うまいこと空っぽのツリーを構成するノードだけ取れてますね

空っぽのノードを削除する処理 にしてもいいのですが、
空っぽだけど
アトリビュートを格納していてコントローラーとしての機能を持っているノード
の場合もあるので、ひとまずリストするだけにしました。

1
0
0

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
1
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?