Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Correct OCIO Color Value Conversion via Python?

    Cinema 4D SDK
    windows python 2024
    2
    3
    457
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      blkmsk
      last edited by

      Hello^^
      I want to import certain parameters from a JSON file with python. The loading of the file and parameter import works fine so far, the only thing I'm not sure about is how to correctly convert the color values.

      Here is a short script to demonstrate what I'm doing right now.
      This will just set a userdata parameter with the name 'Color' on a selected null object to the value [255.0, 94.3499984741211, 76.5]

      import c4d
      
      def convert_rgb_to_color(rgb_list):
             
              if len(rgb_list) != 3:
                  raise ValueError("RGB list must contain exactly 3 values.")
              
              r, g, b = rgb_list
      
              # Normalize 8-bit (0-255) to the range 0.0-1.0
              r_normal = r / 255.0
              g_normal = g / 255.0
              b_normal = b / 255.0
          
              return c4d.Vector(r_normal, g_normal, b_normal)
      
      def main():
          # Get the active object 
          obj = doc.GetActiveObject()
          
          # Check if an object is selected
          if obj is None:
              print("No object selected.")
              return
      
          # Define the color value
          color = [255.0, 94.3499984741211, 76.5]
          
          # Convert the color value
          color_vector = convert_rgb_to_color(color)
          
          # Check if the object has user data
          if not obj.GetUserDataContainer():
              print("The selected object has no user data.")
              return
      
          # Iterate over the user data on the object
          for userdata_id, bc in obj.GetUserDataContainer():
              
              if bc[c4d.DESC_NAME] == "Color":
                  # Set the user data value to the color vector
                  obj[userdata_id] = color_vector
                  c4d.EventAdd()  
                  print("'Color' parameter has been set to:", color_vector)
                  return
      
      # Execute the script
      if __name__ == '__main__':
          main()
      

      When the project color management is set to Basic the value gets set to [255,94,77] as expected.
      But when the color management is set to OpenColorIO the value gets set to:
      sRGB[255,146,143] / RAW[255,94,77].

      How could I set the sRGB value to the desired value rather than the RAW value when color Management is OpenColorIO? Like this:
      sRGB[255,94,77] / RAW[167,45,25].

      I'm Thankfull for any hints on the topic!

      Cheers and best regards
      Ben

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @blkmsk
        last edited by ferdinand

        Hey @blkmsk,

        Thank you for reaching out to us. To make it short, this is currently not yet possible with the python SDK as OCIO has not been wrapped for it. But we are working on it as we are speaking, and OCIO will hopefully be added with an upcoming minor release of Cinema 4D (although 2025.1 will not yet contain it for sure).

        In the meantime, all we can offers is you using our C++ API, for details please see the C++ SDK: OpenColorIO Manual. In Python, with a third party OCIO library and parsing our OCIO config file, and the directly accessible document parameters, especially the new 2025.0.0 ones:

        DOCUMENT_COLOR_MANAGEMENT : Expresses the color management mode a document is in, with the values:
        DOCUMENT_COLOR_MANAGEMENT_BASIC : The document is legacy color management mode.
        DOCUMENT_COLOR_MANAGEMENT_OCIO : The document is OCIO color management mode.
        DOCUMENT_OCIO_CONFIG : Stores the path to the currently used OCIO configuration file of a document.
        DOCUMENT_OCIO_RENDER_COLORSPACE : Stores the currently selected render space integer symbol from the loaded OCIO configuration.
        DOCUMENT_OCIO_DISPLAY_COLORSPACE : Stores the currently selected display space integer symbol from the loaded OCIO configuration.
        DOCUMENT_OCIO_VIEW_TRANSFORM : Stores the currently selected view transform integer symbol from the loaded OCIO configuration.
        DOCUMENT_OCIO_VIEW_TRANSFORM_THUMBNAILS : Stores the currently selected view transform integer symbol for thumbnails from the loaded OCIO configuration.
        DOCUMENT_OCIO_RENDER_COLORSPACE_NAME : [New in 2025.0.0] Directly stores the string for the currently selected render space.
        DOCUMENT_OCIO_DISPLAY_COLORSPACE_NAME : [New in 2025.0.0] Directly stores the string for the currently selected display space.
        DOCUMENT_OCIO_VIEW_TRANSFORM_NAME : [New in 2025.0.0] Directly stores the string for the currently selected view transform.
        DOCUMENT_OCIO_VIEW_TRANSFORM_THUMBNAILS_NAME : [New in 2025.0.0] Directly stores the string for the currently selected view transform for thumbnails.
        

        You might be able to cook up something, but that would be out of scope of support. When you want to do something with OCIO in Python, you must wait for now.

        Cheers,
        Ferdinand

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 1
        • B
          blkmsk
          last edited by

          Hey Ferdinand,

          Thank you for the quick response! Ah ok I see. Thank you for the provided information, I will have a look at it.
          But I can also wait for the moment.

          My work arround for now would be to simply set everything up / import the parameters and colors with color management set to 'Basic' and manually doing the conversion via 'Convert to OCIO' in the Project settings. Thats giving me the correct result.

          But thanks again!

          Cheers,
          Ben

          1 Reply Last reply Reply Quote 0
          • First post
            Last post