LumaPictures / pymel

Python in Maya Done Right
Other
478 stars 130 forks source link

toGivenClass() uses __import__() and passes a parameter that is incompatible with Python3 #447

Closed shunjiAdsk closed 1 year ago

shunjiAdsk commented 2 years ago

This code prints # Error: level must be >= 0 with Python3 (Maya2022).

import pymel.core as pm
pm.getPanel(withFocus=True)
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "C:\Program Files\Autodesk\Maya2022\Python37\lib\site-packages\pymel\core\windows.py", line 3624, in getPanel
#     res = func(res)
#   File "C:\Program Files\Autodesk\Maya2022\Python37\lib\site-packages\pymel\internal\factories.py", line 322, in toGivenClass
#     module = __import__(moduleName, globals(), locals(), [objectName], -1)
#   File "C:\Program Files\Autodesk\Maya2022\Python37\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
#     return original_import(name, *args, **kwargs)
# ValueError: level must be >= 0 #

There is __import__ of toPyType.toGivenClass() in pymel/internal/factories.py

module = __import__(moduleName, globals(), locals(), [objectName], -1)

The negative value (-1) is not compatible with Python3 now. What's New In Python 3.3

The index argument to __import__() now defaults to 0 instead of -1 and no longer support negative values.

This will work with "0" instead of "-1", but I'm not sure this is a reasonable change.

# Modified
module = __import__(moduleName, globals(), locals(), [objectName], 0)
shunjiAdsk commented 2 years ago

This is the same issue noted in #445

windxu88 commented 2 years ago

This also happens with AELoader.load() method in lib/python/site-package/pymel/core/uitypes.py:

    @staticmethod
    def load(modname, classname, nodename):
        mod = __import__(modname, globals(), locals(), [classname], -1)

For details, please see https://forums.autodesk.com/t5/maya-programming/did-maya-2022-nodes-template-change-python/td-p/10494560

martin-chatterjee commented 2 years ago

We just stumbled over this as well while deploying PyMel 1.2.0 for Maya 2022. (→ which is the first Maya version with Python 3 on our side.)

This issue and PR #445 seem to define and deal with the problem we encountered.

I'm not sure if simply calling __import__ with level=0 might not have any other unintended side effects – I haven't looked deep enough into it to have an opinion on this yet...

The "What's new in Python 3.3" document seems to suggest a switch to importlib.import_module() instead.

@chadrik, is there any plan or ETA to look into this and merge a python 3 compatible solution into the next release soon?

I think that PyMel is an essential part of developing for Maya, and it should be fully Python 3 compatible.

Thanks in advance for the feedback, Martin

chadrik commented 1 year ago

This is fixed and will be in the next release, coming soon.

jamesdeschenes commented 1 year ago

When is the next release?