# Cycles nodes: is there a way to find coordinates change, derivative, direction?

I want to get a unique random value for every face using direction of mesh coordinates. Like a derivative, position change, a unit vector based on current face's vertices. (I guess, something similar to dFdx() in OpenGL.)

I tried to find the answer on Google, but got no results, though as a software developer I think this function isn't difficult to create. Maybe some Blender experts here know the solution?

• Bake world position to a texture image, then do a two lookups on that texture, one at a UV offset, to get ddx(UV)/ddx(worldpos). – Nathan Jul 17 '18 at 17:24

To get a random value per face to be read by Cycles, you could use an adaptation of Chebhou's answer to this question.

Run this script on the active object:

import bpy
import random

obj = bpy.context.object
obj_data = obj.data
group_name = 'face_ID'

#check for existing group with the same name
if None == obj_data.vertex_colors.get(group_name):
obj_data.vertex_colors.new(name=group_name)
color_map =  obj_data.vertex_colors[group_name]

for poly in obj_data.polygons:
rnd=random.random()
color = (rnd, rnd, rnd,0)
for loop_ind in poly.loop_indices:
color_map.data[loop_ind].color = color


and then use the Cycles 'Attribute' node to retrieve the generated vertex color layer by name: result of this example on a subdivided plane: I've moved the randomization into the script - I was having trouble getting a good pseudo-random number out of a 0.0-1.0 input range using nodes. If nobody points out something obvious in a comment, I'll post that as another question.

• I haven't yet worked with Python scripts in Blender, but will start it with your code :) But my answer (use normals) is closer to geometry, and I will use it if won't find an answer to my other question. And thanks for your reply! – AivanF. Jul 17 '18 at 19:24

I just realised that I can use normal to achieve almost the same behaviour – it is perpendicular to the direction of a face, so, it is an almost isomorphic value that gives some unique value for a face. But I will appreciate any better answer.