Page MenuHome

Cryptomatte: Add RNA to extract the hashes in python scripts
Closed, ArchivedPublicTO DO

Description

EEVEE Cryptomatte took into account to put the cryptomatte hashes in BKE. There are multiple ways how to create a python API.

The challenge of the API is that there are multiple representations of a cryptomatte hash.
I would propose to use Solution 1b from below as it has an explicit interface and the code completion guides the developer on how to use the API.

DevTalk: https://devtalk.blender.org/t/cryptomatte-bpy-api/16543

Solution 1a: CryptomatteHash object

This solution will add functions to the Object and Material RNA struct to get a CryptomatteHash object. The object has a functions to retrieve the representation of the hash as a float or an uint.

crypto_hash = ob.cryptomatte_hash(type='CRYPTOMATTE_ASSET', hasher='MurmurHash3_32')
crypto_hash_uint = int(crypto_hash) # example: 1239807102983
crypto_hash_hex = str(crypto_hash) # example: F3982A8B
crypto_hash_float = crypto_hash.encode(encoding='uint32_to_float32')
crypto_hash_float = float(crypto_hash)

the crypto_hash_uint is the default representation. the crypto_hash_float is the representation when stored in a 32 bit color channel.
the as_float. uint32_to_float32 and 'MurmurHash3_32' are defaults and only opytions. It shows how the API could be enhanced when needed.

  • (-1) Implicit type conversions are not clear to the developer who is using the API.
  • (+1) Code completion will guide the developer on what is possible

Solution 1b: CryptomatteHash object

This solution will add functions to the Object and Material RNA struct to get a CryptomatteHash object. The object has a functions to retrieve the representation of the hash as a float or an uint.

crypto_hash = ob.cryptomatte_hash(type='CRYPTOMATTE_ASSET', hasher='MurmurHash3_32')
crypto_hash_uint = crypto_hash.to_int() # example: 1239807102983
crypto_hash_hex = crypto_hash.to_hex() # example: F3982A8B
crypto_hash_float = crypto_hash.to_float(encoding='uint32_to_float32')

uint32_to_float32 and 'MurmurHash3_32' are defaults and only options. It shows how the API could be enhanced when needed. The implicit conversion to int, str and float can still be supported.

  • (+1) Explicit type conversion
  • (+1) Code completion will guide the developer on what is possible

Solution 2: ID Interface

This solution would add a cryptomatte_hash property to Material and Object ID block what will convert the name of the ID block to the uint representation. Inside bpy.utils.cryptomatte there will be functions to convert the hash to a different encoding. There will also be a function to het the asset object of an object to handle the CRYPTOMATTE_ASSET_LAYER.

crypto_hash_uint = ob.cryptomatte_asset().cryptomatte_hash(hasher='MurmurHash3_32')
crypto_hash_hex = bpy.app.cryptomatte.encode(encoding='uint32_to_hex')
crypto_hash_float = bpy.app.cryptomatte.encode(encoding='uint32_to_float32')
  • (-1) API isn't clear without reading documentation. (No guidance from code completion)