mayaがエラー・ワーニングを吐く時の対処 7　~fileLoadMonitor~

maya2020にて特定のファイルを open / impotするとこんなエラーがでる

```
// Error: TypeError: file C:\Program Files\Autodesk\Maya2020\Python\lib\site-packages\maya\app\renderSetup\model\fileLoadMonitor.py line 102: 'NoneType' object has no attribute '__getitem__' // 
```

pyなので、とりあえず中身を見てみましょうか。

```
def _getErrorSwitchToRenderLayerIfNeeded():
	# If we have a render layer manager with render layers (not connected to render setup layers)
	# in the scene, then return an error warning the user that the combination of render setup and 
	# legacy render layers is not supported.
	renderLayerManagers = cmds.ls(type="renderLayerManager")
	for renderLayerManager in renderLayerManagers:
		size = cmds.getAttr(renderLayerManager + ".renderLayerId", size=True)
		allRenderLayers = (cmds.listConnections(renderLayerManager + ".renderLayerId[" + str(i) + "]")[0] for i in range(0, size))
		numRenderLayers = len([rsl for rsl in (legacyRenderLayer.renderSetupLayer(rl) for rl in allRenderLayers) if rsl is None])

		# It's ok to import or reference a file that has a render layer manager and one
		# legacy render layer (the default one). If the number of legacy render layers that
		# aren't used for render setup is greater than one, we have an intentional use of
		# legacy render layers and we should flag it.
		if numRenderLayers > 1:
			return kErrorSwitchToRenderLayer
	return None
```

この中でエラーが起きてる様子。

おそらく

    cmds.listConnections(renderLayerManager + ".renderLayerId[" + str(i) + "]")[0]

ここで、Noneが返ってきてエラーになってるんでしょうな。

ちょっと切り出して、エラー起きてるノードとattrを確認

```
renderLayerManagers = cmds.ls(type="renderLayerManager")
for renderLayerManager in renderLayerManagers:
    size = cmds.getAttr(renderLayerManager + ".renderLayerId", size=True)
    for i in range(0, size):
        
        try:
            cmds.listConnections(renderLayerManager + ".renderLayerId[" + str(i) + "]")[0]
        except:
            print renderLayerManager + ".renderLayerId[" + str(i) + "]"
```

importするとシーン内のrenderLayerManagerが残って、importするファイルに付属していた物は破棄されてしまうようなので反応せず。

openの場合はそのままなので、反応あり

    renderLayerManager.renderLayerId[1]
    renderLayerManager.renderLayerId[2]
    renderLayerManager.renderLayerId[3]
    renderLayerManager.renderLayerId[4]
    renderLayerManager.renderLayerId[5]


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/e1d68447-6c5e-9782-e5a5-a61cadb6aef2.png)

なるほど、エラーになりますね。
簡単に対処する用のスクリプトを作成

```
renderLayerManagers = cmds.ls(type="renderLayerManager")
for renderLayerManager in renderLayerManagers:
    indexes = cmds.getAttr(renderLayerManager + ".renderLayerId", mi = True) or []
    for i in indexes:        
        try:
            cmds.listConnections(renderLayerManager + ".renderLayerId[" + str(i) + "]")[0]
        except:
            print renderLayerManager + ".renderLayerId[" + str(i) + "]"
            cmds.removeMultiInstance(renderLayerManager + ".renderLayerId[" + str(i) + "]")

```

cmds.getAttr(renderLayerManager + ".renderLayerId", size=True) だと正確なindexが取れないので
cmds.getAttr(renderLayerManager + ".renderLayerId", mi = True)　に変更


http://me.autodesk.jp/wam/maya/docs/Maya2009/CommandsPython/removeMultiInstance.html


そもそも何で空のindexが増えたのかは追加調査中。