Discussion:
[Bf-python] re-ordering collection items
Tamito KAJIYAMA
2014-05-14 02:02:27 UTC
Permalink
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.
--
KAJIYAMA, Tamito <rd6t-kjym at asahi-net.or.jp>
CoDEmanX
2014-05-14 07:11:38 UTC
Permalink
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.
Post by 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.
Campbell Barton
2014-05-15 00:09:33 UTC
Permalink
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.
Post by CoDEmanX
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.
Post by 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.
_______________________________________________
Bf-python mailing list
Bf-python at blender.org
http://lists.blender.org/mailman/listinfo/bf-python
--
- Campbell
Tamito KAJIYAMA
2014-05-15 00:35:36 UTC
Permalink
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>
Post by Campbell Barton
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.
Post by CoDEmanX
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.
Post by 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.
Loading...