"""MIT LicenseCopyright (c) 2018 Sebastian BullingerCopyright (c) 2021 Vladimir Guzov and Ilya PetrovPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE."""importbpy
[docs]defcompute_particle_color_texture(colors,name="ParticleColor"):# To view the texture we set the height of the texture to vis_image_heightimage=bpy.data.images.new(name=name,width=len(colors),height=1)_copy_values_to_image(colors,image.name)image=bpy.data.images[image.name]# https://docs.blender.org/api/current/bpy.types.Image.html#bpy.types.Image.packimage.pack()returnimage
def_copy_values_to_image(value_tuples,image_name):""" Copy values to image pixels """image=bpy.data.images[image_name]# working on a copy of the pixels results in a MASSIVE performance speedlocal_pixels=list(image.pixels[:])forvalue_index,value_tupleinenumerate(value_tuples):column_offset=value_index*4# (R,G,B,A)# Order is R,G,B, opacitylocal_pixels[column_offset]=value_tuple[0]local_pixels[column_offset+1]=value_tuple[1]local_pixels[column_offset+2]=value_tuple[2]# opacity (0 = transparent, 1 = opaque)iflen(value_tuple)==4:local_pixels[column_offset+3]=value_tuple[3]else:# local_pixels[column_offset + 3] = 1.0 # already set by defaultpassimage.pixels=local_pixels[:]