Hello Tom,
See the example of the script below. Should work if you have one shape of Polygon type selected (should also shave 3D vertices, i.e. shape should be displayed in the Model view):
import Metashape, tempfile
def create_mesh_from_vertices(chunk, vertices, crs):
chunk.model = None
output = ""
for v in vertices:
output += "v {:.5f} {:.5f} {:.5f}\n".format(v.x, v.y, v.z)
for i in range (1, len(vertices)-1):
output += "f 1 {:d} {:d}\n".format(i+1, i+2)
print(output)
temp_obj = tempfile.NamedTemporaryFile(delete=False)
temp_obj.write(output.encode("utf-8"))
temp_obj.close()
print("Temp file saved to " + temp_obj.name)
chunk.importModel(path = temp_obj.name, format = Metashape.ModelFormatOBJ, crs = Metashape.CoordinateSystem('LOCAL_CS["Local Coordinates (m)",LOCAL_DATUM["Local Datum",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]]]'))
chunk.model.transform = None
chunk.model.label = "3D model from shape"
for i in range(len(chunk.model.vertices)):
chunk.model.vertices[i].coord = vertices[i]
print("Created mesh")
return 1
def create_mesh():
print("Script started")
doc = Metashape.app.document
if not doc.chunk:
print("Empty project, script aborted")
return 0
chunk = doc.chunk
if not chunk.shapes:
print("No shapes in the active chunk, script aborted")
return 0
shapes = [s for s in chunk.shapes if s.selected]
if len(shapes) != 1:
print("Only one shape should be selected. Currently selected {:d} shapes. Script aborted.".format(len(shapes)))
return 0
shape = shapes[-1]
if shape.geometry.type != Metashape.Geometry.Type.PolygonType:
print("Polygonal shape should be selected, script aborted.")
return 0
T = chunk.transform.matrix
if chunk.shapes.crs:
crs = chunk.shapes.crs
else:
crs = chunk.crs
vertices = [T.inv().mulp(crs.unproject(v)) for v in shape.geometry.coordinates[0]]
create_mesh_from_vertices(chunk, vertices, crs)
print("Script finished.")
return 1
label = "Custom menu/Create mesh from selected shape"
Metashape.app.addMenuItem(label, create_mesh)
print("To execute this script press {}".format(label))