agisoft-llc / metashape-scripts

Python scripts for Metashape (former PhotoScan)
MIT License
385 stars 206 forks source link

Metashape.app.document returns NoneType #7

Closed OneGneissGuy closed 5 years ago

OneGneissGuy commented 5 years ago

Every example that calls Metashape.app.document fails because Metashape.app.document returns NoneType. Expect examples to work as listed in the documentation:

import Metashape
chunk = Metashape.app.document.addChunk()
chunk.addPhotos(["IMG_0001.jpg", "IMG_0002.jpg"])
camera = chunk.cameras[0]
camera.photo.meta["Exif/FocalLength"]
Traceback (most recent call last):

  File "<ipython-input-34-087caf902849>", line 2, in <module>
    chunk = Metashape.app.document.addChunk()

AttributeError: 'NoneType' object has no attribute 'addChunk'

System specs:

PolarNick239 commented 5 years ago

You need to create new document and use it instead of the Metashape.app.document:

import Metashape
doc = Metashape.Document()
chunk = doc.addChunk()
chunk.addPhotos(["IMG_0001.jpg", "IMG_0002.jpg"])
camera = chunk.cameras[0]
camera.photo.meta["Exif/FocalLength"]

This is because Metashape.app.document represents the project visible from GUI, and so it is None in stand-alone python wheel.

And yes, we should update documentation :)

nys09 commented 5 years ago

I am getting the following error after following your direction and defining the following. doc = Metashape.document AttributeError: module 'Metashape.Metashape' has no attribute 'document'

I am using the following code. Please let me know if I am missing something. Also, please correct other mistakes in the code if there are any. import os import Metashape import numpy as np pa='/home/nys09/' pat='/home/nys09/Trial1Beamch_arc/' b=[] c=os.path.join(pa,"arc_imagelist_t1.txt") images = open(c,"r+") a = np.loadtxt(images,dtype=str) for i in range(np.shape(a)[0]): b.append(pat+str(a[i])) doc = Metashape.document chunk = doc.addChunk() chunk.addPhotos(b) camera = chunk.cameras[0] camera.photo.meta["Exif/FocalLength"] chunk.matchPhotos(accuracy=Metashape.HighAccuracy, generic_preselection=True,reference_preselection=False) chunk.alignCameras() doc.save(path = "project.psz", chunks = [doc.chunk])

LinshuH commented 4 years ago

@PolarNick239 I got another issue when use doc = Metashape.Document() When I try to export model and camera position by doc.open('project.psz') doc.exportCameras() doc.exportModel() There is an error, error message is:

doc.exportCameras(path=path) AttributeError: 'Metashape.Metashape.Document' object has no attribute 'exportCameras'

I wonder what's the proper way to do the export?

PolarNick239 commented 4 years ago

As you can see in documentation - exportCameras is a method of Metashape.Chunk class. So you need to use something like doc.chunk.exportCameras().

P.S. Please ask questions about generic python scripting (that is not relevant to scripts from this repository) on forum/Python API.