Random number of instances

Hi, I have two folder paths: one for “dolls” and one for “hose”. I need to instantiate from the two group a random number of instances. right now, i am using 5 instances per category.

import omni.replicator.core as rep

HOSE_ON_GROUND_ASSETS_DIR = "C:/Users/dsiss/Desktop/Synthia-LFS/Omniverse/randomization_assets/water_hose/rolled_on_ground"
DOLLS_ASSETS_DIR = "C:/Users/dsiss/Desktop/Synthia-LFS/Omniverse/randomization_assets/toys/dolls"

plane_path = "/World/Plane"


with rep.new_layer():

	traversable_plane = rep.get.prim_at_path(plane_path)
	
	hoses_usd = rep.utils.get_usd_files(HOSE_ON_GROUND_ASSETS_DIR)
	hoses_prims = rep.randomizer.instantiate(hoses_usd, size=5, use_cache = False)
	with rep.utils.sequential():
	    with hoses_prims:
	        rep.modify.semantics(semantics = [("class", "hose")])
	
	dolls_usd = rep.utils.get_usd_files(DOLLS_ASSETS_DIR)
	dolls_prims = rep.randomizer.instantiate(dolls_usd, size=5, use_cache = False)
	with rep.utils.sequential():
	    with dolls_prims:
	        rep.modify.semantics(semantics = [("class", "toy")])
	

How can I instantiate x and y instances respectively where x and y are random integers?

Thank you

Pre-compute a list of random integers, then sample that list for size

import omni.replicator.core as rep
import random

# Number of random samples
NUM_RANDOM = 10
# Create a list of random numbers between 1 and 10
random_list = [ random.randint(1, 10) for _ in range(NUM_RANDOM)]
# ...
hoses_prims = rep.randomizer.instantiate(hoses_usd, size=rep.distribution.sequence(random_list), use_cache = False)
# ...

@dennis.lynch This does not work on my end. I get the errors:

2024-05-08 12:58:39  [Error] [omni.graph.core.plugin] /Replicator/SDGPipeline/OgnSampleSequence: [/Replicator/SDGPipeline] Assertion raised in compute - Tried to set a value on AttributeData 'OgnSampleSequence.outputs:samples' of type 'int' with incompatible data (Unable to cast Python instance to C++ type (compile in debug mode for details))
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]   File "c:\users\dsiss\appdata\local\ov\pkg\isaac_sim-2023.1.1\extscache\omni.replicator.core-1.10.20+105.1.wx64.r.cp310\omni\replicator\core\ogn\python\_impl\nodes\OgnSampleSequence.py", line 117, in compute
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]     db.outputs.samples = [items[state.get_index_ordered()]]
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]   File "c:/users/dsiss/appdata/local/ov/pkg/isaac_sim-2023.1.1/kit/exts/omni.graph/omni/graph/core/_impl/database.py", line 690, in __setattr__
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]     super().__setattr__(item, new_value)
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]   File "C:\Users\dsiss\AppData\Local\ov\cache\ogn_generated\1.41.3\omni.replicator.core-1.10.20\omni.replicator.core\ogn\OgnSampleSequenceDatabase.py", line 127, in samples
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]     self.samples.value = value_to_set
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]   File "c:/users/dsiss/appdata/local/ov/pkg/isaac_sim-2023.1.1/kit/exts/omni.graph/omni/graph/core/_impl/utils.py", line 327, in wrapper_non_const
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]     return func(self, *args, **kwargs)
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]   File "c:/users/dsiss/appdata/local/ov/pkg/isaac_sim-2023.1.1/kit/exts/omni.graph/omni/graph/core/_impl/runtime.py", line 139, in value
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]     self.helper.set(new_value, on_gpu=self._on_gpu)
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]   File "c:/users/dsiss/appdata/local/ov/pkg/isaac_sim-2023.1.1/kit/exts/omni.graph/omni/graph/core/_impl/attribute_values.py", line 126, in set
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin]     self._data.set(new_value, on_gpu=on_gpu)
2024-05-08 12:58:39  [Error] [omni.graph.core.plugin] 

here is my code:

import omni.replicator.core as rep
import random 


plane_path = "/World/Plane"
HOSE_ON_GROUND_ASSETS_DIR = "C:/Users/dsiss/Desktop/synthia-LFS/Omniverse/randomization_assets/water_hose/rolled_on_ground"
DOLLS_ASSETS_DIR = "C:/Users/dsiss/Desktop/synthia-LFS/Omniverse/randomization_assets/toys/dolls"


with rep.new_layer():

    traversable_plane = rep.get.prim_at_path(plane_path)


    NUM_RANDOM = 2
    # Create a list of random numbers between 1 and 10
    random_list = [ random.randint(1, 2) for _ in range(NUM_RANDOM)]

    hoses_usd = rep.utils.get_usd_files(HOSE_ON_GROUND_ASSETS_DIR)
    hoses_prims = rep.randomizer.instantiate(hoses_usd, size=rep.distribution.sequence(random_list), with_replacements = False, use_cache = False)
    with rep.utils.sequential():
        with hoses_prims:
            rep.modify.semantics(semantics = [("class", "hose")])


    with rep.utils.sequential():
        with hoses_prims:
            rep.randomizer.scatter_2d(traversable_plane, check_for_collisions=True)

@dennis.lynch your solution works if i use distribution.choice instead. Thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.