How to add semantic labels to rep.randomizer.instantiate

Hello there,

I was wondering if it would be possible to create a semantic labels with the rep.randomizer.instantiate function. From what I was able to look up this isn’t possible, so to fix this I tried to create a randomizer function that selected a random model from the directory and created it. This did not work, since it only randomly selected the asset in the first interaction, and used the same asset for the rest of the data generation. Would really appreciate some tips.

import omni.replicator.core as rep
import os
import random
  
heatmarks_directory = '/home/rics/Documents/t/heat_A/'
heatmarks_list = rep.utils.get_usd_files(heatmarks_directory, recursive=False)
  
with rep.new_layer():
  
      plane=rep.create.plane(position=(0, 0, -0.42118), scale=50, visible=True)
      camera = rep.create.camera(position=(0, 0, 25), rotation=(0, -90, 0))
      render_product = rep.create.render_product(camera, (1024, 1024))
      rep.settings.set_render_pathtraced(samples_per_pixel=64)
  
      def random_heatmark():
          random_heat_mark= random.choice(heatmarks_list)
          heatmark = rep.create.from_usd(random_heat_mark, semantics=[('class', 'heat_mark')])
  
          with heatmark:
              rep.modify.pose(
                  scale=(100, 100, 100),
          )
          return heatmark.node
      
      rep.randomizer.register(random_heatmark)    
  
      heatmark = None
     
      # Setup randomization
      with rep.trigger.on_frame(num_frames=100, rt_subframes=50):
  
          if heatmark is not None:
              rep.remove([heatmark])
  
          rep.randomizer.random_heatmark()
  
          with camera:
              rep.modify.pose(
                  position=rep.distribution.uniform((-3, -3, 18),(3, 3, 30)),
                  rotation=rep.distribution.uniform((-10, -100, -180),(10, -80, 180))
              )
  
      # Initialize and attach writer
      writer = rep.WriterRegistry.get("BasicWriter")
  
      writer.initialize(output_dir="heat_fault", rgb=True)
  
      writer.attach([render_product])
  
      rep.orchestrator.preview()
1 Like

Hello,

If you are ok applying the same semantic label to all the created objects, you can use instantiate: rep.randomizer.instantiate(...., semantics=[('class', 'thing')]

If you wish to apply a different semantic depending on the object being created, you’ll need to use create.from_usd with a custom function like this:

import omni.replicator.core as rep
file_root = "omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/"

def create_fruits(file_root):
    fruit_usds = rep.utils.get_usd_files(file_root, recursive=True)
    fruits = []
    for fruit_path in fruit_usds:
        fruit = rep.create.from_usd(fruit_path)
        with fruit:
            rep.modify.pose(

                position=rep.distribution.uniform((-100,-100,-100), (100,100,100)),
                rotation=rep.distribution.uniform((-180, -180, -180), (180, 180, 180)),
            )
            if "Avocado" in fruit_path:
                rep.modify.semantics([("class", "avocado")])
            elif "Kiwi" in fruit_path:
                rep.modify.semantics([("class", "kiwi")])
            else:
                rep.modify.semantics([("class", "other_fruit")])

        fruits.append(fruit)
    
    return rep.create.group(fruits)


rep.randomizer.register(create_fruits)

with rep.trigger.on_frame(10):
    rep.randomizer.create_fruits(file_root)

This example also shows instantiating a sequence of objects