Question on setdata

45 views
Skip to first unread message

Bay

unread,
Oct 14, 2013, 3:53:05 AM10/14/13
to python_in...@googlegroups.com
currently I'm doing the PyQT Model View Programming Tutorial by Yasin Uludag. As I'm new to PyQT I'm still unfamilar with how to get information out of pyqt and back into Maya again. Currently I'm doing the Treeview part of the tutorial trying to figure out how to interpret a name change in the menu into an actual rename for the object.This is the setData method for the model class

def setData(self, index, value, role=QtCore.Qt.EditRole):

        if index.isValid():

            if role ==QtCore.Qt.EditRole:

                node = index.internalPointer()
                node.setName(value)


                return True
        return False

and this is the setName method

def name(self):
    return self._name

How do I convert the value in node.setName(value) into something that Maya could read so that I could use it for a cmds.rename? Attached is also the full script in case that information is needed.

Thanks for any suggestions given. 
testtree.py

Justin Israel

unread,
Oct 14, 2013, 5:55:00 AM10/14/13
to python_in...@googlegroups.com
Technicallywhat you have commented out in your Node.setName() could work just fine if you want manage your maya calls directly from the Node objects:

class Node(object):
   ....
   def setName(self, name):
        print self._name, name
        cmds.rename(self._name, name)
        self._name=name

To me, it seems appropriate to do it from the model/items since what you are modeling is the SceneGraph and you have the most context between the model and the data you are representing. The alternative is to emit the signals from your model and handle them in some other slot:

def setData(self, index, value, role=QtCore.Qt.EditRole):
    if index.isValid():

        if role ==QtCore.Qt.EditRole:

            node = index.internalPointer()
            oldName = node.name()
            node.setName(value)

            self.dataChanged.emit(index, index)
            self.nameChanged.emit(node, oldName, value)

            return True
            
    return False


# some other function or method as a slot
def nodeNameChanged(node, oldName, newName):
print node, "name was changed from", oldName, "to", newName
cmds.rename(oldName, newName)

myModel = SceneGraphModel()
myModel.nameChanged.connect(nodeNameChanged)

It might be much cleaner though to go with the first and keep all the SceneGraph <=> Model logic together, so that you can have multiple views and not have to duplicate any logic.


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e2f648f4-8910-4309-8d64-6d31f9c09bfe%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Bay

unread,
Oct 14, 2013, 12:20:44 PM10/14/13
to python_in...@googlegroups.com

Hi, 
      Thanks for the reply Justin, The reason I had that commented out was because I kept getting a long PyQt4_QtCore_QVariant_object_at_0x000000002DCEAC78 name when i tried to rename my object. So I'm guessing that this is much like the Maya API where I have to convert it into something that Python can understand. Is there a way to do this?

     I would much like to do it in a more appropriate way since I'm new to this and just trying to wing a desired function, what would you think would be the most option overall?

     P.S Sorry about emailing this response, didn't realized it wasn't a reply button. 

Thank you
Gann Boon Bay

Bay

unread,
Oct 14, 2013, 12:58:09 PM10/14/13
to python_in...@googlegroups.com
Hi , 

       got it to work! I just converted the QVariant Object into a string (eg: newname.toString). 


Justin Israel

unread,
Oct 14, 2013, 3:01:32 PM10/14/13
to python_in...@googlegroups.com

Sorry my mistake on that. I have been doing so much PySide these days that I forgot about the QVariant return value. It is a C++ hold over to allow them to box different types into one interface.  But it was removed in PySide or if you use PyQt with the SIP api set to version 2.
Glad you worked that one out!

On Oct 15, 2013 5:58 AM, "Bay" <gannbo...@gmail.com> wrote:
Hi , 

       got it to work! I just converted the QVariant Object into a string (eg: newname.toString). 


--
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.
Reply all
Reply to author
Forward
0 new messages