Forum

Author Topic: shape vertex to marker conversion script  (Read 1451 times)

dussla

  • Newbie
  • *
  • Posts: 12
    • View Profile
shape vertex to marker conversion script
« on: January 08, 2022, 08:23:37 AM »
Hi friend
I have a sone problem
I am seeking python
"shape vertex  to marker conversion script"

Pls  Can you help  this problem
« Last Edit: January 08, 2022, 07:43:37 PM by dussla »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: shape vertex to marker conversion script
« Reply #1 on: January 08, 2022, 08:54:55 PM »
Hello dussla,

Do you need just to get the newly created markers, which correspond to the 3D locations of all the vertices of the shapes in the project?
Best regards,
Alexey Pasumansky,
Agisoft LLC

dussla

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: shape vertex to marker conversion script
« Reply #2 on: January 08, 2022, 11:34:29 PM »

Yes  right  ~~~~~

Really   thank you for reply....

dussla

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: shape vertex to marker conversion script
« Reply #3 on: January 22, 2022, 02:08:40 PM »
Hello dussla,

Do you need just to get the newly created markers, which correspond to the 3D locations of all the vertices of the shapes in the project?


Hi alexey
Pls  i am wating your good answer

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: shape vertex to marker conversion script
« Reply #4 on: January 29, 2022, 06:54:55 PM »
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.


Code: [Select]
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)
« Last Edit: January 29, 2022, 07:08:37 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC