def _set_scene_camera(self, renderer, scene):
     if scene.hdusd.final.nodetree_camera != '' and scene.hdusd.final.data_source:
         usd_camera = UsdAppUtils.GetCameraAtPath(self.stage, scene.hdusd.final.nodetree_camera)
     else:
         usd_camera = UsdAppUtils.GetCameraAtPath(self.stage, Tf.MakeValidIdentifier(scene.camera.data.name))
    
     gf_camera = usd_camera.GetCamera(scene.frame_current)
     renderer.SetCameraState(gf_camera.frustum.ComputeViewMatrix(),
                             gf_camera.frustum.ComputeProjectionMatrix())
Ejemplo n.º 2
0
    def setUpClass(cls):
        cls._usdFilePath = os.path.abspath('AnimCube.usda')
        cls._stage = Usd.Stage.Open(cls._usdFilePath)
        cls._usdCamera = UsdAppUtils.GetCameraAtPath(
            cls._stage, UsdUtils.GetPrimaryCameraName())

        cls._glWidget = cls._SetupOpenGLContext()

        cls._frameRecorder = UsdAppUtils.FrameRecorder()
        cls._frameRecorder.SetColorCorrectionMode('sRGB')
Ejemplo n.º 3
0
    def testGetCameraBadParams(self):
        """
        Tests trying to get cameras that don't exist and using invalid
        parameters.
        """
        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage, 'BogusCamera')
        self.assertFalse(usdCamera)

        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage,
                                                Sdf.Path.emptyPath)
        self.assertFalse(usdCamera)

        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage, 'foo.bar')
        self.assertFalse(usdCamera)

        with self.assertRaises(Tf.ErrorException):
            UsdAppUtils.GetCameraAtPath(None, 'MainCamera')
Ejemplo n.º 4
0
    def testGetCameraByName(self):
        """
        Tests getting cameras using just the camera's prim name. The stage
        should be searched for the camera prim matching that name.
        """
        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage, 'MainCamera')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString, '/Cameras/MainCamera')

        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage, 'CameraB')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString,
                         '/Cameras/Deeply/Nested/CameraB')

        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage, 'OtherCamera')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString,
                         '/OtherScope/OtherCamera')
Ejemplo n.º 5
0
    def testGetCameraAbsolutePath(self):
        """
        Tests getting cameras using an absolute path.
        """
        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage,
                                                '/Cameras/MainCamera')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString, '/Cameras/MainCamera')

        usdCamera = UsdAppUtils.GetCameraAtPath(
            self._stage, '/Cameras/Deeply/Nested/CameraB')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString,
                         '/Cameras/Deeply/Nested/CameraB')

        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage,
                                                '/OtherScope/OtherCamera')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString,
                         '/OtherScope/OtherCamera')
Ejemplo n.º 6
0
    def testGetCameraByMultiElementPath(self):
        """
        Tests getting cameras using a multi-element path. Currently these paths
        are just made absolute using the absolute root path before searching.
        """
        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage,
                                                'Cameras/MainCamera')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString, '/Cameras/MainCamera')

        usdCamera = UsdAppUtils.GetCameraAtPath(
            self._stage, 'Cameras/Deeply/Nested/CameraB')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString,
                         '/Cameras/Deeply/Nested/CameraB')

        usdCamera = UsdAppUtils.GetCameraAtPath(self._stage,
                                                'OtherScope/OtherCamera')
        self.assertTrue(usdCamera)
        self.assertEqual(usdCamera.GetPath().pathString,
                         '/OtherScope/OtherCamera')
Ejemplo n.º 7
0
    def render(self, depsgraph):
        if not self.is_synced:
            return

        scene = depsgraph.scene
        width, height = scene.render.resolution_x, scene.render.resolution_y

        renderer = UsdImagingLite.Engine()
        renderer.SetRendererPlugin('HdRprPlugin')
        renderer.SetRendererSetting('rpr:maxSamples', self.SAMPLES_NUMBER)
        renderer.SetRendererSetting('rpr:core:renderQuality', 'Northstar')
        renderer.SetRendererSetting('rpr:alpha:enable', False)
        renderer.SetRendererSetting('rpr:adaptiveSampling:minSamples', 16)
        renderer.SetRendererSetting('rpr:adaptiveSampling:noiseTreshold', 0.05)

        renderer.SetRenderViewport((0, 0, width, height))
        renderer.SetRendererAov('color')

        # setting camera
        usd_camera = UsdAppUtils.GetCameraAtPath(
            self.stage, Tf.MakeValidIdentifier(scene.camera.data.name))

        gf_camera = usd_camera.GetCamera()
        renderer.SetCameraState(gf_camera.frustum.ComputeViewMatrix(),
                                gf_camera.frustum.ComputeProjectionMatrix())

        params = UsdImagingLite.RenderParams()
        image = np.empty((width, height, 4), dtype=np.float32)

        def update_render_result():
            result = self.render_engine.begin_result(0, 0, width, height)
            render_passes = result.layers[0].passes
            render_passes.foreach_set('rect', image.flatten())
            self.render_engine.end_result(result)

        renderer.Render(self.stage.GetPseudoRoot(), params)

        while True:
            if self.render_engine.test_break():
                break

            if renderer.IsConverged():
                break

            renderer.GetRendererAov('color', image.ctypes.data)
            update_render_result()

        renderer.GetRendererAov('color', image.ctypes.data)
        update_render_result()

        # its important to clear data explicitly
        renderer = None
 def _set_scene_camera(self, renderer, scene):
     usd_camera = UsdAppUtils.GetCameraAtPath(self.stage, sdf_path('Camera.002'))
     gf_camera = usd_camera.GetCamera()
     renderer.SetCameraState(gf_camera.frustum.ComputeViewMatrix(),
                             gf_camera.frustum.ComputeProjectionMatrix())
Ejemplo n.º 9
0
def main():
    programName = os.path.basename(sys.argv[0])
    parser = argparse.ArgumentParser(
        prog=programName, description='Generates images from a USD file')

    # Positional (required) arguments.
    parser.add_argument('usdFilePath',
                        action='store',
                        type=str,
                        help='USD file to record')
    parser.add_argument(
        'outputImagePath',
        action='store',
        type=str,
        help=('Output image path. For frame ranges, the path must contain '
              'exactly one frame number placeholder of the form "###" or '
              '"###.###". Note that the number of hash marks is variable in '
              'each group.'))

    # Optional arguments.
    parser.add_argument(
        '--mask',
        action='store',
        type=str,
        dest='populationMask',
        metavar='PRIMPATH[,PRIMPATH...]',
        help=(
            'Limit stage population to these prims, their descendants and '
            'ancestors. To specify multiple paths, either use commas with no '
            'spaces or quote the argument and separate paths by commas and/or '
            'spaces.'))

    parser.add_argument(
        '--purposes',
        action='store',
        type=str,
        dest='purposes',
        metavar='PURPOSE[,PURPOSE...]',
        default='proxy',
        help=(
            'Specify which UsdGeomImageable purposes should be included '
            'in the renders.  The "default" purpose is automatically included, '
            'so you need specify only the *additional* purposes.  If you want '
            'more than one extra purpose, either use commas with no spaces or '
            'quote the argument and separate purposes by commas and/or spaces.'
        ))

    UsdAppUtils.cameraArgs.AddCmdlineArgs(parser)
    UsdAppUtils.framesArgs.AddCmdlineArgs(parser)
    UsdAppUtils.complexityArgs.AddCmdlineArgs(parser)
    UsdAppUtils.colorArgs.AddCmdlineArgs(parser)
    UsdAppUtils.rendererArgs.AddCmdlineArgs(parser)

    parser.add_argument(
        '--imageWidth',
        '-w',
        action='store',
        type=int,
        default=960,
        help=(
            'Width of the output image. The height will be computed from this '
            'value and the camera\'s aspect ratio (default=%(default)s)'))

    args = parser.parse_args()

    UsdAppUtils.framesArgs.ValidateCmdlineArgs(
        parser, args, frameFormatArgName='outputImagePath')

    args.imageWidth = max(args.imageWidth, 1)

    purposes = args.purposes.replace(',', ' ').split()

    # Open the USD stage, using a population mask if paths were given.
    if args.populationMask:
        populationMaskPaths = args.populationMask.replace(',', ' ').split()

        populationMask = Usd.StagePopulationMask()
        for maskPath in populationMaskPaths:
            populationMask.Add(maskPath)

        usdStage = Usd.Stage.OpenMasked(args.usdFilePath, populationMask)
    else:
        usdStage = Usd.Stage.Open(args.usdFilePath)

    if not usdStage:
        _Err('Could not open USD stage: %s' % args.usdFilePath)
        return 1

    # Get the camera at the given path (or with the given name).
    usdCamera = UsdAppUtils.GetCameraAtPath(usdStage, args.camera)

    # Frame-independent initialization.
    # Note that the size of the widget doesn't actually affect the size of the
    # output image. We just pass it along for cleanliness.
    glWidget = _SetupOpenGLContext(args.imageWidth, args.imageWidth)

    frameRecorder = UsdAppUtils.FrameRecorder()
    if args.rendererPlugin:
        frameRecorder.SetRendererPlugin(args.rendererPlugin.id)
    frameRecorder.SetImageWidth(args.imageWidth)
    frameRecorder.SetComplexity(args.complexity.value)
    frameRecorder.SetColorCorrectionMode(args.colorCorrectionMode)
    frameRecorder.SetIncludedPurposes(purposes)

    _Msg('Camera: %s' % usdCamera.GetPath().pathString)
    _Msg('Renderer plugin: %s' % frameRecorder.GetCurrentRendererId())

    for timeCode in args.frames:
        _Msg('Recording time code: %s' % timeCode)
        outputImagePath = args.outputImagePath.format(
            frame=timeCode.GetValue())
        try:
            frameRecorder.Record(usdStage, usdCamera, timeCode,
                                 outputImagePath)
        except Tf.ErrorException as e:

            _Err("Recording aborted due to the following failure at time code "
                 "{0}: {1}".format(timeCode, str(e)))
            break

    # Release our reference to the frame recorder so it can be deleted before
    # the Qt stuff.
    frameRecorder = None
 def _set_scene_camera(self, renderer, scene):
     usd_camera = UsdAppUtils.GetCameraAtPath(
         self.stage, Tf.MakeValidIdentifier(scene.camera.data.name))
     gf_camera = usd_camera.GetCamera()
     renderer.SetCameraState(gf_camera.frustum.ComputeViewMatrix(),
                             gf_camera.frustum.ComputeProjectionMatrix())
    def render(self, depsgraph):
        if not self.is_synced:
            return

        scene = depsgraph.scene
        width, height = scene.render.resolution_x, scene.render.resolution_y

        # uses for creating a transparent background icon to follow blender UI style
        is_preview_icon = width == 32 and height == 32

        self.renderer.SetRendererSetting('rpr:maxSamples', self.SAMPLES_NUMBER)
        self.renderer.SetRendererSetting('rpr:core:renderQuality', 'Northstar')
        self.renderer.SetRendererSetting('rpr:alpha:enable', is_preview_icon)
        self.renderer.SetRendererSetting('rpr:adaptiveSampling:minSamples', 16)
        self.renderer.SetRendererSetting('rpr:adaptiveSampling:noiseTreshold',
                                         0.05)

        self.renderer.ClearRendererAovs()
        self.renderer.SetRenderViewport((0, 0, width, height))
        self.renderer.SetRendererAov('color')

        # setting camera
        usd_camera = UsdAppUtils.GetCameraAtPath(
            self.stage, Tf.MakeValidIdentifier(scene.camera.data.name))

        gf_camera = usd_camera.GetCamera()
        self.renderer.SetCameraState(
            gf_camera.frustum.ComputeViewMatrix(),
            gf_camera.frustum.ComputeProjectionMatrix())

        params = UsdImagingLite.RenderParams()
        image = np.zeros((width, height, 4), dtype=np.float32)

        def update_render_result():
            result = self.render_engine.begin_result(0, 0, width, height)
            render_passes = result.layers[0].passes
            render_passes.foreach_set('rect', image.flatten())
            self.render_engine.end_result(result)

        while True:
            if self.render_engine.test_break():
                break

            try:
                self.renderer.Render(self.stage.GetPseudoRoot(), params)

            except Exception as e:
                # known RenderMan issue https://github.com/PixarAnimationStudios/USD/issues/1415
                if isinstance(
                        e, Tf.ErrorException
                ) and "Failed to load plugin 'rmanOslParser'" in str(e):
                    pass  # we won't log error "GL error: invalid operation"
                else:
                    log.error(e)

            if self.renderer.IsConverged():
                break

            self.renderer.GetRendererAov('color', image.ctypes.data)
            update_render_result()

        self.renderer.GetRendererAov('color', image.ctypes.data)
        update_render_result()