Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

4 REPLIES 4
Reply
Message 1 of 5
am5143
3346 Views, 4 Replies

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

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"
4 REPLIES 4
Message 2 of 5
Kahylan
in reply to: am5143

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?

Message 3 of 5
am5143
in reply to: am5143

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

Message 4 of 5
am5143
in reply to: am5143

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
in reply to: am5143

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:

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report