# Manipulate vertex color of fluid mesh with python

I’m pretty new to Python programming and to the Blender API so this might be a stupid question.

I want to set the vertex color of the vertices of a fluid simulated mesh in Blender 2.90. The goal is to have the vertex color set to the velocity of the nearest fluid particle. I already managed to get the velocity of the nearest particle at any point in space. The problem is, I can’t get access to the fluid mesh vertex colors.

With the following code:

``````domain = bpy.data.objects["Domain"]
mesh = domain.data
color = (0.0, 1.0, 1.0, 0.0)
for poly in mesh.polygons:
for idx in poly.loop_indices:
mesh.vertex_colors.data[idx].color = color
``````

I can set the vertex color of the original domain object (visible in edit mode).
I tried various combinations with .evaluated_get() and .to_mesh() but couldn’t get it to manipulate the modified mesh.

For example:

``````domain = bpy.data.objects["Domain"]
depg = bpy.context.evaluated_depsgraph_get()
mesh = domain.evaluated_get(depg).data
color = (0.0, 1.0, 1.0, 0.0)
for poly in mesh.polygons:
for idx in poly.loop_indices:
mesh.vertex_colors.data[idx].color = color
``````

with this code, there exists no vertex_colors (‘bpy_prop_collection[key]: key 0 not found’)

But I couldn’t get it to manipulate the vertex color after the FluidSim modifier (therefore the domain has the set color in edit mode but the fluid mesh stays black)

Can anyone help me with this?

I found the solution:

Apaprently “applying” the fluid sim modifier (either in the UI or by .evaluated_get()) the vertex colors get removed. This makes kind of sense, because the original vertices get completely replaced. So the solution is to create new vertex_colors after the .evaluated_get():

``````domain = bpy.data.objects["Domain"]
depg = bpy.context.evaluated_depsgraph_get()
mesh = domain.evaluated_get(depg).data
vert_colors = mesh.vertex_colors.new(name="start_pos")
for poly in mesh.polygons:
for idx in poly.loop_indices:
vert_colors.data[idx].color = (1.0, 0.5, 0.0, 1.0)
``````
2 Likes
{"ft_lang_label":"__label__en","ft_lang_prob":0.7512441,"math_prob":0.777419,"size":1391,"snap":"2023-40-2023-50","text_gpt3_token_len":351,"char_repetition_ratio":0.13554434,"word_repetition_ratio":0.16901408,"special_character_ratio":0.25449318,"punctuation_ratio":0.17607974,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97809196,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T17:35:22Z\",\"WARC-Record-ID\":\"<urn:uuid:99a97b53-10d4-44b2-9ceb-290ece82325d>\",\"Content-Length\":\"29445\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:822a0122-30b9-42da-919e-264af221a115>\",\"WARC-Concurrent-To\":\"<urn:uuid:962a2092-a3f0-4b81-8a5c-6a3cd944402a>\",\"WARC-IP-Address\":\"172.67.182.25\",\"WARC-Target-URI\":\"https://blenderartists.org/t/manipulate-vertex-color-of-fluid-mesh-with-python/1255054\",\"WARC-Payload-Digest\":\"sha1:2FEN46LN4UGOQIS3TNAIDHK4KKAOOR5U\",\"WARC-Block-Digest\":\"sha1:R5YBMNOCV7LQO72OXGHOVONDWRXPMZH3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510427.16_warc_CC-MAIN-20230928162907-20230928192907-00145.warc.gz\"}"}