[Bf-python] Extending Blender's Python API: expandpath(), GetAbsolutePath(), Get('scenefile'), Get('scenefiledir'), image.filenameAbsolute

Dietrich Bollmann diresu at web.de
Fri Jul 20 08:44:18 CEST 2007


Hi Campbell,

Thanks for your "quick" answer :)

So - if I understand you right - the only thing which would be useful
from my functions would be a function `Blender.sys.cleanpath()' which
behaves as follows?

  >>> Blender.sys.cleanpath('/foo/./bar/dummy/..//baz.blah')
  '/foo/bar/baz.blah'

All the rest could be done by combining this function with things
already there, right?

On Tue, 2007-07-17 at 23:55 +1000, Campbell Barton wrote:
> Hi, quick reply on these topics....
> 1)
> Blender.GetAbsolutePath()
> This would be good, though Id do a little differently
> 
> use sys.expandpath
> ADD sys.cleanpath
> 
> cleanpath would deal with removing ../foo/../foo/.
> and expandpath makes absolute.

You are friendly :)

The truth is that I didn't know about 'sys.expandpath' and found it
after solving my problem.  Only after I found 'sys.expandpath' - which 
didn't cleanup the file though...

Thanks for trying to not hurt my feelings/motivations :)

> use sys.expandpath
> ADD sys.cleanpath
> 
> cleanpath would deal with removing ../foo/../foo/.
> and expandpath makes absolute.

Ok - I will do so in the next days.

> 2)
>  >   - Blender.Get('scenefile')
>  >   - Blender.Get('scenefiledir')
> 
> Dosnt 'filename' do this?
> and for the dir...
>    sys.dirname(Blender.Get('filename'))

No - actually I didn't understand the `Get('filename')' function as it
returns an empty string in my case.  Ah - I loaded a blender file,
saved it with 'File -> Save Default Settings' and restarted blender
with these settings.  Is this the reason?

Anyway, here what happens in my case:

  >>> Blender.Get('filename')
  ''
  >>> Blender.sys.dirname(Blender.Get('filename'))
  '.'

...and using my function:

  >>> Blender.Get('scenefile')
  '/home/dietrich/blender/textures/tiles03/exp/sandstone-mat.blend'

The latter (Get('scenefiledir')) is indeed not necessary as
`sys.dirname' does the trick, thank you (didn't know about this
function either):

  >>> Blender.Get('scenefiledir')
  '/home/dietrich/blender/textures/tiles03/exp/'
  >>> Blender.sys.dirname(Blender.Get('scenefile'))
  '/home/dietrich/blender/textures/tiles03/exp'

> 3)
> I find sys.expandpath(image.filename) is fine to pass on to the 
> operating system or for pythons own file arguments, only bad thing is 
> the ..//../ in the path but the OS supports that.

Yes, but I am writing some kind of exporter and didn't like all those
'/.//blah/../' in the filename.  I wonder that there is no Python
standard way for dealing with paths anyway.

> 4) - Sounds like a bug- do you mean the icon in the image menu? should 
> probably be cleared when python reloads.

No, I mean the big preview window at the left of the shading/texture panel.

Could it be that normally people use the build-in python editor for
writing and executing scripts and that things get updated after
pushing the "execute" button?  I use emacs with python mode (or
actually a self-made blender mode) and execute the code directly from
emacs via a command port socket connection.

So I write `Blender.sys.cleanpath' in the next days.  What should I do
with `Blender.Get('scenefile')'?

Thanks, Dietrich


> Dietrich Bollmann wrote:
> > Hi,
> > 
> > I needed some functionality to get the absolute path of a texture
> > image - and as I couldn't find anything and nobody responded to my
> > posting ( http://www.blender.org/forum/viewtopic.php?p=62764#62764 )
> > I finally implemented my own method:
> > 
> >   Blender.GetAbsolutePath()
> > 
> > Example:
> > 
> >   the scene file is '/home/dietrich/blender/scene/cube.blend'
> > 
> >   >>> Blender.GetAbsolutePath('//../foo/bar')
> >   '/home/dietrich/blender/foo/bar'
> > 
> > Today by chance I found the function `Blender.sys.expandpath()'
> > which more or less does what I want - but not as nice as my 
> > function, as the returned path is not brought into the 
> > canonical form (/.//foo/../bar/./ --> /bar/):
> > 
> >   >>>
> > Blender.sys.expandpath('//../.././/.///.././/../././foo//././/.////one/./two/../three/../../bar')
> > 
> > '/home/dietrich/blender/yafray/textures/tiles/exp/../.././/.///.././/../././foo//././/.////one/./two/../three/../../bar'
> >   >>>
> > Blender.GetAbsolutePath('//../.././/.///.././/../././foo//././/.////one/./two/../three/../../bar')
> >   '/home/dietrich/blender/foo/bar'
> > 
> > I thought to create a patch with my new function but now don't know
> > how to proceed...
> > 
> > Different ideas are:
> > 
> >   - leave my new function as it is (`Blender.GetAbsolutePath()')
> >     probably not a good idea as there would be two similar functions
> >     at different places
> >     
> >   - rename my function to `Blender.sys.expandpathSimplify()'
> > 
> >   - put my function into `expandpath()'
> > 
> >   - give the function `expandpath()' a second facultative argument
> >     which might be 'raw' or 'simplify' with one of the two the default
> > 
> >   - make a third function `simplify()' which could be used to simplify
> >     whatever path there is
> > 
> >   - other ideas?
> > 
> > What do you think would be best?
> > 
> > 
> > * second question
> > 
> > I implemented two new setting keys usable with Blender.Get() as I
> > couldn't find anything similar in the documentation:
> > 
> >   - Blender.Get('scenefile')
> >   - Blender.Get('scenefiledir')
> > 
> > The first returnes the path of the scenefile, the second its
> > directory:
> > 
> >   >>> Blender.Get('scenefile')
> >   '/home/dietrich/blender/scene/cube.blend'
> >   >>> Blender.Get('scenefiledir')
> >   '/home/dietrich/blender/scene/'
> > 
> > I this usable for others also and should I make it a patch?
> > 
> > 
> > * third question
> > 
> > My first solution to my problem - obtaining the absolute path of an
> > image - was to implement `image.filenameAbsolute':
> > 
> > Example:
> > 
> >   with the scene file:  '/home/dietrich/blender/scene/cube.blend'
> >   and the image:        '//../tex/stone.png'
> > 
> >   >>> image.filenameAbsolute
> >   '/home/dietrich/blender/tex/stone.png'
> > 
> > which would result in the same as:
> >   
> >   >>> Blender.GetAbsolutePath(image.filename)
> >   '/home/dietrich/blender/tex/stone.png'
> > 
> > Is this worth a path?  Or should people just use something like the
> > second form?
> > 
> > 
> > * a last question
> > 
> > When loading a new image into a texture with:
> > 
> >   >>> image.filenameAbsolute = '/home/dietrich/blender/tex/stone.png'
> >   >>> image.reload()
> > 
> > and redrawing the windows with  
> > 
> >   >>> Blender.Redraw()
> > 
> > or  
> > 
> >   >>> Blender.Window.RedrawAll()
> > 
> > The image preview window still shows the old image.  Only hitting the
> > [Reload] button on the GUI or reselecting the images in the outliner
> > etc. redraws the preview window.
> > 
> > Is this a bug?  Is there some other redraw function?
> > 
> > Sorry for too many subjects in one mail and thanks for your help :)
> > 
> > Dietrich
> > 
> > 
> > _______________________________________________
> > Bf-python mailing list
> > Bf-python at blender.org
> > http://lists.blender.org/mailman/listinfo/bf-python
> > 
> 
> 




More information about the Bf-python mailing list