theodox / mGui

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

Adding a filtered view class #88

Open bob-white opened 7 years ago

bob-white commented 7 years ago

This creates a view into an ObservableCollection and lets you apply a filter against that view.

Each FilteredView can be bound to a different control, but still reference the same underlying collection. Similar to the KeysView or ValueView on a mapping class.

When the base collection changes, each view is updated. Which allows for code like this:

import random
from mGui import gui, forms, lists, observable
from mGui.bindings import bind

for poly_cmd in (cmds.polySphere, cmds.polyCube, cmds.polyCylinder):
    for _ in xrange(random.randint(0, 10)):
        poly_cmd()

meshes = observable.ViewCollection(*cmds.ls(type='mesh'))

sphere_view = observable.FilteredView(meshes, lambda x: 'Sphere' in x)
cube_view = observable.FilteredView(meshes, lambda x: 'Cube' in x)
cyl_view = observable.FilteredView(meshes, lambda x: 'Cylinder' in x)

with gui.BindingWindow() as win:
    with forms.HorizontalStretchForm():
        spheres = gui.TextScrollList()
        spheres.bind.items < bind() < sphere_view
        cubes = gui.TextScrollList()
        cubes.bind.items < bind() < cube_view
        cyls = gui.TextScrollList()
        cyls.bind.items < bind() < cyl_view

win.show()

I had run into this problem in the past when trying to display a different subset of a collection on separated tabs in a TabLayout, and solved it by swapping the filter on a ViewCollection when the tab was changed.

This on the other hand allows for multiple filtered views to be bound to multiple visible controls simultaneously.