code: def add_material(name, obj, **properties):
      """
      Create a new material and assign it to the active object. "name" should be the
      name of a material that has been previously loaded using load_materials.
      """
      # Figure out how many materials are already in the scene
      mat_count = len(bpy.data.materials)
  
      # Create a new material; it is not attached to anything and
      # it will be called "Material"
      bpy.ops.material.new()
  
      # Get a reference to the material we just created and rename it;
      # then the next time we make a new material it will still be called
      # "Material" and we will still be able to look it up by name
      mat = bpy.data.materials["Material"]
      mat.name = "Material_%d" % mat_count
      mat.use_nodes = True
  
      for i in range(len(obj.material_slots)):
          bpy.ops.object.material_slot_remove({"object": obj})
  
      # if obj.data.materials:
      #     obj.data.materials[0] = mat
      # else:
      assert len(obj.data.materials) == 0
      obj.data.materials.append(mat)
  
      # Find the output node of the new material
      output_node = None
      for n in mat.node_tree.nodes:
          if n.name == "Material Output":
              output_node = n
              break
  
      # Add a new GroupNode to the node tree of the active material,
      # and copy the node tree from the preloaded node group to the
      # new group node. This copying seems to happen by-value, so
      # we can create multiple materials of the same type without them
      # clobbering each other
      group_node = mat.node_tree.nodes.new("ShaderNodeGroup")
      group_node.node_tree = bpy.data.node_groups[name]
  
      # Find and set the "Color" input of the new group node
      for inp in group_node.inputs:
          if inp.name in properties:
              inp.default_value = properties[inp.name]
  
      # Wire the output of the new group node to the input of
      # the MaterialOutput node
      mat.node_tree.links.new(
          group_node.outputs["Shader"],
          output_node.inputs["Surface"],
      )
  
file_path: kubric/challenges/complex_brdf/worker.py
imports: import pathlib
  import logging
  import numpy
  import os
  import kubric
  from kubric.renderer import Blender
  from kubric import randomness
  import bpy
  import os.path
  from glob import glob
  import mathutils
  from kubric import core
  from kubric.core import color
label_desc: Use image node_id 2 for calling a global function with example usage: add_material(name, obj) without return types