Delete empty visibility layersΒΆ

This script delete visibility layers (also called geometry layers) that contains no surfaces.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from p3dsdk import *


def delete_layer_if_empty(layer: VisibilityLayer):
    sub_layers = layer.list_child_layers(recursive=False)
    for s in sub_layers:
        delete_layer_if_empty(s)

    have_surfaces = len(layer.list_surfaces()) > 0
    have_sub_layers = len(layer.list_child_layers(recursive=False)) > 0

    # Delete the layer if it contains no surfaces and no sub_layers
    # (they are already deleted if they were empty)
    if not have_surfaces and not have_sub_layers:
        layer.remove(True)


with CurrentP3DSession(fallback_port=33900) as p3d:
    model = p3d.data.list_models()[0]  # Get the first model
    layers = model.list_visibility_layers(recursive=False)
    for layer in layers:
        delete_layer_if_empty(layer)

Download example

delete_layer_if_empty() is a post-order recursive function. It does the following:

  • Recurse into direct sub layers of the layer.

  • Delete the layer if it contains no surfaces and no sub layers (i.e. it is empty). The empty child layers are already removed by the recursive step.