I am currently writing my own shader using a Scriptnode in Blender, and I cannot figure out how to limit the range of a slider between 0 and 1.

I basically want something like the "Fac" slider in a Mix Shader, but whatever I try in my custom shader, I always can enter values < 0 or > 1 when I use my script node in the node editor. (I tried to clamp the value, and to do annotations for min and max as in the OSL doc.)

Does anyone know how to limit the input range of a float slider to 0...1 ?


1 Answer 1


March 14 2016

The markup enclosed in the [[ ]] is known as "suggested metadata", it's shown in the OSL specifications pdf, something like this:

surface wood
            [[ string help = "Realistic wood shader" ]]
        float Kd = 0.5
            [[ string help = "Diffuse reflectivity",
                float min = 0, float max = 1 ]] ,
        color woodcolor = color (.7, .5, .3)
            [[ string help = "Base color of the wood" ]],
        color ringcolor = 0.25 * woodcolor
            [[ string help = "Color of the dark rings" ]],
        string texturename = "wood.tx"
            [[ string help = "Texture map for the grain",
                string widget = "filename" ]],
        int pattern = 0
            [[ string widget = "mapper",
                string options = "oak:0|elm:1|walnut:2" ]]

is not currently supported, it gets ignored.

Until someone implements the parsing of suggested metadata the best you can do is implement the clamp yourself in your OSL code. The UI would still allow you to input values outside that range, therefore cosmetically this isn't a good solution, but if you make your code deal with unwanted extreme values appropriately you'll still get stuff done.

Later when metadata parsing is implemented it's easy enough to strip away your clamping code.

related: Can a Script node using OSL have a checkbox?

  • $\begingroup$ Thanks for the detailed answer, but what puzzles me: the "default" nodes in cycles are able to have limited ranges between 0...1. Shouldn't thus exist a workaround to this problem? (I looked up the build-in cycles shaders, and they don't do much more than I do in my shaders...so where and how does the blender sourcecode handle this?) $\endgroup$ Mar 14, 2016 at 13:20
  • $\begingroup$ The problem isn't that the node system can't limit values (min / max ..etc) -- obviously it can, the problem is that the parser that reads the OSL code doesn't translate the metadata into properties of the associated inputs/parameters. $\endgroup$
    – zeffii
    Mar 14, 2016 at 13:26
  • $\begingroup$ Best to get a conversation going about this on the 'blender functionality' mailing list. -- it's kind of outside the scope of what BlenderStackExchange is set up to discuss. (ie features already present and functional in current Blender) $\endgroup$
    – zeffii
    Mar 14, 2016 at 13:30

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.