Page Menu
Home
Search
Configure Global Search
Log In
Files
F85749
ui_list_custom_sorting.py
Public
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
codemanx
Apr 23 2014, 6:44 PM
Size
3 KB
Subscribers
None
ui_list_custom_sorting.py
View Options
import
bpy
class
MESH_UL_mylist
(
bpy
.
types
.
UIList
):
# Constants (flags)
# Be careful not to shadow FILTER_ITEM (i.e. UIList().bitflag_filter_item)!
# E.g. VGROUP_EMPTY = 1 << 0
# Custom properties, saved with .blend file. E.g.
# use_filter_empty = bpy.props.BoolProperty(name="Filter Empty", default=False, options=set(),
# description="Whether to filter empty vertex groups")
# Called for each drawn item.
def
draw_item
(
self
,
context
,
layout
,
data
,
item
,
icon
,
active_data
,
active_propname
,
index
,
flt_flag
):
self
.
use_filter_show
=
True
if
self
.
layout_type
in
{
'DEFAULT'
,
'COMPACT'
}:
split
=
layout
.
split
(
0.1
)
split
.
label
(
"
%i
."
%
(
index
+
1
))
split
=
split
.
split
(
0.5
)
split
.
prop
(
item
,
"label"
,
text
=
""
,
emboss
=
False
)
split
.
prop
(
item
,
"description"
,
text
=
""
,
emboss
=
False
)
elif
self
.
layout_type
in
{
'GRID'
}:
pass
# Called once to filter/reorder items.
def
filter_items
(
self
,
context
,
data
,
propname
):
col
=
getattr
(
data
,
propname
)
filter_name
=
self
.
filter_name
.
lower
()
flt_flags
=
[
self
.
bitflag_filter_item
if
any
(
filter_name
in
filter_set
for
filter_set
in
(
str
(
i
),
item
.
label
.
lower
(),
item
.
description
.
lower
()
)
)
else
0
for
i
,
item
in
enumerate
(
col
,
1
)
]
if
0
:
#self.use_filter_sort_alpha:
flt_neworder
=
[
x
[
1
]
for
x
in
sorted
(
zip
(
[
x
[
0
]
for
x
in
sorted
(
enumerate
(
col
),
key
=
lambda
x
:
x
[
1
]
.
label
)],
range
(
len
(
col
))
)
)
]
else
:
flt_neworder
=
list
([
1
,
0
,
2
,
3
][::
-
1
])
return
flt_flags
,
flt_neworder
class
MyColl
(
bpy
.
types
.
PropertyGroup
):
#name = bpy.props.StringProperty()
label
=
bpy
.
props
.
StringProperty
()
description
=
bpy
.
props
.
StringProperty
()
class
HelloWorldPanel
(
bpy
.
types
.
Panel
):
"""Creates a Panel in the Object properties window"""
bl_label
=
"Hello World Panel"
bl_idname
=
"OBJECT_PT_hello"
bl_space_type
=
'PROPERTIES'
bl_region_type
=
'WINDOW'
bl_context
=
"scene"
def
draw
(
self
,
context
):
layout
=
self
.
layout
layout
.
template_list
(
"MESH_UL_mylist"
,
""
,
context
.
scene
,
"col"
,
context
.
scene
,
"col_idx"
)
# Demo data
coll_data
=
(
(
"Option 1"
,
"B"
),
(
"Option 2"
,
"C"
),
(
"Some text"
,
"D"
),
(
"Blabla"
,
"A"
)
)
def
register
():
bpy
.
utils
.
register_module
(
__name__
)
bpy
.
types
.
Scene
.
col
=
bpy
.
props
.
CollectionProperty
(
type
=
MyColl
)
bpy
.
types
.
Scene
.
col_idx
=
bpy
.
props
.
IntProperty
(
default
=
0
)
# Add demo data
scene
=
bpy
.
context
.
scene
scene
.
col
.
clear
()
if
1
:
#len(scene.col) < 1:
for
label
,
description
in
coll_data
:
item
=
scene
.
col
.
add
()
item
.
label
=
label
item
.
description
=
description
def
unregister
():
bpy
.
utils
.
unregister_module
(
__name__
)
del
bpy
.
types
.
Scene
.
col
del
bpy
.
types
.
Scene
.
col_idx
if
__name__
==
"__main__"
:
register
()
File Metadata
Details
Attached
Mime Type
text/x-c++
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
97/c3/cc037ef14bfd05be07c6445c0db3
Attached To
T39845: UI List: Custom filter_items() sorting gives unpredictable item order
Event Timeline
Log In to Comment