Forum

Author Topic: How to split mesh ?  (Read 3188 times)

cyrilp

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
How to split mesh ?
« on: January 27, 2022, 08:36:31 PM »
Is there a way to split a mesh the way we can split chunks ?

i tried to use split chunks but it doesn't split the included mesh.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14816
    • View Profile
Re: How to split mesh ?
« Reply #1 on: January 28, 2022, 06:03:20 PM »
Hello cyrilp,

Do you need to have already generated single mesh be split into several cubic blocks?
Best regards,
Alexey Pasumansky,
Agisoft LLC

cyrilp

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: How to split mesh ?
« Reply #2 on: February 07, 2022, 11:47:33 AM »
Hi, yes indeed, i have to split them before generating texture as it's a very big model.

cyrilp

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: How to split mesh ?
« Reply #3 on: February 11, 2022, 08:46:11 PM »
?

cyrilp

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: How to split mesh ?
« Reply #4 on: February 16, 2022, 01:23:38 PM »
Hi, i really need your answer, thanks

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14816
    • View Profile
Re: How to split mesh ?
« Reply #5 on: February 25, 2022, 06:40:30 PM »
Hello cyrilp,

Please check this script. It splits the active (default) mesh model in the active chunk by NxN grid (N will be asked on script start) in XY plane (as if you are looking from Top on the mesh).

Code: [Select]
import Metashape

def split_mesh():
doc = Metashape.app.document
chunk = doc.chunk
if not chunk:
print("Empty project, script aborted")
return 0
if not chunk.model:
print("No active mesh model in chunk, script aborted")
return 0
step = Metashape.app.getInt("Please input number of tiles per side:", 5)
if step <= 0:
print("Wrong number! Value should be positive. Script aborted.")
return 0
print("Script started...")

T = chunk.transform.matrix
crs = chunk.crs
if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
chunk.shapes.crs = crs

tiles = chunk.shapes.addGroup()
tiles.label = "Mesh split grid ({:d}x{:d})".format(step, step)

###creating grid
region = chunk.region
corners = list()
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
point = crs.project(T.mulp(pos))
corners.append(point)

x_min = min([point.x for point in corners])
x_max = max([point.x for point in corners])
y_min = min([point.y for point in corners])
y_max = max([point.y for point in corners])

x = x_min
y = y_min
x_step = (x_max - x_min) / step
y_step = (y_max - y_min) / step

shape_grid = list()
i = 1
j = 1
while x < x_max:
y = y_min
j = 1
while y < y_max:
shape = chunk.shapes.addShape()
shape.geometry.type = Metashape.Geometry.Type.PolygonType
shape.geometry = Metashape.Geometry.Polygon([Metashape.Vector([x, y]), Metashape.Vector([x+x_step, y]), Metashape.Vector([x+x_step, y+y_step]), Metashape.Vector([x, y+y_step])])
shape.label = str(i) + "/" + str(j)
shape.group = tiles
shape_grid.append(shape)
y += y_step
j += 1
x += x_step
i += 1

###splitting mesh
for shape in chunk.shapes:
shape.boundary_type = Metashape.Shape.BoundaryType.NoBoundary
model = chunk.model
for shape in shape_grid:
shape.boundary_type = Metashape.Shape.BoundaryType.OuterBoundary
task = Metashape.Tasks.DuplicateAsset()
task.clip_to_boundary = True
task.asset_type = Metashape.DataSource.ModelData
task.asset_key = model.key
task.apply(chunk)
chunk.model.label = "Tile " + shape.label
shape.boundary_type = Metashape.Shape.BoundaryType.NoBoundary
chunk.model = model

print("Script finished")
return 1

Metashape.app.addMenuItem("Custom menu/Split mesh in blocks", split_mesh)
Best regards,
Alexey Pasumansky,
Agisoft LLC

ean

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: How to split mesh ?
« Reply #6 on: September 23, 2022, 10:19:37 PM »
Hey Alexey,

I stumbled on this thread with a Google search. Your script helped me, thanks. After I run it, I get all the Tiles as expected under my chunk. HOw can I export all those to Alembic instead of going through and exporting manually one by one? I couldn't find an obvious way in the batch window.

Thank you
Ean

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14816
    • View Profile
Re: How to split mesh ?
« Reply #7 on: September 26, 2022, 05:35:26 PM »
Hello Ean,

To export multiple assets of the same type (like mesh models) from the single chunk you need to use Python scripting as well, if you want to avoid manual work.

Code: [Select]
for model in chunk.models:
   chunk.model = model
   path = output_folder + "\\" + str(model.key) + "_model.abc"
   chunk.exportModel(path, format = Metashape.ModelFormatABC)
Best regards,
Alexey Pasumansky,
Agisoft LLC