MExternalDropCallback does not receive drop

MExternalDropCallback does not receive drop

haggi_master
Advocate Advocate
1,606 Views
11 Replies
Message 1 of 12

MExternalDropCallback does not receive drop

haggi_master
Advocate
Advocate

Hi, I try to use drag and drop behaviour for our fx and shader library windows. The shaderlibrary window is created with pyside2 from within Maya (2019). For the widgets I added the setDragEnabled(True) and I can see that dragging is working. Now I found the great MExternalDropCallback in Maya. If I simply use the proviede python example, all I get are these lines:

External Drop: doDrop = 0, controlName = MainPane|viewPanes|modelPanel4|modelPanel4|modelEditorTabLayout|modelPanel4, LMB
External Drop: doDrop = 0, controlName = MainPane|viewPanes|modelPanel4|modelPanel4|, LMB

 

The docs mention that doDrop==0 means that it is only checked if a drop is valid. But if I release the mousebutton, I expect some message with doDrop=1 but nothing happens at all if I release the mousebutton. Any idea what's wrong?

 

0 Likes
Accepted solutions (1)
1,607 Views
11 Replies
Replies (11)
Message 2 of 12

haggi_master
Advocate
Advocate

If I drag and drop a file from Windows explorer or a website into the Maya window, everything works and I get a doDrop=1 message. But not from my own pyside window.

0 Likes
Message 3 of 12

haggi_master
Advocate
Advocate

I was able to add mime data to my drag object in python and I can see it in the line printed by the ExternalDropCallback: External Drop: doDrop = 0, controlName = MainPane|viewPanes|modelPanel4|modelPanel4|modelEditorTabLayout|modelPanel4, LMB, text = Dies ist ein ganz dufter Drop Text. Hihi.

 

But still no drop action.

0 Likes
Message 4 of 12

olarn
Advocate
Advocate

Are your pyside widget trapping drop or mouse event like using event.setAcceptDrops or event.accept?

Was it a child of maya main window?

0 Likes
Message 5 of 12

haggi_master
Advocate
Advocate

No, I do not use any mouse button events or drop events at all in my script. I'll try to write a very simple example to show the problem.

0 Likes
Message 6 of 12

haggi_master
Advocate
Advocate

This is a minimal example:

 

import PySide2.QtWidgets as QtWidgets


class MyWidget(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent=parent)
        
        self.button1 = QtWidgets.QLabel("DragMeA")
        self.button2 = QtWidgets.QLabel("DragMeB")
        self.button3 = QtWidgets.QLabel("DragMeC")
        self.setColumnCount(1)
        self.setRowCount(3)
        self.horizontalHeader().hide()
        self.verticalHeader().hide()
        self.setCellWidget(0, 0, self.button1)
        self.setCellWidget(1, 0, self.button2)
        self.setCellWidget(2, 0, self.button3)
        self.setDragEnabled(True)

mw = MyWidget()
mw.show()

And the external drop code comes from here: MExternalDrop 

0 Likes
Message 7 of 12

olarn
Advocate
Advocate

Not a solution but I observed that doDrop=1 only happens with file from explorer browser and image dragged over from chrome web page.

What does not work, so far:

  • plain text dragged over from chrome and IE.
  • images from IE
  • image and objects from photoshop.
  • plaintext from Maya's console output
0 Likes
Message 8 of 12

haggi_master
Advocate
Advocate
Accepted solution

Okay, I think I found the solution just in case anyone has the same problem. If doDrop == 0 the MExternalDropCallback checks if the drop is valid. Of course we have then give feedback that the drop is valid. If we simply return OpenMayaUI.MExternalDropCallback.kMayaDefault then Maya decides if the drop is valid and says no if it does not recognize it. But if we return OpenMayaUI.MExternalDropCallback.kNoMayaDefaultAndAccept, then Maya does nothing and we signal that the drop is a good one and we get a doDrop == 1 if we release the mousebutton.

Message 9 of 12

devanjm
Explorer
Explorer

Hi,

 

I can't get this to work, even printing text like you were able to achieve.  Can you post an example of how you are attaching the data you want recognized by the ExternalDropData?  I am doing it like this:

 

class TestListView(QtWidgets.QListWidget):
    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)

    def dragLeaveEvent(self, event):

        drag = QtGui.QDrag(self)
        mimeData = QtCore.QMimeData()
        url = QtCore.QUrl.fromLocalFile(self.currentItem().text())
        mimeData.setUrls([url])
        mimeData.setText(self.currentItem().text())
        print mimeData.text()
        print mimeData.urls()
        drag.setMimeData(mimeData)
        drag.start(QtCore.Qt.CopyAction)

 

Where the item text is just a maya file on my local drive I am attempting to have maya recognize when I drop.

0 Likes
Message 10 of 12

haggi_master
Advocate
Advocate

Sorry, I don't have the code any more available. Do your drop callback behaves correctly? It should be called twice, one with doDrop==0 and one with doDrop==1.

Message 11 of 12

devanjm
Explorer
Explorer

Thanks for the reply, I know it's months later.

 

Yes I see both prints, doDrop=0 when initially dragging into maya viewport, and doDrop=1 when the mouse button is released.  I also see the text and url data when dragging in a file from file exlplorer, but not with my Qt object with mimeData.

0 Likes
Message 12 of 12

joelP74AW
Observer
Observer

I've found this works with the MExternalDrop script in terms of passing text over:

class ListWidget(QtWidgets.QListWidget):
_drag_info = []
def __init__(self, type, parent=None):
super(ListWidget, self).__init__(parent)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.setSpacing(4)
self.setUniformItemSizes(True)
self.setFlow(QtWidgets.QListView.LeftToRight)
self.setResizeMode(QtWidgets.QListView.Adjust)
self.setWrapping(True)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.setStyleSheet("QListWidget{background-color: rgb(28, 28, 28);}")

def startDrag(self, actions):
drag = QtGui.QDrag(self)
mimeData = self.model().mimeData(self.selectedIndexes())
mimeData.setText("text to copy")
drag.setMimeData(mimeData)
drag.exec_(QtCore.Qt.DropAction)
super(ListWidget, self).startDrag(actions)

 

0 Likes