OT: Maya Python and constant color detection

Slightly OT question for the coding/color wizards here…

Working on a Python script used in Maya to connect texture maps to shaders. I’d like to parse the image files to detect if all the pixels have the same value (i.e. detect whether they are a constant color), … or even more simply check all the pixels in an image are black i.e. zero value… then do stuff based on that (connect it if false, or don’t if it’s a black constant meaning an empty image.

What would be the best way to check if all pixels are 0 in an image (it is a black constant) in Maya Python?

Are there modules that do this in the Maya Python install or would it require an extra module like PIL? Would be ideal to avoid extra dependencies if at all possible.

This is Maya2022/2023 on Windows 10

OIIO python bindings has a isConstantColor.
https://openimageio.readthedocs.io/en/latest/pythonbindings.html

The above being said, can you talk a bit about what your end goal is? You might want to do some pre-processing when you mipmap your textures which will save you a lot of time and disk space. Oiiotool has a constant_color_detect, monochrome_detect, opaque_detect, etc… which we use. It will auto convert a constant color to a single pixel and remove extra channels when the texture is just a b&w image written to r, g, b and remove an alpha when it is just white which are all wasteful in disk space and add needed processing time to the renderer.

https://openimageio.readthedocs.io/en/latest/oiiotool.html

Howdy Deke

Yes, the whole idea was inspired by that option in OpenImageIO’s MakeTx

Sure, I’m working on a Python script to connect textures to shaders in Maya. The script parses the texture maps recognizing when an image is a flat solid color, indicating textures output by Substance Painter that were not painted. It will then do the following depending on the texture map type:

  • BaseColor/diffuse maps Will sample the color and put this in the color swatch, rather than connecting the flat texture map.
  • Metalness maps Will set the slider to that value, rather than connecting the flat texture map.
  • Bump & Normal maps Will not connect the flat map, as it will have no effect on the shader.
  • Spec roughness maps Will not connect the flat map and network. Roughness value remains at its default settings.
  • Layer Masks Will not convert the shader to a Layer Shader network.

I have this working on 8-bit textures (jpg, png) using Pyside2 (QT) which comes with Maya’s Python. It however cannot handle image formats like EXR. I was hoping to not have the script require the user to install additional Python modules (like OpenEXR or OpenImageIO) since they can be a bear.

Hello,

Maya 2023 is aligned with the VFX Reference Platform 2022 and has Python 3.9. Given that, you should be able to install/use imageio trivially and use its freeimage backend to read EXR, it is probably the path of least resistance. I’m not sure how you detect flat solid colour but you could do that easily with Numpy by for example subtracting image mean to the image and looking if the residuals are within a tolerance. It is like 3 lines of code.

Cheers,

Thomas

1 Like

Thomas just wanted to say thanks for this tip! I had hit a dead-end wall with trying to get either OIIO or OpenEXR Python bindings working on Windows. Your suggestion with using imageio was like a lifeline out of that hole. :heart_eyes:

1 Like

You are very much welcome!