Geometry nodes: How to transfer correctly Color attribute from an instance to Python?

I wanted to modify mode code so it would take into consideration instances’ Color attribute transfered through modifier from another object, for each instance individually.

I’m unsure, if I’m just using incorrect names or if there’s a problems elsewhere I don’t see. I could use a fresh pair of eyes on this!

test_color_transfer.blend (2.8 MB)

I’ll try to keep it simple for this demonstration.

  1. I have a plane with distributed instances and randomised colors (which are used in material shader for the instanced object).



  1. I realized all instances out of geometry node tree into individual instances I can manually select and manipulate further.

  1. I go back to the original distribution plane and switch menu from “For preview only” to “For realising and color transfer”.

image

  1. Now I assign “Coloring Realized instances” modifier to all instances for receiving color information for instances’ material.

  1. Instances have exactly the same color as the originals, still on the original distribution plane.

This is the code I’m trying to run.

import bpy

for obj in bpy.context.selected_objects:
    if obj.active_material:
        material = obj.active_material

        # Check if the material has a node tree
        if material.node_tree:
            node_tree = material.node_tree

            # Find the "Vertex Color" node named "Color Geo"
            for node in node_tree.nodes:
                if node.type == "VERTEX_COLOR":  # Check if it's a Vertex Color node
                    if node.layer_name == "Color Geo":  # Ensure it's the right layer
                        color_output = node.outputs[0]  # First output should be the color

                        if color_output.is_linked:
                            # Find the linked node that receives the color data
                            link = color_output.links[0]
                            target_socket = link.to_socket  # Socket receiving the color

                            # If the target socket has a default value, read it
                            if hasattr(target_socket, "default_value") and target_socket.default_value:
                                color = target_socket.default_value
                                colorR = round(color[0] * 255)
                                colorG = round(color[1] * 255)
                                colorB = round(color[2] * 255)

                                print(f"Color Geo RGB: {colorR}, {colorG}, {colorB}")

Color Geo RGB values are always 0. I’d like to get colors printed thanks to modifiers I created. Is that possible?

I spent many hours today trying to get all of this to work and could use some help. Please, let me know, if you see where to problem is and if can show me how to solve it. Thank you!

So, I’ve decided to do it the way I wanted to avoid from the beginning, is this have become too problematic.

Once instances are realised into individual objects, I do following:

Make every instance a single user with individual mesh, data and unique material.
Adding the modifier for changing vertex color “Color geo” (based on original plane)
Applying said modifier.

Now I have thousands of individual objects I didn’t have to change vertex colour manually, but it still works and it’s faster than any other (functional) alternative I know of. Now I can just run my script as originally wanted to.