Maya Python: Unbound Method due to Reload()

2019-09-14 08:18发布

问题:

I have two files that import the same object tracking method from a third file. It works something like this

file TrackingMethod

    class Tracker(object):
        def __init__(self,nodeName=None,dag=None,obj=None):
            #Does some open maya stuff to get the dag path
        def fullpath(self):
            return dag.fullpath()

file Shapes #classes that create objects with shape nodes
    import TrackingMethod as trm
    reload(trm)

    class circle(trm.Tracker):
        def __init__(self,nodeName=None,dag=None,obj-None):
            #does some shape related stuff then inits tracker
            trm.Tracker.__init__(self,nodeName=nodeName,dag=dag,obj=obj)

file ShaderNodes #Classes that create shading nodes
    import TrackingMethod as trm
    reload(trm)

    class shaderThingy(trm.Tracker):
        def __init__(self,nodeName=None,dag=None,obj-None):
            #does some shader related stuff then inits tracker
            trm.Tracker.__init__(self,nodeName=nodeName,dag=dag,obj=obj)

Here's the problem. The error occurs at the trm.Tracker.init. If I use both files and I happen to reload() either ShaderNode or Shapes, the methods of the other will no longer recognize they're subclasses of the original TrackingMethod class. By reloading the other class loses it's reference back and I get either:

>>unbound method __init__() must be called with Tracker instance as first argument (got circle instance instead) 

or

>>unbound method __init__() must be called with Tracker instance as first argument (got ShaderThingy instance instead) 

..depending on which gets reloaded. Whichever is the last to get reloaded works, and the previously reloaded gets unbound.

Mind you, I need to reload these scripts to test my changes. I know once the reloads are out of there they'll no longer be unbound, but I need to see my changes as I work.

What do I do?

回答1:

You can try importing TrackingMethods twice, with two names.

In shapes:

import TrackingMethods as trm_shapes


class shape(trm_shapes.Tracker) ...

And in shaders:

import TrackingMethods as trm_shaders

class shader(trm_shaders.Tracker) ...

This should work, as long as nobody outside tries to check whether a shader or shape objects are the instance of a Tracker - it will fail.



回答2:

You probably want to remove the reloads from your submodules and reload them in the logical order implied by the dependencies in the files:

reload(TrackingMethod)
reload(Shapes)
reload(ShaderNodes)

For a small case like this it works, but if things get more complex it's going to be hard to manage.