The good thing is, it's not magic, there is a simple explanation. But I don't know what it is yet.
The Linux permissions and ssh permissions are the same. The octoprint permissions are different. Octoprint is running as the pi user, AFAIK. So when you log into octoprint as changedsoul, it still runs your script as pi.
The first part at play is the permissions, which you say are 777, and you can run it via ssh as pi, so the folders must be accessible. The second part is the environment, which I think is the same as for pi also. So if you can run it via command line, I don't know why octoprint can't.