[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

absolute path to a file

On 19Aug2019 08:52, Paul St George <email at paulstgeorge.com> wrote:
>On 19/08/2019 01:31, Cameron Simpson wrote:
>>On 18Aug2019 17:29, Paul St George <email at paulstgeorge.com> wrote:
>>>On 18/08/2019 02:03, Cameron Simpson wrote:
>>>1: Is image01.tif a real existing file when you ran this code?
>>>Yes. image01.tif is real, existing and apparent.
>>But in what directory? What is its _actual_ full path as you expect it 
>>to be?
>Aha. The Blender file that I am using to test this on has two images 
>used as textures. They reside at:
>The Blender file is at /Users/Lion/Desktop/test8/tifftest8.blend

There's a remark on that web page I mentioned that suggests that the 
leading '//' indicates the filename is relative to the Blender model, so 
the context directory for the '//' is likely /Users/Lion/Desktop/test8.

>>>(Chris and Peter lead me to believe that Blender has a special kind 
>>>of relative path. The double slashes replace the path to the blend 
>>>file?s directory.) realpath has done something but I know not what.

realpath needs a UNIX path. Your //image01.tif isn't a UNIX path, it is 
a special Blender path. First you need to convert it. By replacing '//' 
with the blend file's directory. Then you can call realpath. If you 
still need to.

>>So you might want to write an unBlenderiser (untested example):
>> ?from os.path import isabs, join as joinpath
>> ?def unblenderise(filename, context_dir=None):
>> ?? # transmute Blender "relative" path
>> ?? if filename.startswith('//'):
>> ???? filename = filename[2:]
>> ?? if not isabs(filename) and context_dir is not None:
>> ???? # attach the filename to `context_dir` if supplied
>> ???? filename = joinpath(context_dir, filename)
>> ?? return filename
>>The idea here is to get back a meaningful UNIX path from a Blender 
>>path. It first strips a leading '//'. Next, _if_ the filename is 
>>relative _and_ you supplied the optional context_dir (eg the 
>>directory used for a file brwoser dialogue box), then it prepends 
>>that context directory.
>>This _does not_ call abspath or realpath or anything, it just 
>>returns a filename which can be used with them.
>>The idea is that if you know where image01.tif lives, you could 
>>supply that as the context directory.
>Yes! Until Peter Otten's timely intervention, I was trying to do this 
>and had managed to join the 
>path_to_the_folder_that_contains_the_Blender_file to 
>the_name_of_the_image (stripped of its preceding slashes).
>Your unblenderise looks much better than my nascent saneblender so 
>thank you. I will explore more!

Looks like you want wd=os.path.dirname(path_of_blend_file).

>Does it depend on knowing where image01.tif lives and manually 
>supplying that?

Sort of.

What you've got is '//image01.tif', which is Blender's special notation 
indicating a filename relative to the blend file's directory. All the 
Python library stuff expects an OS path (OS==UNIX on a Mac). So you want 
to convert that into a UNIX path.  For example:

    from os.path import dirname

    # Get this from somewhere just hardwiring it for the example.
    # Maybe from your 'n' object below?
    blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend'

    blender_image_file = n.image.filename

    unix_image_file = unblenderise(blender_image_file, dirname(blend_file))

Now you have a UNIX path. If blend_file is an absolute path, 
unix_image_path will also be an absolute path. But if blend_file is a 
relative path (eg you opened up "tifftest8.blend") unix_image_path will 
be a relative path.

You don't need to use realpath or abspath on that _unless_ you need to 
reference the file from any directory (i.e. _not_ relative to where you 
currently are).

Cameron Simpson <cs at cskk.id.au>