2
$\begingroup$

I'm aware of how to do this with Python, that won't solve the issue in an ideal way for me. Introducing Python is uncomfortably outside of the scope of what I'm writing.

I'm attempting to use a texture lookup from within OSL, as part of a tutorial I'm creating; but the default working directory is the same as Blender's. That makes it very difficult to write portable code that will work on any other machine--I can't use relative paths. I'm really trying to ensure that the tutorial file will immediately work on 2.92, on any machine, out-of-the-box.

Is there a manner in which OSL can retrieve the path to the current .blend file internally?

$\endgroup$
6
  • 1
    $\begingroup$ You can prefix the filename with ‘//‘ to indicate the directory of the current .blend file. Does that not work for your case? $\endgroup$ Mar 19, 2021 at 19:54
  • $\begingroup$ @RichSedman In the case of OSL-internal, it does not. I get an error-pink texture. $\endgroup$ Mar 19, 2021 at 20:12
  • $\begingroup$ It seems I can use "//" in an input field to my OSL node, though! It simply doesn't work inside the script. This is very helpful. I can just make the file path a parameter on the shader. This way, it's handled by some other part of Blender. $\endgroup$ Mar 19, 2021 at 21:42
  • 1
    $\begingroup$ I've just re-tested this and I found I had to use '///' (instead of just '//') as a prefix or it didn't work!! Also, you're right - it's the Blender node that's doing the filename conversion on the string; I always thought it was the OSL side that was doing this.... this does mean that all strings are being interpreted this way! $\endgroup$ Mar 19, 2021 at 22:00
  • $\begingroup$ Alright, so '//' and '///' have subtly different meanings... On Windows, '//' prefixes with the local path using the O/S file path delimiter (eg,'c:\users\rsedman\Desktop\' - ie, backslashes) and '///' does the same but forcing '/' as the path separator (eg, 'c:/users/rsedman/Desktop/...') $\endgroup$ Mar 19, 2021 at 22:09

1 Answer 1

5
$\begingroup$

OSL itself doesn't provide any facilities to access files relative to the .blend file.

However, when passing a 'string' into an OSL script node, Blender automatically assumes that the string represents a filename. As with other filenames in Blender, prefixing the filename with '//' will indicate a path relative to the saved .blend file.

Therefore, in order to get the filepath to the .blend in an OSL shader simply add a 'string' input and set its value (within the node field) to '//'.

$\endgroup$
2
  • 1
    $\begingroup$ Wow. That is arcane (at least, to me). If you don't mind me asking :) Where does that info come from? $\endgroup$
    – Robin Betts
    Mar 20, 2021 at 19:26
  • 1
    $\begingroup$ Of course I don’t :-) I actually happened across this little feature as part of another answer regarding OSL and image filenames - and verified by reviewing the OSL api document (although some of the features in there haven’t actually been implemented yet (as picked out of the source code!) $\endgroup$ Mar 20, 2021 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .