0
0

maya renderSetupでコレクションの中身にnamespaceを足す2

Last updated at Posted at 2023-12-20

つづき

collectionが全部とれたので、中身をゲットしてネームスペースを足してく

ちょっと手動で編集してこんな感じにしておく
image.png

※余談ですが、collectionのstaticSelectionの中身はstringなので 選択 -> 編集でいじれますが
※renderSetupのGUIへは即時反映されません。
※該当レイヤーを読み直すして(目玉マーク) GUI上でコレクションを選択しなおすと反映されます。

フィルターの種類は色々あるのだけれども今回はとりあえずstaticSelectionだけ対応で。
image.png

雑に実装

from maya.app.renderSetup.model import renderSetup

def getRSCollectionsHierarchy(taregtNode,collections):
    #対象のノードが collectionか否か
    if taregtNode.isCollection():        
        collections.append(taregtNode)
    
    #対象のノードが hasChildren アトリビュートを持つか否か
    if "hasChildren" in dir(taregtNode):
        
        #対象のノードが子ノードを持つか否か
        if taregtNode.hasChildren():
            
            #対象のノードの子ノードをリスト
            chNodes = taregtNode.getChildren()

            #ぐーるぐーる
            for node in chNodes:
                collections = getRSCollectionsHierarchy(node,collections)

    return collections

def getRSCollections(layerName):
    rs = renderSetup.instance()
    layer = rs.getRenderLayer(layerName)
    chNodes  = layer.getChildren()
    collections = []
    
    #layer直下の子ノード
    for taregtNode in layer.getChildren():    
        getRSCollectionsHierarchy(taregtNode,collections)

    #layer直下のグループノード
    for taregtNode in layer.getGroups():    
        getRSCollectionsHierarchy(taregtNode,collections)
    
    return collections

## ココから今回の分
def getRSCollectionMembers(collections):
    collectionDict = {}

    for collection in collections:
        selector = collection.getSelector()        
        staticSelection = selector.staticSelection.asList()
        collectionDict[collection.name()] = staticSelection
    
    return collectionDict

collections = getRSCollections("renderSetupLayer1")
collectionDict = getRSCollectionMembers(collections)
print(collectionDict)

#{'collection1': ['|pCube1'], 'collection3': [], 'collection4': []}

とれた・・・かな
これを加工して戻す感じですかね

若干面倒なのが、ロングネームなところですかね


from maya.app.renderSetup.model import renderSetup

def getRSCollectionsHierarchy(taregtNode,collections):
    #対象のノードが collectionか否か
    if taregtNode.isCollection():
        #名前を取りたいので .name()を一旦ついか
        collections.append(taregtNode)
    
    #対象のノードが hasChildren アトリビュートを持つか否か
    if "hasChildren" in dir(taregtNode):
        
        #対象のノードが子ノードを持つか否か
        if taregtNode.hasChildren():
            
            #対象のノードの子ノードをリスト
            chNodes = taregtNode.getChildren()

            #ぐーるぐーる
            for node in chNodes:
                collections = getRSCollectionsHierarchy(node,collections)

    return collections

def getRSCollections(layerName):
    rs = renderSetup.instance()
    layer = rs.getRenderLayer(layerName)
    chNodes  = layer.getChildren()
    collections = []
    
    #layer直下の子ノード
    for taregtNode in layer.getChildren():    
        getRSCollectionsHierarchy(taregtNode,collections)

    #layer直下のグループノード
    for taregtNode in layer.getGroups():    
        getRSCollectionsHierarchy(taregtNode,collections)
    
    return collections

## ココから今回の分
def getRSCollectionMembers(collections):
    collectionDict = {}

    for collection in collections:
        selector = collection.getSelector()        
        staticSelection = selector.staticSelection.asList()
        collectionDict[collection.name()] = staticSelection
    
    return collectionDict

def addNamespaceCollectionMember(namespace,collectionDict):
    replaceDict = {}

    for collectionName in list(collectionDict.keys()):
        curMembers = collectionDict[collectionName]
        editedMembers = []
        for curLongName in curMembers:        
            longNameList = curLongName.split("|")
            NSAddedlongName = ("|"+namespace + ":").join(longNameList)
            
            if cmds.objExists(NSAddedlongName):
            #登録したいノード名がネームスペースありで存在してる場合
                if NSAddedlongName not in editedMembers:
                    editedMembers.append(NSAddedlongName)
            elif cmds.objExists(curLongName):
            #登録してあるノード名がネームスペース無しで存在してる場合
                if curLongName not in editedMembers:
                    editedMembers.append(curLongName)
        
        replaceDict[collectionName] = editedMembers

    return replaceDict

def setCollectionMember(collections,collectionDict):

    for collection in collections:
        collectionName = collection.name()

        if collectionName not in list(collectionDict.keys()):
            continue

        selector = collection.getSelector()
        selector.staticSelection.set(collectionDict[collectionName])
        selector.staticSelection.asList()

collections = getRSCollections("renderSetupLayer1")
collectionDict = getRSCollectionMembers(collections)
namespace = "hako2"
replaceDict = addNamespaceCollectionMember(namespace,collectionDict)
setCollectionMember(collections,replaceDict)

ビフォアー
image.png

アフター
image.png

制限事項と改修案

今の制限

  • staticSelectionを使用しているcollecitonに限る
  • 読み込み初期staticSelectionの中身にはnamespaceを使用していない

改修で来そうなところ

  • staticSelection以外のフィルターにも対応
  • staticSelectionの中身にネームスペースが既に使われていた場合、ネームスペースを置き換える

などなどなど

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