Forum

Author Topic: Script for filtering mesh via gradual selection, connected component size option  (Read 5805 times)

jazvec

  • Newbie
  • *
  • Posts: 13
    • View Profile
hello,

to automate point cloud or mesh filtering, I'd like to use scripts as much as possible.
for filtering point cloud, I'm using this script:

import PhotoScan

for chunk in PhotoScan.app.document.chunks:
    f = PhotoScan.PointCloud.Filter()
    f.init(chunk,PhotoScan.PointCloud.Filter.ImageCount)
    f.removePoints(2)

what code should be used for filtering mesh with Gradual Selection > Connected Component Size option?
in other words, I'd like to translate these settings into script:
http://ft.trillian.im/efa6257197fce847ff12c38f511516cb45fa16b0/6Wl85tO3ypy2MHkTJsx3w7zRdiyj6.jpg

thank you.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Hello jazvec,

Currently there's a method that allows to remove connected components that have less then given number of polygons:
Code: [Select]
chunk.model.removeComponents(N)where N is integer number.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Yoann Courtois

  • Sr. Member
  • ****
  • Posts: 316
  • Engineer in Geodesy, Cartography and Surveying
    • View Profile
Hi !

Is there nothing more now ?
Indeed, with GUI, I remove all connected component size but the main one (threshold = 99%)
But, as my model can contain 10 000 or 10 millon faces, it's quite difficult to define a threshold as a number of faces for python scripting

Regards
--
Yoann COURTOIS
R&D Engineer in photogrammetric process and mobile application
Lyon, FRANCE
--

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Sorry for bringing this old topic up but I too want to know about this.

I've just made a script for removeComponents but I want a threshold = 99% in the script, however after researching as much as I can I just can't seem to find anything that is equivalent.

Is there a way to do this in Phython by any chance???

PolarNick

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Hi, maybe this gradual_selection_mesh.py script can help you?

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Hi, maybe this gradual_selection_mesh.py script can help you?

Thanks for that!

Although that looks incredibly complicated, I've managed to use the
Code: [Select]
removeComponents set to a certain number that's close to my mesh polycount to do the job (although I still prefer to Level/Percentage the input).

Although I am having problem doing this for multiple chunks in the Workspace.  :(
How do I get this to select each chunk to be active so that the script applies the actions?

PolarNick

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Quote
Although I am having problem doing this for multiple chunks in the Workspace.  :(
How do I get this to select each chunk to be active so that the script applies the actions?

You can use for-loop over all chunks (like here). And after that use chunk.model like this:


...
        stats = chunk.model.statistics()
...
        chunk.model.removeComponents(faces_threshold)
...

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Once again, thanks for that.

However I was hoping if there was a way to specifically name a chunk, make that active, and then apply all the actions to it?

PolarNick

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Yes, that's possible too :)

This is example of how to check all chunks' labels and then, if label equal to desired name - make it active, so that all Metashape.app.document.chunk accesses were addressed to it:

Code: [Select]
for chunk in Metashape.app.document.chunks:
    if chunk.label == "My Custom Chunk Name":
        Metashape.app.document.chunk = chunk

Probably you meant to name a chunk from GUI - but for the sake of completeness - this is how to change the currently active chunk name from Python:

Code: [Select]
Metashape.app.document.chunk.label = "My Custom Chunk Name"

P.S. script was adapted from https://www.agisoft.com/forum/index.php?topic=7815.0 (googled with "metashape python make chunk active")

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Yes, that's possible too :)

This is example of how to check all chunks' labels and then, if label equal to desired name - make it active, so that all Metashape.app.document.chunk accesses were addressed to it:

Code: [Select]
for chunk in Metashape.app.document.chunks:
    if chunk.label == "My Custom Chunk Name":
        Metashape.app.document.chunk = chunk

Probably you meant to name a chunk from GUI - but for the sake of completeness - this is how to change the currently active chunk name from Python:

Code: [Select]
Metashape.app.document.chunk.label = "My Custom Chunk Name"

P.S. script was adapted from https://www.agisoft.com/forum/index.php?topic=7815.0 (googled with "metashape python make chunk active")

Thanks for that, but still incredibly confused how to implement it or what to type to replace words.

At the moment my script looks like:

Code: [Select]
PhotoScan.app.document.chunk.importModel('Path/0.obj')
PhotoScan.app.document.chunk.model.removeComponents(100000)

And yes I am doing this in Phtoscan at the moment but I know it's possible to do the same in Metashape.

PolarNick

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Something like that:

Code: [Select]
for chunk in PhotoScan.app.document.chunks:
    if chunk.label == "My Custom Chunk Name":
        chunk.importModel('Path/0.obj')
        chunk.model.removeComponents(100000)

Or like that (if you want to make chosen chunk to be active):

Code: [Select]
for chunk in PhotoScan.app.document.chunks:
    if chunk.label == "My Custom Chunk Name":
        PhotScan.app.document.chunk = chunk

PhotoScan.app.document.chunk.importModel('Path/0.obj')
PhotoScan.app.document.chunk.model.removeComponents(100000)