What is the data path to the Random number in the object info node.

Hi,

In Cycles material nodes, we have this very useful unique random number for each object, found under the ‘object info’ node.

I was wondering if I could get this random number outside the node editor for use in drivers.

Thankyou

In a scripted expression for a driver you can use the built-in random module of python itself.

But fetching the Cycles node value is a little more complicated than just referencing a data path. You have to fetch the node by name then fetch the output socket then fetch the default_value for that socket.

Here are some utility routines I have used in such cases.


def returnNodeFromName(passedNodeName):
    result = None
    for node_tree in bpy.data.node_groups:
        for node in node_tree.nodes:
            if node.name == passedNodeName:
                result = node
    return result


def returnOutputSocketFromName(passedNodeName, passedSocketName):
    result = None
    for node_tree in bpy.data.node_groups:
        for node in node_tree.nodes:
            if node.name == passedNodeName:
                for out in node.outputs:
                    if out.name == passedSocketName:
                        result = out
                        break
    return result

Can you tell me how to use the built-in random module of python?

I don’t need to get the exact same value from the cycles node editor… I was hoping get a random value unique to each object but does not change with every frame of animation or with the transforms of the object.

The reason I asked for the number from the node editor was cause I thought the node editor was fetching a random number assigned to an object on its creation.

I have no idea of python beyond copying data paths and pasting it for drivers.

Thankyou

Here is an example BLEND file setup for you. As you can see it really is just a one line script to return a random number in the range of a passed request. In this case we pass 360 to the driver def and it returns a random number which rotates the cube along the Z-axis where the driver is installed.

So what can go wrong?

Using a ‘custom’ def in a scripted expression requires that the def be registered in the name space of the Blender application before trying to evaluate the result. So the script has to be run before you change the frame. The image shows the order of operation to achieve success with this. But you can also setup Blender to do this automatically. By default, however, Blender blocks the automatic running of scripts on startup as a security option. But this is what we want in our case so to make this an automatic process you have to open up User Preferences and enable Auto Run Python Scripts under the File TAB. Once enabled click Save User Settings to lock this in as a permanent option. This is needed for any kind of complex rig to operate as well.

Attachments

27_random_num_driver.blend (88.6 KB)