Forum

Author Topic: Possible to Import Model to specific labelled chunk(s)?  (Read 10196 times)

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Possible to Import Model to specific labelled chunk(s)?
« on: July 30, 2023, 05:50:47 AM »
Hi everyone,

As what the title says, I was wondering if it is possible to import a model to a specific chunk in the Workspace?

This is in a scenario where you do have multiple labelled chunks and you would want to import multiple models but of the same filename as the chunk itself.

Of course this will have to be done via. Scripting.

I have skimmed over many pages on the Python API PDFs and I don't think I can see much relevance or that maybe I am missing something.

Please do let me know if this was or is possible at the very least  :)

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Possible to Import Model to specific labelled chunk(s)?
« Reply #1 on: August 02, 2023, 02:31:32 AM »
Anyone at all may I ask? :(

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15691
    • View Profile
Re: Possible to Import Model to specific labelled chunk(s)?
« Reply #2 on: August 02, 2023, 03:28:41 PM »
Hello 3D_Scan_Fan,

Do you mean that you have a folder with the model files and would like to perform the import only if model filename corresponds to the chunk label in the Workspace?

If so, than what are the formats of model files available in the import folder and whether the chunk labels include the file extension?
Best regards,
Alexey Pasumansky,
Agisoft LLC

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Possible to Import Model to specific labelled chunk(s)?
« Reply #3 on: August 02, 2023, 04:27:39 PM »
Hello 3D_Scan_Fan,

Do you mean that you have a folder with the model files and would like to perform the import only if model filename corresponds to the chunk label in the Workspace?

If so, than what are the formats of model files available in the import folder and whether the chunk labels include the file extension?

That is correct.

The formats of everything is pretty standard, they're all OBJs.
The chunk labels don't contain the file extension (they're labelled numerically).

If this is not possible then an alternative would be, how do you set a (specific) chunk active in the workspace throgh a script perhaps?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15691
    • View Profile
Re: Possible to Import Model to specific labelled chunk(s)?
« Reply #4 on: August 02, 2023, 05:13:14 PM »
Hello 3D_Scan_Fan,

Please check, if the following script works for your needs:

Code: [Select]
import Metashape, os, glob

def get_chunk(label):

global doc
for chunk in doc.chunks:
if chunk.label == label:
return chunk
return None

def batch_import_mesh_to_chunks():

global doc
doc = Metashape.app.document
if not len(doc.chunks):
print("Empty project, script aborted.")
return 0

input_dir = Metashape.app.getExistingDirectory("Specify the folder with OBJ files:")
if not input_dir:
print("Invalid path, script aborted.")
return 0

import_list = [file for file in glob.iglob(input_dir + "\\*.*", recursive = False) if (os.path.isfile(file) and os.path.splitext(file)[1][1:].upper() in ["OBJ"])]

for input_model in import_list:
for chunk in doc.chunks:
if not (chunk.label.lower() == os.path.basename(input_model).rsplit(".",1)[0].lower()):
continue
chunk.model = None
try:
chunk.importModel(path = input_model, format = Metashape.ModelFormatOBJ, crs = chunk.crs)
except:
print("Can't import model. Skipping " + input_model)
print("No chunk found for " + input_model)

print ("Script finished")
return 1

label = "Custom menu/Batch Import Mesh"
Metashape.app.addMenuItem(label, batch_import_mesh_to_chunks)
print("To execute this script press {}".format(label))
Best regards,
Alexey Pasumansky,
Agisoft LLC

3D_Scan_Fan

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Possible to Import Model to specific labelled chunk(s)?
« Reply #5 on: August 02, 2023, 08:52:06 PM »
At the moment my script looks like:

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

How do I get it to select the next chunk or select chunk by name?

And yes I am doing this in Phtoscan at the moment.