1
$\begingroup$

Hello Blender Community,

With my groupmate, we are working on a traffic simulation on Blender as our graduation project. Initially, the simulation engine runs and outputs a .txt file, sim_log.txt, containing the details to be processed by the script on Blender. Then, we run the script and get a visualization of the traffic flow.

Sometimes, I could not really figure out why, the Blender script throws the following exception when trying to read the output file which is in the same directory with the .blender file:

Info: Deleted 0 object(s)
Traceback (most recent call last):
  File "C:\BlenderTest\CS402\SimulationAnimator.blend\Text", line 24, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'sim_log.txt'
Error: Python script failed, check the message in the system console

The script up to the line that the error occurs is as follows:

import bpy
import os
import numpy
import random

#Test values
time = "day"

# Clean console, view and orphan data
os.system("cls")
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.types.BlendData.orphans_purge()

# Set world background
if time == "day":
    bpy.data.worlds['World'].node_tree.nodes['Environment Texture'].image = bpy.data.images.load("//hdris\day.hdr", check_existing=True)
else:
    bpy.data.worlds['World'].node_tree.nodes['Environment Texture'].image = bpy.data.images.load("//hdris\\night.hdr", check_existing=True)

cameraCar = "" # Name of the car that camera will follow

# File read section
f = open("sim_log.txt", "r")

I mostly encounter this problem after another program accesses this file, but sometimes it occurs randomly. I could not find a way to tell Python API/Blender that the file actually is there, so I have been copying the file and updating the filename in the script. I thought the community could spot the problem, so I would not need to work with sim_logv19.txt anymore :)

If you would like to take a look at the project repository, here is the link: https://github.com/sefasenlik/TrafficSim

$\endgroup$
0
0
$\begingroup$

I think based on the Error that open is trying to load the file from the Blender project as a directory, but the project is a file and doesn't have this subdirectory "Text". You could probably fix this by using an absolute path and store the "sim_log.txt" in the same directory of the blender project. To get the absolute path you could use os.path.join(os.path.dirname(bpy.data.filepath), "sim_log.txt")

$\endgroup$
2
  • $\begingroup$ Thank you, Rivin. It seems it worked, and I also think this can even be used as a generic solution for many relative path errors. By the way, I guess I discovered that ending Blender's process on Task Manager resolves the problem temporarily. $\endgroup$ May 11 at 23:33
  • $\begingroup$ If you agree with the answer, please accept it. $\endgroup$
    – Rivin
    May 14 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.