Filtering values ​​available for selection through the UILayout.prop()

When creating custom properties, the values ​​of which the user can choose from a list, it is often necessary to filter the list of values ​​offered for selection. We can use the poll function to shorten the proposed list of values by removing unwanted elements.

Let’s see, how we can use such filtering for example fot selecting a collection into a custom property.

First, let’s create a custom property for the “Scene” object with a pointer to the collection:

Let’s append a custom panel to the 3D Viewport area UI and place the custom property we created on it:

In the “draw” function, we show our custom scene property “my_collection” on the panel through the UILayout.prop(), specifying in its parameters a link to the scene and the name of the displayed property.

Now the user can select any collection with no restrictions.

We can limit the list of collections available for selection, for example, only to those collections that are marked with a green icon.

Let’s define the filter function that will return True if the collection has a green icon and False for all other collections.

Since we will be calling this function as a handler of a custom property, it must take two parameters:

self – a pointer to the property container, in our case – the Scene;

collection – the current collection for which we need to determine whether to display it or not;

The following condition

determines if the collection has a green “COLOR_04” icon.

Now we need to change the definition of the custom property with a pointer to the collection by assigning our filter function as a poll handler:

Now, when showing the collections list, the filter we assigned is used, and the user can select only “green” collections.

Also, a collection already selected by the user could be removed from the list. To do this, let’s modify our filter function by adding one more condition:

This way we can filter any data lists proposed for user selection to a custom property in the way we need.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments