theodox / mGui

Python module for cleaner maya GUI layout syntax
MIT License
123 stars 23 forks source link

best idiom for dealing with existing controls #11

Closed theodox closed 10 years ago

theodox commented 10 years ago

in a couple of places the current code uses this idiom to work with existing controls:

 @classmethod
 def from_existing(cls, widget):
      try:
      _cache = cls.CMD
      def fake_CMD(*args, **kwargs):
            return widget
      cls.CMD = fake_CMD
      return cls(widget, widget)
      finally:
           cls.CMD = _cache

This seems to work, but I'm nervous about it. It won't be thread safe (not a major concern - the gui stuff is thread hell no matter what) and it will need extra work for things like parsing existing menus with submenus....

theodox commented 10 years ago

I've added this to mGui.core:

   @classmethod
   def wrap(cls, control_name, key=None):

       def _spoof_create(*args, **kwargs):
           return control_name

        try:
            cache_CMD = cls.CMD
            cls.CMD = _spoof_create
            key = key or control_name
           return cls(key, control_name)
        finally:
            cls.CMD = cache_CMD

Which makes the idiom part of the base mGui,control