A bit more about V-Ray scripting

Hello everyone,
especially @matt_newberg,
I wanted to make my own version of VRay animation from grasshopper, but there are some closed doors for me.
I have something like this:

Private Sub RunScript(ByVal Run As Boolean, ByVal Folder As String, ByVal W As Integer, ByVal H As Integer, ByRef A As Object) 


Dim Vid As guid = rhino.PlugIns.RenderPlugIn.IdFromName("V-Ray for Rhino")

Dim VRay = rhino.RhinoApp.GetPlugInObject(Vid)

Vray.SetRenderOutputSize(W, H)

' Vray.SetBatchRenderOn(True)
' Vray.SetShadowsOn(True)
' Vray.SetDistributedRenderingOn(True)
' VRAy.setRTEngineOn(True)
' VRay.SetAdaptiveDMCNoiseThreshold(0.1)
' VRay.SetAdaptiveDMCMinMaxSubdivs(10, 20)
' VRay.SetAdaptiveSubdivisionThreshold(0.1)
' VRay.SetAdaptiveSubdivisionNormals(1, 0.0)
' VRay.SetAdaptiveSubdivisionSamples(1)
' VRay.SetFixedRateSubdivs(10)
' VRay.SetDMCAdaptiveAmount(0.01)
' VRay.SetDMCMinimumSamples(10)
' VRay.SetDMCNoiseThreshold(0.01)
' VRay.SetDMCSubdivisionMultiplier(0.01)
' VRay.SetDMCPathSampler(0)

'Load and apply vismat.
For i As int32 = 0 To vismat.count - 1
  Dim idxof1 As Integer = vismat(i).LastIndexOf("\")
  Dim idxof2 As Integer = vismat(i).LastIndexOf(".")
  Dim namematerial As String = vismat(i).substring(idxof1 + 1, idxof2 - idxof1 - 1)


  'Dim find As int32 = rhino.DocObjects.Tables.MaterialTable.Find(namematerial, True)
  ' print(find) 'This return -1
  
If find <> -1 then
  Rhino.RhinoApp.RunScript("_-visLoadVismat " & vismat(i), True)
end if

'  rhino.RhinoApp.RunScript("_-visApplymaterial", True)
Next


If Run Then
  Rhino.RhinoApp.RunScript("_-Render", False)
  '   Rhino.RhinoApp.RunScript("_-SaveRenderWindowAs " & vbLf & """" + Folder + """" & vbLf, False) iter += 1
  '   
End If
end sub

So when I run the script, new materials (duplicate) appear. How do I avoid that? there is away to check if there is already a material with that name?

Other questions:

How render is executed from Vray. (…)?
How the materials are loaded and applied?

Thank you so much!

Have you tried using:
VRay.GetSceneMaterialNames(materials)
or
VRay.GetSceneMaterialNamesScript(materials)

They should be able to give you all the materials currently loaded.

I was unable to run the script you posted, there appears to be a lot code missing.

Thank you very much for your answer,
something I’m doing wrong:

    Dim materials As String()
    VRay.GetSceneMaterialNames(materials)

error: Los tipos no coinciden. (ExcepciĂłn de HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) (line: 0)

I am attaching the file so you can check quickly.

tryGHVRAYanimation.gh (13.6 KB)

And the functions to render and save image, you can share?

Will they be published someday all functions?

Beginner question yes…

Dim materials As String() = Nothing
VRay.GetSceneMaterialNames(materials)

Now works. But please @matt_newberg would be so kind as to tell me what are the functions to render, save image, load material, apply materials… ?? I would avoid using rhinoapp.runscript ()
Many thanks!

EDIT:

Could you tell me if there is also a function that determines if the render is complete or is in process?
Of course, I will post the script here and Gh forum when I finish it.

For save image you will want to call RunScript, this is what use in the BatchRender plugin.

ON_wString script = “”;
script.Format(L"-_SaveRenderWindowAs "%s"", pRender->GetImagePath());
RhinoApp().RunScript(script);

The code is C++, but you should be able to get the idea.

LoadVisopt, LoadVismat, LoadDirectoryOfVismats should all be in the RhinoScript interface you are currently uses.

Running visApplyMaterials should work fine run script. You would be amazed what you can do with running scripts. There is another solution which I will get to later on.

Ok, we currently do not have a RhinoScript function that does this, but we have had a C export that will give you the status of the renderer. In order to access this you can add a reference to the he attached wrapper DLL and call VRayInterface.HasRenderFinished() VRayForRhinoNETInterface.zip (11.1 KB) You can also call the same methods by manually pInvoking the needed functions with the code in the attached grasshopper file tryGHVRAYanimation_hasRender.gh (9.6 KB)

In both methods (DLL or code) I have also attached the methods for applying materials.

Edited: Documentation about these methods can be found in the file:
C:\ProgramData\ASGVIS\VfR564\VRayForRhinoExports.h

2 Likes

Wow, I did not expect so much contribution! Thank you so much, I have a lot to learn here :slight_smile:
But it’s bittersweet, because I’ve closed this project. The result is more superficial, but it works. Thanks anyway, sure for someone else will be very useful.

@matt_newberg, LoadVismat works pretty well for loading materials and applying them using a combination of scriptcontext.doc.Materials and rhinoscriptsyntax. The problem with LoadVismat in a script is the need for feeding a file path which gets tricky when working between multiple computers.

So I am wondering if there is a way to circumvent LoadVismat for adding a material to the material table?

Thanks!