Automatically find pipeline model endpoints

About the automatic extraction of endpoints as shown in the figure, how to implement it in code, I want to use this endpoint to extract the centerline of VMTK。
I checked the source code of the VMTK-slicer extension package and found that it calls a lot of slicer methods. My intention was to load an STL pipeline with code that automatically gets the endpoints and later extracts the model centerline