Drag and drop detect?

(cgstrive) #1

Hello,

When you drag and drop a file onto blender (from explorer) then it is registered/echoed in CLI : “drop file C:\some_path\some_file”

Is this data somehow accessible/visible with Python (handler or some object containing this information )?

Thank you sincerely!

(cgstrive) #2

I hope someone can still answer the question.

Meanwhile here’s a quick pyQT solution that i hope is of help for others ( feel free to improve ). You can essentially drag whole folder of files in and trigger any operations in blender to deal with them. This example here imports .obj


import bpy, os
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *




class QTWindowNw(QtGui.QDialog):
    def __init__(self):
    
        super(QTWindowNw, self).__init__()

        self.mainLayout = QtGui.QVBoxLayout()
        self.label = QtGui.QLabel("Blender: Drag&Drop")
        self.label.setStyleSheet("padding:2px; padding-left:2 px; color:rgba(255,255,255,55); border: 0px solid white")
        self.label.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed))
        self.label.setMinimumWidth(25)
        self.mainLayout.addWidget(self.label, 1, Qt.Alignment(Qt.AlignLeft | Qt.AlignTop))
        self.mainLayout.setContentsMargins(2,2,2,2)
        self.setFixedSize(128,128)
        self.setLayout(self.mainLayout)
        self.setAcceptDrops(True)
        self.setWindowOpacity(0.75)
        self.dragPosition=QtGui.QCursor.pos()
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.setStyleSheet('background-color:#222; border:1px solid white;  width:200px; height:200px')




    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            self.setStyleSheet('background-color:#222; border:1px solid red;  width:200px; height:200px')
            event.accept()
        else:
            event.ignore()


    def dragLeaveEvent(self, event):
        self.setStyleSheet('background-color:#222; border:1px solid white;  width:200px; height:200px')
        event.accept()


    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()


    def dropEvent(self, event):

        self.setStyleSheet('background-color:#222; border:1px solid white;  width:200px; height:200px')

        for url in event.mimeData().urls():
            xrl=url.toString().replace("file:///", "")
            print(xrl)

            if os.path.exists(xrl):
                # if OBJ, import OBJ
                if ".obj" in xrl:
                    bpy.ops.import_scene.obj(filepath=xrl)


                #elif ANY OTHER FILE...


    
    def mousePressEvent(self, event):
        point = QtGui.QCursor.pos()
        self.dragPosition=point- self.frameGeometry().topLeft()
        event.accept()


         
            
    def mouseMoveEvent(self, event):

        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            lastPos=[self.pos().x(),self.pos().y()]
            event.accept()
                 


    def closeWindow(self):

        global window
        print ("CLOSING")

        self.window=None
        self.close()



class qtDragDrop(bpy.types.Operator):
    bl_idname = "nw.dragdrop"
    bl_label = "QT_DragDrop [import]"


    def modal(self, context, event):
        
        # close on ESC
        if event.type in {'ESC'}:
            print ("CLOSE QT")
            self.window.closeWindow()
            return {'CANCELLED'}


        return {'PASS_THROUGH'}




    def invoke(self, context,event):
        
        self._application = QtGui.QApplication.instance()
        if self._application is None:
            self._application = QtGui.QApplication(['blender'])
        self._eventLoop = QtCore.QEventLoop()
        
        self.window = QTWindowNw()
        self.window.show()


        context.window_manager.modal_handler_add(self)
        
        #window position
        point = QtGui.QCursor.pos()
        self.window.move(point.x()-180, point.y()-40)
        return {'RUNNING_MODAL'}




    
def register():
    bpy.utils.register_class(qtDragDrop)


        
def unregister():
    bpy.utils.unregister_class(qtDragDrop)


if __name__ == "__main__":
    register()

Attachments