Fix relative path errors when saving Trajectories#1088
Fix relative path errors when saving Trajectories#1088PardhavMaradani wants to merge 8 commits intoBradyAJohnston:mainfrom
Conversation
|
Hi @BradyAJohnston , I didn't quite understand what's happening in import os
def trim_root_folder(filename):
"Remove one of the prefix folders from a filepath"
return os.sep.join(filename.split(os.sep)[1:])
def make_path_relative(filepath):
"Take a path and make it relative, in an actually usable way"
try:
filepath = os.path.relpath(filepath)
except ValueError:
return filepath
# count the number of "../../../" there are to remove
n_to_remove = int(filepath.count("..") - 2)
# get the filepath without the huge number of "../../../../" at the start
sans_relative = filepath.split("..")[-1]
if n_to_remove < 1:
return filepath
for i in range(n_to_remove):
sans_relative = trim_root_folder(sans_relative)
return f"./{sans_relative}"make_path_relative("/tmp/adk_dims.dcd")prints: make_path_relative("/tmp/x/y/adk_dims.dcd")prints: I can close this and raise it as an issue if you want to take a look. I hope you are able to reproduce the original issue - I just had |
* Isolate trajectory save persistence test to not modify fixture
|
Hi Brady, do we really need to convert the md trajectory filename to a relative path? After a bit more testing, it seems to me that this creates more problems than it solves. Even basic portability on a single machine is broken due to this. This PR converts the trajectory filename relative to the saved blend (and MNSession) file which works for the save and load scenarios, but is still not good enough. Let me explain. MDAnalysis supports a list of files as well for the coordinates, so this doesn't work for those cases (when used from the API, since GUI only supports one trajectory file currently). Blender python's I would recommend removing this conversion all together. If we need to support portability across machines etc, we could potentially expose these filenames - maybe in the Trajectory panel in the n-panel for these to be configurable at runtime - updates of which will call the |
|
It's been a while since I revisited the filepath stuff but it has been on my list of "janky things to fix" for a while. The original idea was to save the filepaths relative to the I agree the current setup is a bit of a mess and does currently fail in some situations. I think we need to make it clear what the path ahead is that needs to be taken though. I don't have time to look over code for a few days but I should be able to get to this soon. The main points / issues that I know around feilpaths are this:
|
|
If the behaviour needs to be as described in the previous comment, we should probably just set the cwd to the blend file directory when saving and loading. This is not ideal because other addons can also change the cwd at will. But this will at least fix the save and load issues and also ensure that any MDAnalysis reload of the trajectory also works. I have updated the PR accordingly. Thanks |
|
We definitely need to avoid setting the |
|
Hi Brady, if we want to change the trajectory path to relative (to the The very first commit sets the |
|
Hi Brady, the latest commit has something workable that fixes all the current issues with save/load and MDAnalysis trajectory reader. There are larger issues with pickling/unpickling that we'll have to address in the future - I'll try raise them as a separate issue. The current changes do the following: During unpickling, the current directory is again temporarily set to that of the blend file, the trajectory paths set back to absolute paths after unpickling and re-loaded. There is no permanent change of |
This PR fixes errors that are thrown due to incorrect relative paths when saving Trajectory entities.
Here is a simple script which when run from a Notebook shows the problem:
Show Traceback
The problem is not specific to API use, but can also be reproduced with regular Blender GUI. The issue arises because Blender Python's current working directory can be very different based on how it is launched (say when launching through jupyter, command line or from GUI). The relative paths made during picking are not necessarily valid and hence leads to errors both during pickling and later during unpickling as those files will not be found.
This PR uses a context manager to temporarily change the current directory to that of the blend file before pickling and unpickling, which fixes the problem. Thanks