Compositing artists often ask to make a custom cryptomatte for one or another multi-part objects or instances. After the attribute is assigned, you first need to go to the mat network where you have the cryptomatte node, enter the name of the attribute and aov and then go to the out network, add aov on the rop node and enter its name. If you have a lot of such objects, then each time moving from one network to another and assigning them can be a fairly routine task. I wrote a script with the help of which, without leaving the working network, you can automatically add these cryptomattes to arnold's rop.
How does it work. Inside the selected nodes, it search for a node with a render/display flag, this node is always on and displays what goes to render. It then search for attributes named 'crypto_', which is the standard name for all cryptomatte string attributes ('crypto_' + name ), optionally you can enter your own name. After it finds these attributes, it follows the path to the shader (it must be specified in the parameters, often it is in the same place and does not change), where you have the cryptomatte node(s) and enters the name and aov name in the empty channels. If all the node channels are occupied, below it creates another cryptomatte node and writes these values into it. After that, it goes to the arnold rop (whose name you also need to specify in the parameters), creates aov and enters the attribute name aov there.
import re
# paths and names
rop_name = 'render_v00?' #glob
matnet = hou.node('/out/ZDepth/AOV/')
attrib_name = "crypto_"
# out
out = hou.node('/out/')
out_aov = [node for node in matnet.children() if node.type().name() == 'arnold_aov'][0]
out_pos = out_aov.position()
# attribs
sel = hou.selectedNodes()
find_attrib_pt = ''
find_attrib_prim = ''
attribs_crypto = []
attribs_crypto_rop = []
num_list = []
attribs_crypto_set = []
attribs_crypto_rop_set = []
num_empty = 0
numbers = 0
numbers_crypto = []
# find crypto attribs
if sel != ():
for child_nodes in sel:
sub_nodes = child_nodes.allSubChildren(recurse_in_locked_nodes=False)
for nodes in sub_nodes:
nodes_path = nodes.path()
match_paths = re.search('(shopnet|matnet)', nodes_path)
if not match_paths:
flag_render = nodes.isGenericFlagSet(hou.nodeFlag.Render)
if flag_render == True:
attribs_pt = nodes.geometry().pointAttribs()
attribs_prim = nodes.geometry().primAttribs()
#find pt crypto
for attrib_names in attribs_pt:
match = re.search(attrib_name, str(attrib_names))
if match:
find_attrib_pt = str(attrib_names).split(' ')[2][1:-1]
attribs_crypto.append(find_attrib_pt)
attribs_crypto_rop.append(find_attrib_pt)
#find prim crypto
for attrib_names in attribs_prim:
match = re.search(attrib_name, str(attrib_names))
if match:
find_attrib_prim = str(attrib_names).split(' ')[2][1:-1]
attribs_crypto.append(find_attrib_prim)
attribs_crypto_rop.append(find_attrib_prim)
# set arrays
for attribs in set(attribs_crypto):
attribs_crypto_set.append(attribs)
for attribs_rop in set(attribs_crypto_rop):
attribs_crypto_rop_set.append(attribs_rop)
# add cryptomatte if all channel busy
for crypto_node in matnet.glob('cryptomatte*'):
for parms in crypto_node.globParms('user_crypto*'):
empty_parms = crypto_node.evalParm(parms.name())
names_parm_crypto = parms.name()
nums = names_parm_crypto[-1:]
num_list.append(empty_parms)
summ_parms = len(num_list)
if empty_parms != '':
num_empty +=1
if num_empty == summ_parms:
crypto_node_mat = matnet.createNode('arnold::cryptomatte')
crypto_node_mat.setPosition(out_pos)
crypto_node_mat.move([-2, -4])
out_aov.setNextInput(crypto_node_mat, output_index=0)
# find matches
for crypto_node in matnet.glob('cryptomatte*'):
for parms in crypto_node.globParms('user_crypto*'):
empty_parms = crypto_node.evalParm(parms.name())
names_parm_crypto = parms.name()
nums = names_parm_crypto[-1:]
for names in attribs_crypto_set:
if names == empty_parms:
attribs_crypto_set.remove(names)
# add cryptomatte attrib
if attribs_crypto_set != []:
for crypto_node in matnet.glob('cryptomatte*'):
for parms in crypto_node.globParms('user_crypto_aov*'):
empty_parms = crypto_node.evalParm(parms.name())
names_parm_crypto = parms.name()
nums = names_parm_crypto[-1:]
num_crypto = len(attribs_crypto_set)
if empty_parms == '':
numbers_crypto.append(nums)
num_parms_crypto=len(numbers_crypto)-1
for number_ch in range(0, num_crypto):
if num_parms_crypto < num_crypto:
attribs = attribs_crypto_set[num_parms_crypto]
crypto_node.parm('user_crypto_src_'+str(nums)).set(attribs)
crypto_node.parm('user_crypto_aov_'+str(nums)).set(attribs + '_aov')
# match rop aovs
for nodes_out in out.glob(rop_name):
out_node = nodes_out
sum_aov = out_node.evalParm('ar_aovs')
for nums_aov in range(1, sum_aov+1):
aovs = out_node.evalParm('ar_aov_label'+str(nums_aov))
for names_crypto in attribs_crypto_rop_set:
names_crypto_aov = names_crypto + '_aov'
if aovs == names_crypto_aov:
attribs_crypto_rop_set.remove(names_crypto)
# write to rop
for nodes_out in out.glob(rop_name):
out_node = nodes_out
num_crypto = len(attribs_crypto_rop_set)
sum_aov = out_node.evalParm('ar_aovs')
out_node.parm('ar_aovs').set(sum_aov+num_crypto)
for names_crypto in attribs_crypto_rop_set:
numbers += 1
nums_aov_rop = sum_aov + numbers
names_crypto_aov = names_crypto + '_aov'
out_node.parm('ar_aov_label' + str(nums_aov_rop)).set(names_crypto_aov)