[Bf-python] re-ordering collection items

Tamito KAJIYAMA rd6t-kjym at asahi-net.or.jp
Thu May 15 02:35:36 CEST 2014


I don't think adding a sort function to RNA is the way to go. In the
case of mesh modifiers, the right order of modifiers is defined by
artists. Sorting the modifiers by some key won't make sense.

My initial question starting this thread was motivated by an API design
question of my own. Recently I added a .new() and .remove() methods to
collection types in the Freestyle Python API, just in line with the data
creation/removal convention of the Blender Python API. Some of the
Freestyle collection types also need a way to re-order items (there are
operators to do so). Then I came to the point where I had to ask myself
if I should add a .move() method to individual collection types, or if I
should consider relying on a generic way (I was not sure if such a way
exists).

To me supporting regular sequence assignments
(bpy_prop_collection[index] = value) seems sufficient.  The question is 
at which level that should be implemented.

-- 
KAJIYAMA, Tamito <rd6t-kjym at asahi-net.or.jp>


On 15/05/2014 09:09, Campbell Barton wrote:
> We could add a python `sort` function to rna, nothing inherently hard
> about that,
>
> But its a fair bit of grunt work.
>
> - Modifiers cant _always_ be re-ordered, so rna sort would need to be
> able to raise exceptions into Python (most likely using reports).
> - Each different list type would need its own sort function. (though
> we could have a generic one for arrays and ListBase which could be
> used in most cases).
> - Should support Python like operations key=lambda, compare function,
> reverse=bool. etc.
>
>
> On Wed, May 14, 2014 at 5:11 PM, CoDEmanX <codemanx at gmx.de> wrote:
>> Some support re-ordering by re-assignment, just like regular python lists:
>>
>> m = Object.materials # assuming there are only two!
>> m[0], m[1] = m[1], m[0]
>>
>> The Modifier stack does not support this. There is no other way to
>> re-order modifiers than to use operators.
>>
>> CollectionProperty() creates objects from <class
>> 'bpy_prop_collection_idprop'>, which comes with a handy method move().
>> You give it the index of which element you want to move, and another
>> index for the target position to swap these two elements.
>>
>>
>> Am 14.05.2014 04:02, schrieb Tamito KAJIYAMA:
>>> Hi,
>>>
>>> Is there a general way to re-order items of bpy_prop_collection
>>> in Python? For example, how can we swap two modifiers in
>>> bpy.context.object.modifiers? Since the collection is read-only,
>>> the general Python sequence protocol seems not to work.



More information about the Bf-python mailing list