Hello dussla,
Please check, if the attached script works as expected.
It should create new markers, corresponding to 3D vertices of all the shapes in the active chunk.
Markers will be grouped to the marker groups named according to the original shape type and label.
import Metashape
def flatten_list(input_list):
if not input_list:
return []
output_list = []
for i in input_list:
if not isinstance(i, list):
output_list.append(i)
else:
output_list += flatten_list(i)
return output_list
def shape_vertices_to_markers():
chunk = Metashape.app.document.chunk
if not chunk:
print("Empty project, script aborted.")
return 0
if not chunk.shapes:
print("No shapes in the active chunk, script aborted")
return 0
shapes_crs = chunk.shapes.crs
crs = chunk.crs
T = chunk.transform.matrix
shapes = list()
num_ignored = 0
num_processed = 0
num_markers = 0
for shape in chunk.shapes:
if shape.is_attached:
shapes.append(shape)
continue
elif shape.geometry.is_3d:
shapes.append(shape)
continue
num_ignored += 1
for shape in shapes:
if shape.is_attached:
markers = shape.geometry.coordinates
if not len(markers):
continue
num_ignored += 1
new_group = chunk.addMarkerGroup()
new_group.label = str(shape.geometry.type).rsplit(".",1)[1][:-4] + "_" + str(shape.key) + " (" + shape.label +")"
markers = set(flatten_list(list(markers)))
markers = [marker for marker in list(chunk.markers) if marker.key in list(markers)]
n = 1
for marker in markers:
new_marker = chunk.addMarker()
new_marker.label = new_group.label + "_" + str(n)
new_marker.group = new_group
new_marker.reference.location = crs.project(T.mulp(marker.position))
new_marker.reference.enabled = True
n += 1
num_markers += n - 1
num_processed += 1
else:
vertices = shape.geometry.coordinates
markers = flatten_list(vertices)
markers = [crs.project(shapes_crs.unproject(marker)) for marker in list(markers)]
if not len(markers):
continue
num_ignored += 1
if (len(markers) > 1) and (markers[0] - markers[-1]).norm() == 0:
markers.pop(-1)
new_group = chunk.addMarkerGroup()
new_group.label = str(shape.geometry.type).rsplit(".",1)[1][:-4] + "_" + str(shape.key) + " (" + shape.label +")"
n = 1
for marker in markers:
new_marker = chunk.addMarker()
new_marker.label = new_group.label + "_" + str(n)
new_marker.group = new_group
new_marker.reference.location = marker
new_marker.reference.enabled = True
n += 1
num_markers += n - 1
num_processed += 1
print("Script finished.")
print("{:d} shapes processed, {:d} markers created, {:d} shapes ignored".format(num_processed, num_markers, num_ignored))
return 1
Metashape.app.addMenuItem("Custom menu/Shape vertices to markers", shape_vertices_to_markers)