Accessing pyNode from QtreeWidget

181 views
Skip to first unread message

shapeofmotion

unread,
Sep 22, 2013, 7:39:07 AM9/22/13
to python_in...@googlegroups.com
Hi,
First off thanks for a great forum!

I am using the pyside QTreeWidget to create a basic ui to list which materials and or SG that has a specific V-Ray material ID assigned to it. The widget lists parent items with the shader ID as label and the child items are the materials or SG that have that ID assigned.

I have assigned the name of the material to the data attr so that when the item is clicked the material gets selected in the hypershade. As it works now if I were to rename the material it would no longer work since the widget is pointing to the name and not to the actual object.

I am sort of stuck here, my initial idea was to serialize a dict that was pointing to the pyNode, but the JSON.dumps method is giving me errors that my dict is not JSON serializable.

I am pretty new to pyside and especially the qtreewidget so any tips, ideas or pointers are very welcome!

Thanks, Johan

Justin Israel

unread,
Sep 22, 2013, 4:02:07 PM9/22/13
to python_in...@googlegroups.com

Why does the object need to be serialized to json? Can't you just store your object as data directly?
Also, you could subclass QTreeWidgetItem and store a custom item in the tree.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

shapeofmotion

unread,
Sep 24, 2013, 4:38:25 AM9/24/13
to python_in...@googlegroups.com

As you pointed out the best way is obviously to store the object as a custom data type in the threewidgetitem. I am quite new to pyside and found the documentation (http://srinikom.github.io/pyside-docs/) on the subject a bit hard to grasp. Do you have any good tips on how to do this or can you point me to some information on the subject?

Best regards,
Johan

Justin Israel

unread,
Sep 24, 2013, 5:01:15 AM9/24/13
to python_in...@googlegroups.com
Sure.

from PySide import QtCore, QtGui

# Some random class
class MyCustomType(object):
foo = "foo"
bar = 1
d = {}
l = []
s = set()

# On top of the standard roles that Qt uses such as DisplayRole, TextAlignmentRole, ...
# We can add our own data from anything starting with UserRole and higher
# Qt wont touch roles this high up
OBJECT_ROLE = QtCore.Qt.UserRole + 1

tree = QtGui.QTreeWidget()

# Create a random instance
# And a tree item
anObject = MyCustomType()
i = QtGui.QTreeWidgetItem()

# Set our instance as a custom data role
col = 0
i.setData(col, OBJECT_ROLE, anObject)
# add to tree
tree.addTopLevelItem(i)

# Now fetch it back out again by row, column
item = tree.itemAt(0, col)
print item.data(0, OBJECT_ROLE)
# <__main__.MyCustomType at 0x103f1d090>

Justin Israel

unread,
Sep 24, 2013, 5:37:35 AM9/24/13
to python_in...@googlegroups.com

On Sep 24, 2013, at 9:01 PM, Justin Israel wrote:

# Now fetch it back out again by row, column
item = tree.itemAt(0, col)
print item.data(0, OBJECT_ROLE)
# <__main__.MyCustomType at 0x103f1d090>

Clarification:   item.data(col, OBJECT_ROLE)
Each item represents the data for a row, so you can store different data in different columns.

shapeofmotion

unread,
Sep 24, 2013, 5:38:33 AM9/24/13
to python_in...@googlegroups.com
Super!!
Thank you so much! short, sweet and correct :)

( being quite new to this group should I delete the previous post when I post a reply ? )

Justin Israel

unread,
Sep 24, 2013, 6:17:43 AM9/24/13
to python_in...@googlegroups.com
Quite welcome.
Do you mean the other posts of this same thread? No need. Its a forum and provides a nice context and archive of the conversation :-)

Welcome!
Reply all
Reply to author
Forward
0 new messages