9
$\begingroup$

I need a way to get the Object Color in Cycles. I was thinking about using the attribute node, but I don't know what to write in the name entry.

Object color

Is there another way to get the color?

Please don't edit the title or the description; when I write Object Color, it's because that's the name of the property.

In Blender Render, I just have to check this box:

Object Color in Blender Render

I don't want "Vertex Color" or "Viewport Color"; I need the Object Color attribute.

$\endgroup$
  • $\begingroup$ I'm asking to get the "Object Color" propierty inside a Cycles shader look at the pictures please. Why? Because in that way i can assign a Material to several objects and only change the "Object Color" to get anothe combo. $\endgroup$ – Hikaru Ai Dec 7 '13 at 0:58
  • 1
    $\begingroup$ object color is a BI material option that isn't available in cycles. You should be able to access the objects color value through python while using cycles but not through a cycles material node. You may have some luck using a py driver. $\endgroup$ – sambler Dec 7 '13 at 11:25
  • $\begingroup$ I suggest you rearrange some in your workflow. Instead of using the same material depending on Object color (not available in cycles), use different materials in each object but put the common nodes in a group, shared between all objects. See blender.stackexchange.com/questions/3654/… $\endgroup$ – Gunslinger Dec 7 '13 at 19:07
  • $\begingroup$ @sambler I don't think this is possible with a driver, because of this issue. $\endgroup$ – gandalf3 Mar 16 '14 at 19:32
6
$\begingroup$

Vertex Color:

Assuming that you mean Vertex colors, and that you want to get it in the node editor, then you would type the name of the vertex color layer into the attribute node:

enter image description here

From the wiki:

Retrieve attribute attached to the object or mesh. Currently UV maps and vertex color layers can be retrieved this way by their names, with layers and attributes planned to be added. Also internal attributes like P (position), N (normal), Ng (geometric normal) may be accessed this way, although there are more convenient nodes for this.

Note that if you have a UV layer with the same name it will be used instead.

Object Color:

For the Object Color property, the answer is no, this is not possible with the Attribute node currently.

You could try using a driver to get the color into a node tree, however I don't think it will do what you want it too. The driver can only access the object color of one object at a time, so you wouldn't be able to use a different color per object in one material.

As an alternative, you could specify the different colors in the node setup by using object pass indexes and the Object Info node:

  1. Create an "equal to" nodegroup:

    enter image description here

  2. Then setup something like this:

    enter image description here

    If the object index is 1, the object will be green.
    If the object index is 2, the object will be cyan.
    If the object index is not 1 or 2, it will be red.

  3. The object info node uses the Pass index of the object as the index. You can set the Pass index in Properties > Object > Relations:

    enter image description here

You could write a python script to automatically set the colors, Pass index, and create the appropriate nodes for all the objects in your scene.

Here is a script (thanks to iKlsR and sambler for answering my questions here, here, and here) that will automatically setup the pass indexes and create an "Object Color" node group which you can add to your node setups:

import bpy

# Add "equal to" node group:
group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Equal to")
group.inputs.new("NodeSocketFloat", "Value")
group.inputs.new("NodeSocketFloat", "Value")
input_node = group.nodes.new("NodeGroupInput")
input_node.location = (0, 0)

group.outputs.new("NodeSocketFloat", "Value")
output_node = group.nodes.new("NodeGroupOutput")
output_node.location = (600, 0)

# Add a greater than node
gtr_math_node = group.nodes.new(type='ShaderNodeMath')
gtr_math_node.operation = "GREATER_THAN"
gtr_math_node.name = "greater"
gtr_math_node.location = (200, 100)
group.links.new(input_node.outputs[0], gtr_math_node.inputs[0])
group.links.new(input_node.outputs[1], gtr_math_node.inputs[1])

# Add a less than node
lss_math_node = group.nodes.new(type='ShaderNodeMath')
lss_math_node.operation = "LESS_THAN"
lss_math_node.name = "less"
lss_math_node.location = (200,-100)
group.links.new(input_node.outputs[0], lss_math_node.inputs[0])
group.links.new(input_node.outputs[1], lss_math_node.inputs[1])

# Add an add node
add_math_node = group.nodes.new(type='ShaderNodeMath')
add_math_node.operation = "ADD"
add_math_node.name = "add"
add_math_node.use_clamp = True
add_math_node.location = (400, 0)

# connect nodes
group.links.new(gtr_math_node.outputs["Value"], add_math_node.inputs[0])
group.links.new(lss_math_node.outputs["Value"], add_math_node.inputs[1])
group.links.new(add_math_node.outputs["Value"], output_node.inputs[0])

# Create main "Object Color" node group
main_group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Object Color")
main_group.outputs.new("NodeSocketColor", "Object Color")
output_node = main_group.nodes.new("NodeGroupOutput")
output_node.location = (600, 0)

#Add object info node:
ob_index = main_group.nodes.new(type="ShaderNodeObjectInfo")
ob_index.location = (-600,0)

index = 1
# Iterate through all objects in the current scene, set the path indexes and create nodes with the object color
for object in bpy.context.scene.objects:
    if object.type != 'CAMERA' and object.type != 'EMPTY' and object.type != 'LAMP':
        object.pass_index = index
        col = object.color
        if col != (1,1,1,1):

#            add frame node:
            frame = main_group.nodes.new(type="NodeFrame")
            frame.location = (-70, index*200)
            frame.label = object.name
            frame.use_custom_color = True
            frame.color = (0,.005,.133)

#            Add equalto node:
            group_node = main_group.nodes.new("ShaderNodeGroup")
            group_node.node_tree = group
            group_node.label = "Equal to"
            group_node.location = (-200, index*200)
            group_node.name = "eq_" + str(index)
            main_group.links.new(ob_index.outputs["Object Index"], group_node.inputs[0])
            group_node.inputs[1].default_value = index

#            Add mixRGB node:
            mix_node = main_group.nodes.new(type="ShaderNodeMixRGB")
            mix_node.location = (100, index*200)
            mix_node.name = "mix_" + str(index)
            main_group.links.new(group_node.outputs["Value"], mix_node.inputs["Fac"])
            mix_node.inputs["Color1"].default_value = col
            if index != 1:
                main_group.links.new(main_group.nodes["mix_" + str(index-1)].outputs["Color"], mix_node.inputs["Color2"])
            else:
                mix_node.inputs["Color2"].default_value = (1,1,1,1)


            index = index + 1
        else:
            print ("ob-color is white")
    else:
        print ("is camera or lamp")

main_group.links.new(mix_node.outputs["Color"], main_group.nodes["Group Output"].inputs["Object Color"])


# Optional: uncomment the following to add the node group to the active material automatically:
#tree = bpy.context.object.active_material.node_tree
#group_node = tree.nodes.new("ShaderNodeGroup")
#group_node.node_tree = main_group
#group_node.label = "Object Color"

Note that it sets the pass index for every object in the current scene that is not a Lamp, Camera, or an Empty. If you have pass indexes already set, this script will overwrite them.

Usage:

This is script can be run by pasting it into the Text editor and clicking Run Script. This will create the "Object Color" node group with all the nodes and colors set according to the actual Object Color property.

You can add this group to your setups by pressing ShiftA> Add > Node > Group > Object Color.

To change the colors, press Tab with the node group selected to edit it and change the colors of the Color1 slots on the mix nodes.

To update it for new objects etc. you will have to run the script again and create another node group.

$\endgroup$
  • 3
    $\begingroup$ No, i don't want vertex colors, i want "Object Color" that's the name, i'm be asking for this and people just tell me about viewpor color, or shader color. $\endgroup$ – Hikaru Ai Dec 7 '13 at 0:02
  • $\begingroup$ @HikaruAi Getting object color is not really possible atm, but there is a workaround. See my updated answer. $\endgroup$ – gandalf3 Mar 16 '14 at 19:18

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.