Get current raytraced viewport iteration

Hi guys I am trying to take some renders from the Cycles render engine by calling some functions from GH, so as of know I am able to start the viewport in Raytraced mode (from within a C# Grasshopper component) and by calling a Rhino command through Rhino.RhinoApp.RunScript take a screenshot. Think is that I am unable to get the current Cycles pass through C#, I’ve tried a couple of thing like using Component.ExpireSolution(true) for a while until it reaches a threshold so that I take the capture to file but that does not have any sort of information on real rendering data like if the framebuffer is available for capture (so sometimes I get artifacts) or on what current pass I am. Any thoughts?
My code so far:

  int counter = 0;
  void Capture(bool takeScreenShot, int maxPasses)
  {
    if (takeScreenShot)
    {
      var myView = RhinoDoc.ActiveDoc.Views.ActiveView;
      myView.ActiveViewport.DisplayMode = Rhino.Display.DisplayModeDescription.FindByName("Raytraced");
      var cycles = myView.RealtimeDisplayMode;
      var oldpasses = cycles.MaxPasses;
      cycles.PostEffectsOn = true;
      cycles.MaxPasses = 10;
      cycles.Paused = false;

      var viewCapture = new Rhino.Display.ViewCapture();
      viewCapture.Width = myView.ActiveViewport.Size.Width;
      viewCapture.Height = myView.ActiveViewport.Size.Height;
      viewCapture.ScaleScreenItems = false;
      viewCapture.DrawAxes = false;
      viewCapture.DrawGrid = false;
      viewCapture.DrawGridAxes = false;
      viewCapture.TransparentBackground = false;
      viewCapture.RealtimeRenderPasses = 10;

      var pipeline = myView.DisplayPipeline.RenderPass;

      int prevHash = -10000;

      if (counter < maxPasses)
        //if (cycles.)
      {
        Component.ExpireSolution(true);
        int currentHash = viewCapture.GetHashCode();
        counter = currentHash == prevHash ? counter : counter + 1;
        Print("Counter: {0}", counter);
        Print("Current Frame: {0}", currentHash);

        prevHash = currentHash;
      }
      else
      {
        string filename = string.Format("{0}_{1}.png", "C:\\Users\\Felipe\\Documents\\", "Capture");
        if (!System.IO.File.Exists(filename))
        {
          Rhino.RhinoApp.RunScript("_-ScreenCaptureToFile C:\\Users\\Felipe\\Documents\\Test.png", true);
        }
        else
        {
          Print("File exists");
        }
      }
    }
    else
    {
      counter *= 0;
    }
  }

I am kind of well versed on graphics programming so I wouldn’t mind having to add stuff to the Cycles project if needed but I do need some help in going the right way. @jesterking
Many thanks by the way.

Use RealtimeDisplayMode.LastRenderedPass Method

Instead of asking for the display mode description you need to get RhinoView.RealtimeDisplayMode Property from myView. That gives you access to LastRenderedPass.

Note that RealtimeDisplayMode property will give null if the view you’re querying it from is not a realtime display integration (including any third-party engine integrations).

Thanks for the answer, I figured out just a few moments before your post. Now I am having trouble being able to call viewport capture, it seems to be deadlocking (not sure as I haven’t tried to run any threading to test if that’s it), screen capture to file does work (running Rhino commands), same happens when trying to do it directly in GH by saving the bitmap. I’ve tried the .IsComplete() method but no luck either. I mean after the passed count is achieved, also tried to call .pause = true before the viewport capture and also run into a full lock of GH and Rhino.

So you were trying to use _ViewCaptureToFile first but that doesn’t work? Or were you trying to do the capture using the ViewCapture class? If the latter you’ll have to make sure you do that on the main thread (RhinoApp.InvokeOnUiThread Method) - commands are always run on the main thread.

Both crash even when ran from other threads or using the OnUiThread method.

Can you share a self-contained minimal example file that exhibits this? I’d rather not guess how you actually implemented this if I could just see and test the code directly.

FirstPrototype_14.gh (11.6 KB)

This appears to be working just fine for me. Both the screenshot and the viewport capture methods ended up with a saved image file to my desktop (after I adjusted the paths first, of course)

Thanks for testing, then the issue might be running Rhino on bootcamp on Mac. I’ll try on another machine. I attach my specs as this might be a bug?

<admin snip>blank out your serial number before upping such an image<end admin snip>

I’d be surprised if that is the actual problem. I tested your GH definition on my Mac M1 in Rhino for Mac.

I think that probably you have ran _-ViewCaptureToFile before and maybe the command is remembering some settings there that cause it to rerender everything to capture the view. This will lock Rhino until it is done.

Instead of using _ViewCaptureToFile you may want to think about just doing a _Render and _SaveRenderWindowAs, since both Raytraced and Rhino Render are the same engine.

That way you don’t have to have your viewport of interest in Raytraced mode and you’ll still get the final rendered result.