I'm writing a script to animate tex characters morphing from one character to another. To make it look smooth, I want to sort the splines in the spline collection within curve data (sorting by key).

This is what I'd like to do, but it doesn't work on the bpy_prop_collection.

spline_collection = obj.data.splines #An instance of bpy_prop_collection
spline_collection.sort(key = lambda x: get_spline_length(x), reverse = True)

If I put the spline objects in a normal python list, this works well, but then I run into issues when I try to put the splines back into a bpy_props_collection instance. (Due to the fact that I can't seem to deepcopy instances of bpy_struct subclasses, which is its own question.)

EDIT: I did what I needed to do by copying the primitive values I cared about to python lists, then sorting and assigning as needed. A bit of a pain, but it worked.


The blender python API is an API over Blender's internal structure. These structures are designed for performance, hence it cannot be influenced directly.

But the developers have made it quite easy, as you can easily create a local list to do more python stuff. But it is not easy to transfer the results back into blender's database.

spline_collection = list(obj.data.splines)
spline_collection.sort(key = lambda x: get_spline_length(x), reverse = True)

Ps same reason why deepcopy does not work as these objects are not fully python. Most objects have a .copy method you can use.

| improve this answer | |
  • $\begingroup$ "The blender python API is an API over Blender's internal structure." $\endgroup$ – Justin Helps Feb 8 '18 at 4:42
  • $\begingroup$ (Disregard the previous comment, which I can't edit.) "These structures are designed for performance" Makes sense. My initial naive thought was that it seems like more work to not use python types if it's written in python, but it looks like Blender is written in C. $\endgroup$ – Justin Helps Feb 8 '18 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.