Change Material color picker from HSV input to RGB input or convert RGB color to HSV color via Code.

Change Material color picker from HSV input to RGB input or convert RGB color to HSV color via Code.

deven.saxenaP7BD5
Enthusiast Enthusiast
956 Views
1 Reply
Message 1 of 2

Change Material color picker from HSV input to RGB input or convert RGB color to HSV color via Code.

deven.saxenaP7BD5
Enthusiast
Enthusiast

Hello,

 

TLDR: I am experiencing an issue with entering RGB color values via code, as the default color picker in use is based on the HSV color model. This has led to the colors not appearing accurate as they are not being accurately translated between the two color models.

 

I have an external source that dictates a color value that is in RGB(0-255). I have a python tool that creates the shader and assigns it to the geometry required. The issue here is that I have a value in RGB say (115,115,115) and when I plug this using " cmds.setAttr("{}.color".format(material_name, 115,115,115, type = "double3") ", it will plug this value as HSV and as a result, Saturation in HSV(which is capped between 0-1) and Value ends up with an extreme level of 115. When I manually assign these values by clicking on color picker and changing the color model from HSV to RGB(0-255), it works. How can I modify the above code to change color model from HSV to RGB before assigning values? Or is there a converter that works and can convert RGB color values into HSV colors?

 

I did try some RGB to HSV tools but they usually have the same issue where Saturation and Value in HSV are more than 1.  

 

Thanks in advance.

0 Likes
957 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

The colorsys library has a rgb to hsv converter that works fine for me normally.

You first have to remap the rgb values to a range of 0-1, which can be done with a simple division by 255 for each value.

Give this a try:

 

import maya.cmds as cmds
import colorsys


(r, g, b) = (115, 115, 115)
(r, g, b) = (r / 255, g / 255, b / 255)
(h, s, v) = colorsys.rgb_to_hsv(r, g, b)

cmds.setAttr("{}.color".format(material_name), h,s,v, type = "double3")

 

 

I hope it helps!

 

 

0 Likes