omniverse/replicator

옴니버스 레플리케이터 예제 2탄

luke12 2022. 5. 23. 13:32

Core Functions - “Hello World” of Replicator

 

Core Functions - “Hello World” of Replicator — Omniverse Extensions documentation

First, let’s create a new USD layer using rep.new_layer() in the USD file which will be used to place and randomize the assets. Now let’s create a camera in the scene at the desired position. This enables a viewport for which the frames are generated.

docs.omniverse.nvidia.com

학습 목표

이 튜토리얼의 목적은 미리 정의된 3D 자산 몇 개로 간단한 씬(scene)을 만들고 랜덤화를 적용한 다음 생성된 이미지를 디스크에 기록하여 추가 처리하는 것과 같은 기본적인 Omniverse Replicator 기능을 소개하는 것입니다.

Replicator API를 사용

스크립트를 실행하려면 스크립트 편집기 설정의 설정 지침을 따르십시오.

먼저 자산을 배치하고 랜덤화하는 데 사용할 USD 파일의 rep.new_layer()를 사용하여 새 USD 계층을 생성하겠습니다. 이제 씬(scene)에 원하는 위치에 카메라를 생성하겠습니다. 그러면 프레임이 생성되는 뷰포트가 활성화됩니다. 이를 위해 카메라는 기본 해상도로 렌더러에 부착됩니다. 이 경우 1024x1024 해상도의 기본 렌더러를 사용하고 있습니다.

import omni.replicator.core as rep

with rep.new_layer():

    camera = rep.create.camera(position=(0, 0, 1000))

    render_product = rep.create.render_product(camera, (1024, 1024))

 

이제 간단한 장면을 위한 기본적인 3D 모양을 만들어 보겠습니다. 또한 사용자는 이미 만들었을 수 있는 커스텀 3D 개체를 가져올 수 있습니다. 완전히 개발된 씬(scene)과 함께 레플리케이터 사용 튜토리얼에서는 씬(scene) 내에 사용자 생성한 자산을 가져오는 프로세스를 설명합니다. 자산을 생성할 때 semantics 플래그를 사용하여 의미을 추가합니다. 이러한 의미론적 레이블은 3D 셰이프에 추가되고 데이터에 대한 주석을 생성하는 동안 사용됩니다.

torus = rep.create.torus(semantics=[('class', 'torus')] , position=(0, -200 , 100))

sphere = rep.create.sphere(semantics=[('class', 'sphere')], position=(0, 100, 100))

cube = rep.create.cube(semantics=[('class', 'cube')],  position=(100, -200 , 100))

우리는 카메라를 만들어 렌더러에 부착하고, 3D 자산과 그 자산을 제한하기 위한 평면을 가져왔습니다. 이 시점에서 지정된 분포(이 경우 균일 랜덤)를 사용하여 랜덤화자를 정의할 준비가 되었습니다. 이 튜토리얼에서는 씬(scene) 내에서 이러한 자산의 위치와 크기를 랜덤화합니다.

 

이러한 랜덤화는 매 프레임 이벤트에서 트리거됩니다. 이 예에서는 랜덤화를 사용하여 10개의 프레임을 생성합니다.

with rep.trigger.on_frame(num_frames=10):
    with rep.create.group([torus, sphere, cube]):
        rep.modify.pose(
            position=rep.distribution.uniform((-100, -100, -100), (200, 200, 200)),
            scale=rep.distribution.uniform(0.1, 2))

마지막으로 각 프레임에 대해 생성된 데이터가 디스크에 기록됩니다. 이를 위해 복제자는 이 예에서 설명한 대로 사용할 수 있는 기본 작성기를 제공합니다. 기본 기록기 외에도 사용자는 자신의 사용자 정의 기록기를 작성할 수도 있습니다. 이는 후속 예제 중 하나에 설명되어 있습니다.

 

다음 코드에서는 기본 기록기가 초기화되어 2D 경계 상자로 영상과 주석의 출력을 생성하기 위해 렌더러에 연결됩니다. 사용자는 기록기에 사용할 특정 출력 디렉토리를 지정할 수 있습니다.

writer = rep.WriterRegistry.get("OmniWriter")

writer.initialize(run_id=1, output_dir="_output", rgb=True,   bounding_box_2d_tight=True)

writer.attach([render_product])

output_dir를 수정하지 않은 경우 Linux에서 데이터는 HOME/_output이 됩니다. Windows(윈도우)에서는 사용 권한으로 인해 실패했을 수 있습니다.  _output 폴더를 유효한 폴더로 수정해야 합니다.

 

다음은 스크립트 편집기에 직접 복사할 수 있는 전체 스크립트입니다.

import omni.replicator.core as rep

with rep.new_layer():

    camera = rep.create.camera(position=(0, 0, 1000))

    render_product = rep.create.render_product(camera, (1024, 1024))

    torus = rep.create.torus(semantics=[('class', 'torus')] , position=(0, -200 , 100))

    sphere = rep.create.sphere(semantics=[('class', 'sphere')], position=(0, 100, 100))

    cube = rep.create.cube(semantics=[('class', 'cube')],  position=(100, -200 , 100) )

    with rep.trigger.on_frame(num_frames=10):
        with rep.create.group([torus, sphere, cube]):
            rep.modify.pose(
                position=rep.distribution.uniform((-100, -100, -100), (200, 200, 200)),
                scale=rep.distribution.uniform(0.1, 2))

    # Initialize and attach writer
    writer = rep.WriterRegistry.get("BasicWriter")

    writer.initialize( output_dir="_output", rgb=True,   bounding_box_2d_tight=True)

    writer.attach([render_product])

    rep.orchestrator.preview()

Script Editor 창 하단에 있는 Run(Ctrl + Enter) 버튼을 클릭합니다. 이렇게 하면 작업을 실행하는 데 필요한 모든 노드가 생성됩니다. 마지막 줄 rep.orchestrator.preview()는 출력을 미리 볼 수 있도록 그래프를 한 번 실행합니다.

 

전체 생성 또는 다른 미리 보기를 실행하려면 Replicator → Run에서 왼쪽 상단 모서리를 클릭하여 Replicator 실행 및 미리 보기에 표시된 대로 데이터 생성 프로세스를 시작합니다. 생성된 데이터는 지정된 출력 디렉토리에 저장됩니다.