SaveRenderWindowAs question

Oh really?
Not sure if I understand though:

If (while) the render finishes, save it.
Otherwise wait 10 seconds (what for?) then carry on (pass) …
But even if the render is not finished, there will be a partial rendering in the vfb so it will be saved, will it not?

So in what situation the script would not wait to finish the render in the loop anyways ?

To cancel the script I use:

if sc.escape_test(False):
            print "ESC pressed. Rendering stopped. "
            return

If I want to stop the renderings I cancel the render progress window and press ESC and it doesn’t save.

The while loop above tests every 10 milliseconds if the render has finished. If not, HasRenderFinished() returns False, once the render has finished it returns True. So the while loop just does nothing until it gets a True, and then the script completes to save the image. The pass is not really required if rs.Sleep(10) is there.

To get out of the while loop (and the whole function) earlier by pressing ESC, it would look like this i guess:

rs.Command("!_Render", echo=False)
while not VRayForRhinoNETInterface.VRayInterface.HasRenderFinished():
    if scriptcontext.escape_test(False):
        return
    rs.Sleep(10)

Note that it does not stop the render, it just exits the current function of the script without saving the unfinished picture. I´m wondering if there is a command or method to stop the render ?

c.

I get it now. Thanks.
Although I like better your method to get out of the loop, and I mean the whole script loop of renders.
Right now my sc.escape_test doesn’t really check at intervals, it’s only somewhere before the render command and picks up the ESC at the first chance which is not very clean because if the rendering has started it’s too late and I have to cancel the rendering by hand first.
I thought that rs.Command ("_-CloseRenderWindow") would cancel the rendering but it doesn’t. The rendering continues even if it’s not being displayed.
After pressing ESC and stopping the rendering manually eventually stops.
If I press ESC and wait for the first render to finish, they the script stops and no file is saved.
Ideally I should press ESC and the script stop even in the middle of the rendering but it doesn’t.
Here is the snippet. See if you can make it work.
Note that this is one of the iterations within a for i in range (…) started earlier where the range is the list of views to render:

EDIT: It works now (almost) ! The trick was to turn off the Batch render option in the Global switches. Usually this needs to be enabled for batch rendering but it was disabling the ESC input somehow. The script runs fine even though the option is off. Now pressing ESC in the middle of the render, the script exits. The current rendering continues but no image will be saved and the script has stopped. To be perfect there should be a way to cancel via the render progress window. Still not 100% but good enough I suppose. … Bedtime.

Juan

...
...
# Render view and save:
        print "Rendering " + current_view + "..."
        rs.Command("!_Render", echo=False)
        while not VRayForRhinoNETInterface.VRayInterface.HasRenderFinished():
            if sc.escape_test(False):
                print ("Rendering cancelled")
                return
            rs.Sleep(10)
        # Calling the save function with two arguments: file and switch (1 or 0) for saving only RGB or all channels
        SaveVFB (file,saveRGB)  
       
    print "Finished."

def SaveVFB(file, saveRGB):   # It saves VfB contents to a file
    plugin = LoadPlugin("V-Ray for Rhino")
    clr.AddReferenceToFileAndPath("C:\ProgramData\ASGVIS\VfR564\VRayForRhinoNETInterface.dll")
    import VRayForRhinoNETInterface
    VRayForRhinoNETInterface.VRayInterface.SaveVFBRenderToPath(file, saveRGB)   # 1: Only RGB ; 0: All channels
    print("Saving " + file)
    return

I noticed the same and think you found two bugs. When batchrender is OFF, and the render is interrupted manually by closing the VFB renderwindow, HasRenderFinished() returns True and the unfinished content of the VFB is saved. On the other side, if you add _CloseRenderWindow right after the ESC press, the VFB is closed but it continues to render, then saves the image.

To understand what the batchrender option does, it forces the focus on the VFB until the render is finished, so an ESC press cannot get fired at all.

c.

Changed it a bit and moved the ESC check in to the saving sub. I have it working now.
Here are the scenarios:
1 - Cancelling with ESC, terminateds the script, does NOT save the file, but the current rendering continues until finished. No big deal as it doesn’t save. I can just click and cancel the progress rendering window.
2 - Canceling by closing the vfb or by cancelling the render progress status window saves vfb and continues to the next rendering. Not ideal but why would I do that when ESC works better.
So really, the function

HasRenderFinished()

is kind of misleading because the name suggests the rendering being completed when it actually responds to any kind of rendering ending wheather complete or not.
I think I spent enough time with this trying to save one or two mouse clicks.
Unless you find something better, I’ll live with it. :slight_smile: Thanks.

def SaveVFB(file, saveRGB):   # It checks for ESC and saves VfB contents to a file
    plugin = LoadPlugin("V-Ray for Rhino")
    clr.AddReferenceToFileAndPath("C:\ProgramData\ASGVIS\VfR564\VRayForRhinoNETInterface.dll")
    import VRayForRhinoNETInterface
    esc = False
    while not VRayForRhinoNETInterface.VRayInterface.HasRenderFinished():
        if sc.escape_test(False):
            print ("Rendering cancelled")
            esc = True
            break
        rs.Sleep(10)
    if esc: return esc    
    VRayForRhinoNETInterface.VRayInterface.SaveVFBRenderToPath(file, saveRGB)   # 1: Only RGB ; 0: All channels
    print("Saving " + file)
    return esc

Very good :wink: I did include the HasRenderFinished() in my script only because i had batchrender disabled. I think this method should never return True when the render has been canceled by closing VFB manually.

@matt_newberg, maybe this can be changed in future versions of VRayForRhinoNETInterface along with an additional method to stop a rendering via script call. I found it not logical that the render stops if VFB (the render window) is closed manually but continues to render if we call _CloseRenderWindow.

c.

1 Like

Small improvement after finding the function CancelRender() with OleWoo function browser.
(http://www.benf.org/other/olewoo/) to open the file in
(C:\ProgramData\ASGVIS\VfR564\VRayForRhino.tlb).

In progress render stops with ESC :smile:
Still no joy cancelling by closing the vfb …

...
while not VRayForRhinoNETInterface.VRayInterface.HasRenderFinished():
        if sc.escape_test(False):
            print ("Rendering cancelled")
            esc = rs.GetPlugInObject("V-Ray for Rhino").CancelRender()
            break
        rs.Sleep(10)
1 Like

Thanks for pointing that out. I´ve put this now after _CloseRenderWindow which works nice.

I guess this is because HasRenderFinished() returns True

c.

Actually something is not allowing to break out of the main loop … Notice the cursor arrow shape and it doesn’t return to the Python editor. Ctl-C resumes the script …
I can’t see what’s wrong. It only happens when I use the CancelRender() method

@juancarreras, the break will just get you out of the loop it runs in, so the script will continue with everything after that loop. But i guess i need to see the whole script to understand what is wrong.

btw. when the VFB is closed manually during the render, i see no way for the script to do anything (because HasRenderFinished() will return a True) except when batch rendering is on. But this would mean that the ESC check will not run anymore.

c.

I’m sending you the whole thing via PM.
Sorry about bugging you so much but I really need this once and for all. I appreciate your patience.
Read the header of the main script to see what it does.
Thanks.

Hi clement … yeah, me again.
Not sure about you but I can’t never seem to let go of unfinished business … ha, ha
I played around again with trying to cancel the ongoing render after cancelling the script with ESC.
Well, I solved the problem of the script freezing when using vray.CancelRender()
The batch option is set to off (False) before the script starts rendering to accept possible ESC calls.
The trick is controlling the focus temporarely during the command CancelRender()

while not VRayForRhinoNETInterface.VRayInterface.HasRenderFinished():
        if sc.escape_test(False):
            print "Render cancelled"
            rs.Command("_CloseRenderWindow", echo=False)
            rs.GetPlugInObject("V-Ray for Rhino").SetBatchRenderOn(True)
            vray.CancelRender()
            rs.GetPlugInObject("V-Ray for Rhino").SetBatchRenderOn(False)
            esc = True
            break
        rs.Sleep(10)

Now the script stops nicely and the current render stops also. Ok now I’m happy … :slight_smile:

Neva, neva give up.
Sir W.Churchill

1 Like

Hi @juancarreras, cool workaround to make CancelRender() behave when batchrender is off :slight_smile:

I´ve changed it as below in my script and it works too:

vray.SetBatchRenderOn(True)
vray.CancelRender()
vray.SetBatchRenderOn(False)

c.

In my globals I have:

import rhinoscriptsyntax as rs
import Rhino as rc

Then later I have defined the vray name as:

vray = rc.RhinoApp.GetPlugInObject("V-Ray for Rhino")

How come if I replace
rs.GetPlugInObject("V-Ray for Rhino") with vray
resulting on
vray.SetBatchRenderOn(False)

the script still works fine even though rc is not the same than rs … ?

@juancarreras,

vray is an instance of the plugin object. rs.GetPluginObject() just calls

Rhino.RhinoApp.GetPlugInObject(plug_in)

take a look for the RhinoScript function here .

c.

Thanks, I’ve been looking for these definitions for a while.

You might find them on your system too:

C:\Users\USERNAME\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

c.

oh, I didn’r know that ! cool

Hi @matt_newberg
It’s been a while since you helped me out with this.
Somehow the script that I had working since we were in this thread, stopped working and I can’t figure out why. I get no errors but no file is written to disk.
As you might recall, you provided a newer VRayForRhinoNETInterface.dll where the function SaveVFBRenderToPath was included. I used it to be able to write only the RGB channel reather that all the channels in my batch render script. It used to work fine at the time. Even I’ve saved again the dll from the zip file in case that has been overwritten with a more recent vray update but still something is wrong.
If I run for example the sample script that you provided (further up in the thread on March 13) no file is written to the desktop but like I said, there’s no error either.
Do you know why this could be happenning ?
Thanks