Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

Functions such as os.path.exists(image.filepath) fail when Blender uses relative paths, This seems to be because Blender uses // prefix for relative paths, unlike the more conventional ./.

How can standard Python filesystem functions be used correctly on Blender paths?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

The // prefix is Blender specific to denote relative paths used within Blender,

To use Python's filesystem functions you'll have to first expand this using:

eg:

filepath_full = bpy.path.abspath(image.filepath, library=image.library)

Unlike ./ however, which simply expands the current-working-directory (os.getcwd()), Blender supports library-linked files. This means the directory of the current data-block could be a different path then the currently open Blend file.

If you are operating data which the script created, You can skip passing the library argument.

filepath_full = bpy.path.abspath(image.filepath)

Note that this isn't specific to images, it works for movie-clip, sounds, sequencer and text blocks too.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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