项目 'layer 7' 已存在于树视图中:在 Maya 中
Item 'layer 7' already exists in the treeview : in Maya
我想将两个具有相同名称的不同项目添加到 TreeView,但它们具有不同的父项。但我收到错误:
Item 'layer 7' already exists in the TreeView
我应该怎么做才能避免这种情况。
我的代码:
import maya.cmds as cmds
import os
window = cmds.window()
layout = cmds.formLayout()
control = cmds.treeView( parent = layout, numberOfButtons = 0, abr = False )
cmds.formLayout(layout,e=True, attachForm=(control,'top', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'left', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'bottom', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'right', 2))
cmds.showWindow( window )
startpath="C:/MyFolder/"
cmds.treeView( control, e=True, addItem = ("layer 1", ""))
cmds.treeView( control, e=True, addItem = ("layer 2", ""))
cmds.treeView( control, e=True, addItem = ("layer 3", ""))
cmds.treeView( control, e=True, addItem = ("layer 4", ""))
cmds.treeView( control, e=True, addItem = ("layer 5", ""))
cmds.treeView( control, e=True, addItem = ("layer 6", ""))
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 3"))
cmds.treeView( control, e=True, addItem = ("layer 9", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 10", "layer 8"))
cmds.treeView( control, e=True, addItem = ("layer 11", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 12", ""))
cmds.treeView( control, e=True, addItem = ("layer 13", "layer 10"))
N。 B: 我试图在这里找到解决办法,但没有成功。
您正在尝试将您的 "layer 7" 分成两个不同的 parents,这就是为什么
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 3"))
===编辑===
我已经 miss-read 你的问题了。所以这是对如何 maya.cmds 包装 Qt QTreeView 的限制。
如果你想在你的图表中有两次第 7 层,我会在 cmds 中这样做:
import maya.cmds as cmds
from collections import defaultdict
treepop = defaultdict(list)
treepop[""]=['layer_1_0',
'layer_2_0',
'layer_3_0',
'layer_4_0',
'layer_5_0',
'layer_6_0',
'layer_12_0']
treepop["layer_2_0"] = ['layer_7_1',
'layer_9_1',
'layer_11_1']
treepop["layer_3_0"] = ['layer_7_2']
treepop["layer_8_0"] = ['layer_10_1']
treepop["layer_10_0"] = ['layer_13_1']
window = cmds.window()
layout = cmds.formLayout()
control = cmds.treeView( parent = layout, numberOfButtons = 0, abr = False )
cmds.formLayout(layout,e=True, attachForm=(control,'top', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'left', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'bottom', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'right', 2))
cmds.showWindow( window )
for k in treepop[""]:
cmds.treeView( control, e=True, addItem=(k, ""))
for k in treepop:
if k == "":
continue
for v in treepop[k]:
cmds.treeView( control, e=True, addItem=(v, k))
因此您可以去除最后一个“_”以获取层的名称。
不然自己包起来QtWidgets.QTreeView或者QTreeWidget,可能更简单
=== 编辑 2 ===
我有一个关于如何使用 qtreewidget 扩展 maya.cmds 的示例:
from PySide2 import QtWidgets
from maya import OpenMayaUI, cmds
from collections import defaultdict
import shiboken2
treepop = defaultdict(list)
treepop[""]=['layer_1',
'layer_2',
'layer_3',
'layer_4',
'layer_5',
'layer_6',
'layer_12']
treepop["layer_2"] = ['layer_7',
'layer_9',
'layer_11']
treepop["layer_3"] = ['layer_7']
treepop["layer_8"] = ['layer_10']
treepop["layer_10"] = ['layer_13']
def mayaToQT(name):
# Maya -> QWidget
ptr = OpenMayaUI.MQtUtil.findControl(name)
if ptr is None: ptr = OpenMayaUI.MQtUtil.findLayout(name)
if ptr is None: ptr = OpenMayaUI.MQtUtil.findMenuItem(name)
if ptr is not None: return shiboken2.wrapInstance(long(ptr),
QtWidgets.QWidget)
cmds.window()
layout = cmds.columnLayout(rowSpacing=10, columnWidth=250)
qwidget= mayaToQT(layout)
qlayout = qwidget.children()[0]
treeview = QtWidgets.QTreeWidget()
treeview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
treeview.setColumnCount(1)
treeview.setAlternatingRowColors(True)
treeview.clear()
qlayout.addWidget(treeview)
items = []
for k in treepop[""]:
root_item = QtWidgets.QTreeWidgetItem()
root_item.setText(0,k)
print(k)
if k in treepop:
for v in treepop[k]:
child_item = QtWidgets.QTreeWidgetItem()
child_item.setText(0,v)
root_item.addChild(child_item)
items.append(root_item)
treeview.addTopLevelItems(items)
cmds.showWindow()
def getSelected():
items = treeview.selectedItems()
for i in items:
print(i.text(0))
我想将两个具有相同名称的不同项目添加到 TreeView,但它们具有不同的父项。但我收到错误:
Item 'layer 7' already exists in the TreeView
我应该怎么做才能避免这种情况。
我的代码:
import maya.cmds as cmds
import os
window = cmds.window()
layout = cmds.formLayout()
control = cmds.treeView( parent = layout, numberOfButtons = 0, abr = False )
cmds.formLayout(layout,e=True, attachForm=(control,'top', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'left', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'bottom', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'right', 2))
cmds.showWindow( window )
startpath="C:/MyFolder/"
cmds.treeView( control, e=True, addItem = ("layer 1", ""))
cmds.treeView( control, e=True, addItem = ("layer 2", ""))
cmds.treeView( control, e=True, addItem = ("layer 3", ""))
cmds.treeView( control, e=True, addItem = ("layer 4", ""))
cmds.treeView( control, e=True, addItem = ("layer 5", ""))
cmds.treeView( control, e=True, addItem = ("layer 6", ""))
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 3"))
cmds.treeView( control, e=True, addItem = ("layer 9", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 10", "layer 8"))
cmds.treeView( control, e=True, addItem = ("layer 11", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 12", ""))
cmds.treeView( control, e=True, addItem = ("layer 13", "layer 10"))
N。 B: 我试图在这里找到解决办法,但没有成功。
您正在尝试将您的 "layer 7" 分成两个不同的 parents,这就是为什么
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 2"))
cmds.treeView( control, e=True, addItem = ("layer 7", "layer 3"))
===编辑===
我已经 miss-read 你的问题了。所以这是对如何 maya.cmds 包装 Qt QTreeView 的限制。 如果你想在你的图表中有两次第 7 层,我会在 cmds 中这样做:
import maya.cmds as cmds
from collections import defaultdict
treepop = defaultdict(list)
treepop[""]=['layer_1_0',
'layer_2_0',
'layer_3_0',
'layer_4_0',
'layer_5_0',
'layer_6_0',
'layer_12_0']
treepop["layer_2_0"] = ['layer_7_1',
'layer_9_1',
'layer_11_1']
treepop["layer_3_0"] = ['layer_7_2']
treepop["layer_8_0"] = ['layer_10_1']
treepop["layer_10_0"] = ['layer_13_1']
window = cmds.window()
layout = cmds.formLayout()
control = cmds.treeView( parent = layout, numberOfButtons = 0, abr = False )
cmds.formLayout(layout,e=True, attachForm=(control,'top', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'left', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'bottom', 2))
cmds.formLayout(layout,e=True, attachForm=(control,'right', 2))
cmds.showWindow( window )
for k in treepop[""]:
cmds.treeView( control, e=True, addItem=(k, ""))
for k in treepop:
if k == "":
continue
for v in treepop[k]:
cmds.treeView( control, e=True, addItem=(v, k))
因此您可以去除最后一个“_”以获取层的名称。
不然自己包起来QtWidgets.QTreeView或者QTreeWidget,可能更简单
=== 编辑 2 ===
我有一个关于如何使用 qtreewidget 扩展 maya.cmds 的示例:
from PySide2 import QtWidgets
from maya import OpenMayaUI, cmds
from collections import defaultdict
import shiboken2
treepop = defaultdict(list)
treepop[""]=['layer_1',
'layer_2',
'layer_3',
'layer_4',
'layer_5',
'layer_6',
'layer_12']
treepop["layer_2"] = ['layer_7',
'layer_9',
'layer_11']
treepop["layer_3"] = ['layer_7']
treepop["layer_8"] = ['layer_10']
treepop["layer_10"] = ['layer_13']
def mayaToQT(name):
# Maya -> QWidget
ptr = OpenMayaUI.MQtUtil.findControl(name)
if ptr is None: ptr = OpenMayaUI.MQtUtil.findLayout(name)
if ptr is None: ptr = OpenMayaUI.MQtUtil.findMenuItem(name)
if ptr is not None: return shiboken2.wrapInstance(long(ptr),
QtWidgets.QWidget)
cmds.window()
layout = cmds.columnLayout(rowSpacing=10, columnWidth=250)
qwidget= mayaToQT(layout)
qlayout = qwidget.children()[0]
treeview = QtWidgets.QTreeWidget()
treeview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
treeview.setColumnCount(1)
treeview.setAlternatingRowColors(True)
treeview.clear()
qlayout.addWidget(treeview)
items = []
for k in treepop[""]:
root_item = QtWidgets.QTreeWidgetItem()
root_item.setText(0,k)
print(k)
if k in treepop:
for v in treepop[k]:
child_item = QtWidgets.QTreeWidgetItem()
child_item.setText(0,v)
root_item.addChild(child_item)
items.append(root_item)
treeview.addTopLevelItems(items)
cmds.showWindow()
def getSelected():
items = treeview.selectedItems()
for i in items:
print(i.text(0))