1
$\begingroup$

I'm trying to reorder my layers in my grease pencil object using python. The only way I found to do this in code is to make the layer you want to move active and then to use the following command:

bpy.ops.gpencil.layer_move(type='DOWN/UP')

But this is not practical when working with multiple layers, and takes a while to execute with the code I currently have. And since I know exactly the order in which I want my layers, it would be much better to assign an order id to a layer directly, and not have to deal with an operator. I've been looking for something that looks like gp_layer.layer_order which would do just that. But I haven't found such a thing and was wondering if it existed anywhere, or if anyone knew of a more efficient way to reorder grease pencil layers.

$\endgroup$
1
  • $\begingroup$ like so much of the interface, there is no way to specify a specific order. You just have to use the up/down tools. $\endgroup$ Commented Apr 23, 2022 at 19:54

1 Answer 1

1
$\begingroup$

As Marty Fouts already mentioned in his comment there doesn't seem to be a way to do that directly - you would need to write a function like that yourself.

But you don't need to operate on the active layer, you can access the data structure itself.

This could be done like the following:

import bpy

layer_order = [
    "layer_1",
    "layer_2",
    "layer_3",
    "layer_4",
]

layers = bpy.data.objects['MyPencil'].data.layers
# We need to reverse the layer order because the first layer displayed in the UI is the last index in the collection list.
layer_order.reverse()

for i in range(len(layer_order)):
    current_index = layers.find(layer_order[i])
    difference = abs(i - current_index)
    layer = layers[current_index]
    for j in range(difference):
        # "DOWN" moves the layer UP in terms of index, e.g. index 3 moves "DOWN" to index 2.
        layers.move(layer, "DOWN")

You would need to define the layer order (I used a list of the layer names and used the indices of that list) and be aware that the UI displays the collection of layers in reversed order.

In the pencil accessing part (bpy.data.objects['MyPencil']) you need to put in your pencil name.

Hope this helps!

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .