101. Writing the export script

Questions and discussion for this lecture live here. Fire away by hitting Reply below :fire:

Hi Sean,
I am experimenting an issue with the function

with open(absolute_path, 'w', newline = '') as file:

This is what I get from the Terminal

Error: Python: Traceback (most recent call last): File "<string>", line 2317, in invoke File "<string>", line 2311, in execute File "<string>", line 2348, in sna_data_export_56812 FileNotFoundError: [Errno 2] No such file or directory: '//mypath/results_data.csv'

I have tried also with your file and the result does not change. Any idea?
Thank you in advance

Hey @Oliviero,
How are you determining that file path (//mypath/results_data.csv), are you using selecting it by using the interface we built, i.e. updating the Serpens property in the UI or is that a path that you have typed into your script?

Seán

I am using the Structure Works interface in Blender, which updates the Serpens property that is then passed through the Serpens property node to the function

Ok, that’s strange since the error is suggesting that the file or path doesn’t exist.

To bug fix this, try to hardcode in the path to your desktop, directly in the script(rather than setting it through the Serpens property/UI. We’re trying to rule out anything on the Serpens/Blender side here. Let me know how this goes.

One other thing…the ‘mypath’ part looks odd…do you actually have a folder called ‘mypath’??

S

One other thing…the ‘mypath’ part looks odd…do you actually have a folder called ‘mypath’??

No, the path is actually something like /Users/user/Desktop/. I just simplified it earlier to avoid writing out the full path, especially since I’ve tried using several different ones.

To bug fix this, try to hardcode in the path to your desktop, directly in the script

When I do that, the file is created successfully. However, I’ve noticed that while the correct path should be /Users/user/Desktop/, for some reason, if I select the same path through our interface in Blender (by choosing it in the file dialog), debug prints show that

file_path is //../../../../../Desktop/user/model_data.csv
while the absolute_path is //Desktop/user/model_data.csv

Neither is correct, but the first one is especially strange. I’ve also seen that this odd path shows up in the UI once it’s been selected.

Additionally, if I manually enter the correct path in our Blender UI, the file is created as expected.

So basically, the problem seems to be related to how the file path is handled when it’s selected via Blender’s file view (the window that pops up when you click the folder button next to the file path box).

I really have no idea what’s going on here—I hope it isn’t due to the fact that I’m using Blender version 4.1, and that for some reason it’s not compatible with Serpens. This would be the first bug I’ve encountered with this version so far.

Ok, interesting…

Here’s what I see in my code, tell me what you see when you run the same exercise.

I access the file_path_input in my export_model_data.py script:

file_path = file_path_input # type: ignore

Printing this…

print(f"raw file path: {file_path}")

Yields…

raw file path: /Users/Sean/Desktop/

Now I update the path to append the file name and enforce absolute path and print this:

absolute_path = os.path.abspath(file_path)
print(f"absolute path: {absolute_path}")

Which yields this:

absolute path: /Users/Sean/Desktop/model_data_2025-08-21-09-57-30.csv

Running the same exercise what is your output?

Also, to try and rule out Blender versioning being an issue, load your development file with a later version of Blender (4.2+…say 4.5). Run the same path printing exercise and observe the output and let me know what you see.

What I get is, respectively:

raw file path: //../../../../../Desktop/user
absolute path: //Desktop/user/model_data.csv

I get the same result when using Blender 4.2.9. In this version, the program is extremely unstable and difficult to work with, but I still managed to select the path and click the export button.

If you think it would help, I can try downloading a more recent version of Blender and see if the issue persists.

Ok - seems like the version of Blender you are using is adding an extra / at the beginning of the file path for some reason. I can only assume that this arises due to the difference in Blender versions since you observe the same behaviour when you run my file (which obviously works normally on recent versions of Blender).

A simple and practical fix would be to check the absolute path, right after it has been created, for a double slash at the beginning of the string. If there is a double slash, strip one out, if not, leave the path as is. This should mean you always have a correct path, even after your addon is exported and you or someone else runs it on a newer version of Blender.

S

Thank you for your suggestion. I tried removing the extra /, but unfortunately, it still doesn’t work because the path is not only incorrect due to the double slash. As I mentioned earlier, the correct path should be:

/Users/user/Desktop/

However, the path I’m getting is:

//Desktop/user/

So, it seems that /Desktop and /user are swapped, and /Users is missing entirely. I’m not sure why the directories are reordered like this. I’m finding it difficult to implement a fix in the code that would work for the path issue and still handle other situations correctly, as the one you suggested before.

Thank you again for your help!

Very strange…would be interesting to know if you replicate the same behaviour on a newer version of Blender (4.5+).

The fact that you observed the same behaviour when you downloaded the course Blend file and script, does suggest that this is a local issue, possibly arising from the version of Blender you’re running.

If you want to zip up your blend file and scripts, I can run them here and let you know what I see.

S