Std_TreeSyncSelection is switched off.
If I test the code by pasting each line in the Python console everything works fine. But running the whole code results in an error:
Traceback (most recent call last):
File "C:/Downloads/test2.py", line 23, in <module>
item = tree.selectedItems()[0]
<class 'IndexError'>: list index out of range
It is this code sequence that causes the problem:
Code: Select all
Gui.Selection.addSelection(grp) # Need to update the tree after this? But how?
item = tree.selectedItems()[0]
Gui.Selection.clearSelection()
Test file attached.
The whole code:
Code: Select all
# https://wiki.freecadweb.org/Macro_ExpandTreeItem
from PySide import QtGui
def expandParents(tree, item):
parent = item.parent()
if parent:
tree.expandItem(parent)
expandParents(tree, parent)
doc = App.ActiveDocument
mw = Gui.getMainWindow()
trees = mw.findChildren(QtGui.QTreeWidget)
tree = trees[0]
obj = App.ActiveDocument.Line003
grp = obj.InList[0]
Gui.Selection.clearSelection()
Gui.Selection.addSelection(grp)
item = tree.selectedItems()[0]
Gui.Selection.clearSelection()
expandParents(tree, item)
Gui.Selection.addSelection(grp)
item = tree.selectedItems()[0]
Gui.Selection.clearSelection()
tree.expandItem(item)
for i in range(item.childCount()):
name = item.child(i).setSelected(True)