3
$\begingroup$

i'm currently trying out a few different things and hit a wall when I was trying to get the rgb value of a color in a palette: enter image description here

I'm looking for the RGB Value of one of the colors in this palette, and i want to get it by python and not just by "what slot is active" but by some sort of ID/Name, my next idea was to go to the Blender File API and look there for the Datapath of the individual color to get an idea how i would need to write my script, however right clicking on it there, does not reveal the usual feature to "copy Data Path" so i suspect that the information is stored somewhere deeper and that i need to access it in another way.

enter image description here Sidenote: It would be really great if we were able to Change the name of the individual colors here and i think that is something that is planned.

My final goal would be to get the color and with that set the color of a material on an object, the last part of it i already got down.

Has anyone an idea how i could get the vaulue? I was only able to get the data path up to the Palette itself:

bpy.data.palettes["PaletteTest"].name

But not on the values it contains.

$\endgroup$
1
3
$\begingroup$

Palette has a colors collection.

Example of displaying and creating palettes. How do i create Palette UI object

Add a new palette,

>>> pal = D.palettes.new("Test")

The palettes color collection holds objects of type bpy.types.PaletteColor. The color is its PaletteColor.color property

give it 10 random colors.

>>> from random import random
>>> for i in range(10):
...     col = pal.colors.new()
...     col.color = (random(), random(), random())
...     

All the palettes in the file (just the new one for example)

>>> for pal in D.palettes:
...     pal.name
...     
'Test'

Get that particular palette, will be None if it doesn't exist

>>> pal = bpy.data.palettes.get("Test")

display its colors..

>>> for col in pal.colors:
...     col.color[:]
...     
(0.9453005194664001, 0.5010330677032471, 0.5785542726516724)
(0.8932003378868103, 0.4246959388256073, 0.30071189999580383)
(0.867354154586792, 0.40056300163269043, 0.9528054594993591)
(0.9828635454177856, 0.22360216081142426, 0.7132857441902161)
(0.6740276217460632, 0.6160644888877869, 0.43397003412246704)
(0.65944504737854, 0.4424607753753662, 0.7807620763778687)
(0.8238367438316345, 0.5752789378166199, 0.6956546306610107)
(0.11555215716362, 0.7942420244216919, 0.33690154552459717)
(0.026920389384031296, 0.7090178728103638, 0.9057185053825378)
(0.36781519651412964, 0.18919502198696136, 0.32881513237953186)

>>> 
$\endgroup$
5
  • $\begingroup$ So bpy.data.palettes.get("PaletteName".colors returns a list/array with all entries in it, how would i get a specific one from that, like the fifth as an example? $\endgroup$
    – Chris
    Jun 24 '21 at 17:55
  • 1
    $\begingroup$ AFAIK the colors aren't assigned name. Index by number pal.colors[0] is the first, etc. & foo.get("bar").prop defeats the purpose, throwing an error if foo.bar is None. Instead if not pal:. $\endgroup$
    – batFINGER
    Jun 24 '21 at 17:57
  • $\begingroup$ Ah okay, i think with the value that is returned its not possible to just straight put it into the value of an RGB Node ... .nodes["NameOfNode"].outputs[0].default_value = (bpy.data.palettes.get("PaletteTest").colors[5]). I get an error doing that : ValueError: bpy_struct: item.attr = val: sequence expected at dimension 1, not 'PaletteColor' $\endgroup$
    – Chris
    Jun 24 '21 at 18:03
  • 1
    $\begingroup$ r, g, b = pal.colors[4].color is the (r, g, b) value given to the 5th color. (ie Use the color attribute as shown in answer) Some nodes may require an alpha, default_value = (r, g, b, 1) $\endgroup$
    – batFINGER
    Jun 24 '21 at 18:05
  • $\begingroup$ that did it! Thank you very much :) ! $\endgroup$
    – Chris
    Jun 24 '21 at 18:13

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.