Hello Stephan,
I have updated the script, so that it creates now a cube mesh around each corner of the bounding box (size of the cube is currently defined in the script body in meters). The cubes are parallel to the bounding box sides.
Separate mesh models are created for each corner and in the end the merged mesh is created.
import Metashape, tempfile
def fake_obj(s):
output ="v {:.5f} -{:.5f} -{:.5f}\nv {:.5f} -{:.5f} {:.5f}\nv {:.5f} {:.5f} -{:.5f}\nv {:.5f} {:.5f} {:.5f}\nv -{:.5f} -{:.5f} -{:.5f}\nv -{:.5f} -{:.5f} {:.5f}\nv -{:.5f} {:.5f} -{:.5f}\nv -{:.5f} {:.5f} {:.5f}\nf 2 6 5\nf 7 8 4\nf 3 4 2\nf 4 8 6\nf 6 8 7\nf 1 5 7\nf 1 2 5\nf 3 7 4\nf 1 3 2\nf 2 4 6\nf 5 6 7\nf 3 1 7\n".format(s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s, s)
return output
def create_cube(chunk, center, side):
s = side / chunk.transform.scale
temp_obj = tempfile.NamedTemporaryFile(delete=False)
output = fake_obj(s)
temp_obj.write(output.encode("utf-8"))
temp_obj.close()
if chunk.model:
chunk.model = None
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"]]]'))
for i in range(8):
vertex = chunk.model.vertices[i]
vertex.coord = chunk.region.rot * Metashape.Vector([s * ((i & 1) - 0.5), 0.5 * s * ((i & 2) - 1), 0.25 * s * ((i & 4) - 2)]) + center
return 1
chunk = Metashape.app.document.chunk
region = chunk.region
SIDE = 10 #meters in georeferenced coordinate system
models = []
for i in range(8): #finding bounding box corners
pos = Metashape.Vector([region.size.x * ((i & 1) - 0.5), 0.5 * region.size.y * ((i & 2) - 1), 0.25 * region.size.z * ((i & 4) - 2)])
pos = region.center + region.rot * pos #coordinates in the internal coordinates
corner = chunk.addMarker()
corner.label = "corner " + str(i+1)
corner.reference.location = chunk.crs.project(chunk.transform.matrix.mulp(pos)) #coordinates in geographic/projected coordinates according to chunk.crs
corner.reference.enabled = True
create_cube(chunk, pos, SIDE)
chunk.model.label = corner.label
models.append(chunk.model)
chunk.model = None
task = Metashape.Tasks.MergeAssets()
task.source_data = Metashape.ModelData
task.assets = models
task.apply(chunk)
chunk.model.label = "Merged cubes"
print("Script finished")