Forum

Author Topic: Document Modification State Not Updating After Save in Metashape Pyt  (Read 350 times)

Rafes

  • Newbie
  • *
  • Posts: 8
    • View Profile
(Note: I had perplexity generate this bug report as I was using it to debug and find the issue. I did not reproduce this in simple isolation as implied here. This is just to illustrate the point. I did see in my debugging session that the modified state did not become `True` again once the doc was saved, and I also confirmed the workaround to use the chunks argument. I am leaving all of this information here in case it is helpful)

Metashape Version: 2.1.1

Description:
When using the Metashape Python API, the document modification state is not being updated correctly after saving the document. This leads to subsequent changes not being saved in following save operations unless a specific workaround is used.

Steps to Reproduce:
1. Open a new or existing document in Metashape using the Python API.
2. Make initial changes to the document (e.g., add photos, align cameras).
3. Save the document using `doc.save(path)`.
4. Make additional changes.
5. Check the modification state using `doc.modified`. It is still False.
6. Save the document again using `doc.save(path)`.
7. Open the saved document to verify changes.

Expected Behavior:
After making changes in step 4, `doc.modified` should return `True`. The second save operation should include all changes made after the first save.

Actual Behavior:
After making changes in step 4, `doc.modified` remain `False`. The second save operation does not include changes made after the first save, particularly changes to camera reference data.

Workaround:
The issue can be resolved by explicitly specifying the chunks to save using the `chunks` parameter in the `save` method:

```python
doc.save(path, chunks=[doc.chunk])

This workaround ensures that all changes to the specified chunks are saved, even if the modification state is not correctly updated.

Additional Observations:
The exportReference function correctly exports the changed reference data to a CSV file, confirming that the changes are made in memory.
Even changing camera labels after the first save does not trigger the modified state or get saved in subsequent save operations without the workaround.
If the initial save operation (step 3) is omitted, all changes are saved correctly without needing the workaround.

Code Example:
Code: (python) [Select]
import Metashape

doc = Metashape.Document()
chunk = doc.addChunk()

# Initial setup and changes
# ... (add photos, align cameras, etc.)

doc.save("project.psx")  # First save

# Make changes to reference data
for camera in chunk.cameras:
    camera.reference.location = [1, 2, 3]
    camera.label = camera.label + "_NEW"

print("Modified state:", doc.modified)  # Prints False

# Workaround: Save with explicit chunks parameter
doc.save("project.psx", chunks=[chunk])  # Second save

# Open project.psx manually to verify - changes are now saved



Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14965
    • View Profile
Re: Document Modification State Not Updating After Save in Metashape Pyt
« Reply #1 on: June 25, 2024, 05:38:03 PM »
Hello Rafes,

When working with PSX project format the recommended way of saving the project is the following:
- save the project using doc.save(path) in the very beginning before any project-related variables have been assigned (like chunk etc.),
- next time when the project should be saved, use doc.save() without any argument.

The reason is that using doc.save(path) works similar to Save As command called from the GUI and in this case all the data is being re-saved completely and not only modified assets, then the project is re-opened - therefore all the project-related variables have to be re-assigned.
Using doc.save(path) approach may also lead to big number of junk files in the project.files directory that will increase the size of the folder, but are not actually linked to the project.

Can you please check, if using the approach mentioned above allows to avoid the unexpected behavior. But not that some minor changes do not trigger modified status of the project - maybe you have applied only one of those when the problem has been observed.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Rafes

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Document Modification State Not Updating After Save in Metashape Pyt
« Reply #2 on: June 25, 2024, 11:02:48 PM »
Thank you for the reply.

In the current code, each save is to a different file path, not the same file path. While I agree adding named chunks could be a nice way to debug multiple steps within the same file, in this case I can't change the codebase.

I tried setting both reference location and rotation attributes as well as the camera labels and the modified state did not go back to True.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14965
    • View Profile
Re: Document Modification State Not Updating After Save in Metashape Pyt
« Reply #3 on: June 26, 2024, 04:56:01 PM »
Hello Rafes,

You need to re-assign chunk variable after each doc.save(path) call then, otherwise it will be pointing to the chunk from the previous project, therefore new project (doc variable) will have modified=False status.

So in your test code I would suggest the following:
Code: [Select]

import Metashape

doc = Metashape.Document()
chunk = doc.addChunk()

# Initial setup and changes
# ... (add photos, align cameras, etc.)

doc.save("project.psx")  # First save

chunk = doc.chunks[-1] #last chunk in the project

# Make changes to reference data
for camera in chunk.cameras:
    camera.reference.location = [1, 2, 3]
    camera.label = camera.label + "_NEW"

print("Modified state:", doc.modified) 

doc.save("project2.psx") #Second save under new name
Best regards,
Alexey Pasumansky,
Agisoft LLC

Rafes

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Document Modification State Not Updating After Save in Metashape Pyt
« Reply #4 on: June 27, 2024, 07:48:47 PM »
Interesting! So doc.save() somehow disconnects the chunk in memory from the document and starts a new chunk copy? So then my code continues to work with the original chunk reference, but the doc doesn't know about it?

Note I never duplicate or create a new chunk myself. I just keep working and saving.
Also note, we just switched to Metashape 2.x and this code didn't exhibit this behavior in 1.x.

Rafes

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Document Modification State Not Updating After Save in Metashape Pyt
« Reply #5 on: June 28, 2024, 01:15:13 AM »
I just realized we were not working with psx for debug projects until recently. That could be why it feels new.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14965
    • View Profile
Re: Document Modification State Not Updating After Save in Metashape Pyt
« Reply #6 on: June 28, 2024, 04:43:23 PM »
Hello Rafes,

Yes, behavior relates to PSZ is different to PSX format.

In the latter case only document variable is reassigned when you call doc.save(path) command, whereas all the others variables would remain linked to the previous document instance. I mean, if you have project1.psx be saved as project2.psx, then only Metashape.Document class variable for which .save(path) command has been used, will be re-assigned to project2.psx, while chunk, camera, sensor and other variables will be linke to the instances from project1.psx. Therefore to achieve the expected script behavior you need to take care of such assignments and re-assign variables after calling doc.save(path) method.
Best regards,
Alexey Pasumansky,
Agisoft LLC