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?

同じ名前のオブジェクトを検出したい

Last updated at Posted at 2025-12-04

オブジェクト名被り問題はまぁよくある話なんですが、

オブジェクトに関する何かを書き出した際にwindowsだとちょっと問題になってしまうことがありまして。

  • Arm_geo
  • arm_geo

例えばこれらのオブジェクトを objファイルなどで書き出して、オブジェクト名をそのままつけたとしたら

  • Arm_geo.obj
  • arm_geo.obj

とそれぞれなるわけですが、
これらはwindows上では同一ファイル名として扱われてしまいます。

なので、 大文字小文字関係なく名前が被ってるノード を検出したいんですわ。

実装

名前被り問題は色々やり方があるのですがー

  • dagノード全取得(フルパス名で)
  • ノード毎にショートネームを取得
  • ショートネームがぶつかってる物を取得

簡単に言えばこんな流れで。
で、最後のショートネームを比較する段階で、

  • ショートネームを全て小文字に変換

という処理を追加したいと思います。

import maya.cmds as cmds
import maya.api.OpenMaya as om2

def getDagNode(target):    
    try:
        sellist = om2.MGlobal.getSelectionListByName(target)
        return sellist.getDagPath(0)
    except:
        return None
        
def getShortName(target):
    if type(getDagNode(target)) != om2.MDagPath:
        return target
    return om2.MFnDagNode(getDagNode(target).node()).name()

def checkCrushName():
    result = []
    
    defaultNodes = cmds.ls(defaultNodes =True)
    allNodes = cmds.ls(dagObjects =True,l=True)
    allNodes = list(set(allNodes) - set(defaultNodes))

    shortNameDict = {}

    for node in allNodes:
        shortName = getShortName(node).lower()

        if shortName not in list(shortNameDict.keys()):
            shortNameDict[shortName] = [node]
        else:
            shortNameDict[shortName].append(node)

    for shortName in list(shortNameDict.keys()):
        if len(shortNameDict[shortName]) > 1:
            result.extend(shortNameDict[shortName])

    return result

ほいではこんな名前被りまくりシーンでテストしてみます。

image.png

checkCrushName()

['|group6|pSphere2|pSphereShape2',
 '|group7|pSphere2|pSphereShape2',
 '|group8|pSphere2|pSphereShape2',
 '|group5|group3|group2|group1|pSphere1',
 '|group4|group3|group2|group1|pSphere1',
 '|group6|pSphere2',
 '|group7|pSphere2',
 '|group8|pSphere2',
 '|group4|group3|group2|group1',
 '|group5|group3|group2|group1',
 '|group5|group3|group2',
 '|group4|group3|group2',
 '|group5|group3|group2|group1|pSphere1|pSphereShape1',
 '|group4|group3|group2|group1|pSphere1|pSphereShape1',
 '|group4|group3',
 '|group5|group3']

あーshapeも取れちゃってますねぇ
まぁshapeも名前被ってると悪さすることあるので、結果オーライということで。

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