How to use omni.replicator.core.modify.register API?

As title,
I want to keep regenerating the number and position of cube groups in each trigger, I think the omni.replicator.core.modify.register API can solve my problem, but I’m not sure how to use it, I used it in that way, but I got an error.
Or if there have any others API is more suitable for me, feel free to tell me!

Thanks!

    with rep.trigger.on_frame(num_frames=3, interval=200):
        group = create_cube_group(random.randint(10,19), random.randint(1,3))
        with group:
             **rep.modify.register**(
                 create_cube_group(random.randint(10,19), random.randint(1,3)), override=True
            )

Error: AttributeError: __ enter __
image

Hello @lily.chen,

Replicator register() functions allow you to extend Replicator modules with your own custom functionality. This is most often desired when desiging your own Omniverse Extension as a way to extend Replicator capabilities and meet the needs of your unique project and application.

To register and use a custom function, the following pattern is used:

# Define custom functionality
# Note: function must return a node or a ReplicatorItem object
def create_cube_group(number_of_cubes):
    cubes = rep.create.cube(count=number_of_cubes):
    with cubes:
        rep.modify.pose(position=rep.distribution.uniform((0, 0, 0), (100, 100, 100)))
    return cube

# Register with replicator
rep.randomizer.register(create_cube_group)

# Now, `create_cube_group` is accessible through the `rep.randomizer` module
with rep.trigger.on_frame(num_frames=3, interval=200):
    rep.randomizer.create_cube_group()

I am asking about rep.modify.register not rep.randomizer.register
Thanks

The answer is the same, it just changes the namespace where the method is registered. It can be under randomizer/modify/distribution

@ christianbarcelo
Could you please give me the example for how to use rep.modify.register for whatever scenario? Many thanks

Sorry for the delay here.
AFAIK, the only thing that changes is the name of the module your method is register into.
You can either do:

def my_fancy_distribution():
  distribution = rep.distribution.combine(rep.distribution.uniform(...), rep.distribution.normal(...))
  return distribution.node
rep.distribution.register(my_fancy_distribution)
...
rep.distribution.my_fancy_distribution()

Or…

def my_fancy_modification():
  xform = rep.get.xform(path_pattern=...)
  with xform:
    rep.modify.semantics(...)
  return xform.node
rep.distribution.register(my_fancy_modification)
...
rep.distribution.my_fancy_modification()

Or…

def my_fancy_randomization():
  xform = rep.get.xform(path_pattern=...)
  with xform:
    rep.randomizer.materials(...)
  return xform.node
rep.randomizer.register(my_fancy_randomization)
...
rep.randomizer.my_fancy_randomization()

It’s all the same, the methods are just wrapped as ReplicatorItems inside the module you select, but they will all work the same, you can register under one module or another with no difference at all.