I would like to have a script node that would output a boolean depending of if two objects are intersecting or not.

The goals is to use it in a mix shader and to hide / show my object depending of the intersection (kind of a mask).

enter image description here

The available inputs of my script node should be two objects ("active" object and "mask" object); and the output should be a boolean (0 or 1) for the FAC of a mix shader.

After having read this, I think it is possible, but don't know Blender scripting good enough yet to achieve it.

Can anyone help ?

Thanks !

  • $\begingroup$ Does this actually need to be a script node or could it be achieved with other means - such as Dynamic Paint? $\endgroup$ Apr 9 '18 at 22:29
  • $\begingroup$ Hi Rich, I don't know about Dynamic Paint. I added an image to improve my question. What do you think ? $\endgroup$
    – gordie
    Apr 10 '18 at 8:39
  • $\begingroup$ Knowing that I would like to be able to "switch" the material of the inside voxels too, not only the "surface" of the pile. $\endgroup$
    – gordie
    Apr 10 '18 at 8:40
  • $\begingroup$ Do you intend for the whole object (cube) to switch as soon as any of it intersected with the ‘mask’ or would only the intersecting bit be affected? $\endgroup$ Apr 10 '18 at 11:50
  • 1
    $\begingroup$ it's better to this with python and hide the geometry if it intersects with another object... specially because there are too many voxels, doing this in OSL is not a good ideia. $\endgroup$
    – Secrop
    Apr 10 '18 at 13:36

I've been working on something that goes somewhere towards answering your problem.. don't take it too seriously, I'm only getting used to some of the quirks of OSL in Blender/Cycles: some of these conditionals may look unnecessary, but for example, if I don't use the 'hit' local variable, it won't work, even though I don't need to.

#include "stdosl.h"

shader Dipper(
    string Container = "",
    output int IsInside = 0

    if  ( raytype("refraction") != 1 ) IsInside = 0;

        int ray_depth;
        getattribute("path:ray_depth", ray_depth);

        if  ( ray_depth % 2  != 0){

            string source_name;
            int hit;
            hit = trace (P,I);
            getmessage ("trace", "geom:name", source_name);

            if (hit==1){
                if (source_name == Container ){ 
                    IsInside =1;

Example Render

You enter the name of your container object in the 'Container' input,(unquoted).


I'm also pretty sure you could make the logic of this shader fail, without too much effort.

  • $\begingroup$ Awesome !!! I'll try that. Just to be sure : OSL does not work on GPU ? $\endgroup$
    – gordie
    Apr 12 '18 at 19:51
  • $\begingroup$ @gordie .. at the moment, you are not offered the option of OSL if you select GPU as your rendering device in Blender. $\endgroup$ Apr 12 '18 at 22:06
  • 1
    $\begingroup$ You could adapt this old script of mine. It works with materialsIDs, so you get a better control of what objects work as a mask and what objects should be rendered as normal. blenderartists.org/forum/… $\endgroup$
    – Secrop
    Apr 13 '18 at 6:16
  • $\begingroup$ Seems nice too. Also, what I wanted to achieve is to "toggle" the material change (eg. when the origin of the mesh is inside the "domain"); not to have a transition. Would it be possible ? $\endgroup$
    – gordie
    Apr 13 '18 at 7:20
  • 1
    $\begingroup$ @Robin Betts, the script is old.. from a time I was still exploring OSL. just posted here to show that you can also probe materialsIDs, when tracing. For the full list of attributes you can retrieve from the message system, check the osl_services.cpp: git.blender.org/gitweb/gitweb.cgi/cycles.git/blob/HEAD:/src/… $\endgroup$
    – Secrop
    Apr 13 '18 at 11:14

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.