Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Maya 2022/3: drag/drop Python script isn't reloaded on import

Maya 2022/3: drag/drop Python script isn't reloaded on import

am5143
Explorer Explorer
5,271 Views
4 Replies
Message 1 of 5

Maya 2022/3: drag/drop Python script isn't reloaded on import

am5143
Explorer
Explorer

Hey all,

 

Found that executing a drag/drop Python file using the onMayaDroppedPythonFile function does not reload the module like it did in 2020.  As a result, updating the script and drag/dropping it in the viewport will not run the updated version, but the cached version.  I have to remove the module from sys.modules in order for drag/drop to reflect any changes.  It'd be great if drag/drop automatically reloaded the module on import again.  I saw this in 2022.3 and 2023 (haven't tried 2021).

 

Repro: 

  1. Create a testScript.py with a onMayaDroppedPythonFile function that prints("hi").
  2. Drag/drop into Maya's viewport, and "hi" will print in the Script Editor.
  3. Modify testScript.py and replace "hi" with "bye".
  4. Drag/drop the testScript.py into the viewport and "hi" will print in the Script Editor.  It should print "bye"
0 Likes
5,272 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor

Hi!

 

The relaod funtion works diffrent in python 3, it needs to be imported using importlib. This could be the problem, could you post the exact code you are using, so I could reproduce the problem and verify what is going wrong?

0 Likes
Message 3 of 5

am5143
Explorer
Explorer

Hi!  Thanks for looking into this 🙂  I've attached an example drag/drop script as a .txt file.

 

In case it's helpful, I compared executeDroppedPythonFile.py between Maya 2020 and 2022.  For Maya 2020, I saw it was using imp.load_module, which reloads the module on import.  (C:\Program Files\Autodesk\Maya2020\Python\Lib\site-packages\maya\app\general\executeDroppedPythonFile.py line:66)

 

Maya 2022 is using importlib.import_module.  The Python 3.7 docs mentioned load_module was deprecated since 3.4 and recommended exec_module and create_module.  I've never used either of those, so I don't know if that's useful info, but maybe it can help save some time.  (C:\Program Files\Autodesk\Maya2022\Python37\Lib\site-packages\maya\app\general\executeDroppedPythonFile.py line: 63)

 

Amy

0 Likes
Message 4 of 5

am5143
Explorer
Explorer

Found a workaround for anyone with the same issue.  In the drag/drop script, within the def onMayaDroppedPythonFile, I remove the module from sys.modules.

e.g., del sys.modules[<"module name">]

Message 5 of 5

jing_local
Community Visitor
Community Visitor

I reproduced this issue  easily, and it can be fixed by adding module reload on 64 line in  C:\Program Files\Autodesk\Maya2022\Python37\Lib\site-packages\maya\app\general\executeDroppedPythonFile.py line

    import importlib
    loadedModule = importlib.import_module(theModuleName)
    importlib.reload(loadedModule)
    
    # If we successfully loaded the module, call the dropped function.
    if loadedModule:

 

 

0 Likes