# A bit more about V-Ray scripting

**URL:** <https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663>\
**Category:** Scripting\
**Created:** [May 22, 2015, 12:07pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663 "2015-05-22T12:07:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dani\_Abalde](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dani_abalde/32/759411_2.png) [@Dani\_Abalde](https://discourse.mcneel.com/u/Dani_Abalde)\
**Post date:** [May 22, 2015, 12:07pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/1 "2015-05-22T12:07:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![matt\_newberg](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/matt_newberg/32/13325_2.png) [@matt\_newberg](https://discourse.mcneel.com/u/matt_newberg)\
**Post date:** [May 22, 2015, 1:49pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/2 "2015-05-22T13:49:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dani\_Abalde](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dani_abalde/32/759411_2.png) [@Dani\_Abalde](https://discourse.mcneel.com/u/Dani_Abalde)\
**Post date:** [May 22, 2015, 3:00pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/3 "2015-05-22T15:00:05Z")

</div>

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](https://global.discourse-cdn.com/mcneel/uploads/default/original/9/c/9c04e74d1b2e71c1f8a3a647ba4fcc06bbb5a193.gh) (13.6 KB)

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

Will they be published someday all functions?

---

<div class="post-metadata">

**Author:** ![Dani\_Abalde](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dani_abalde/32/759411_2.png) [@Dani\_Abalde](https://discourse.mcneel.com/u/Dani_Abalde)\
**Post date:** [May 22, 2015, 4:50pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/4 "2015-05-22T16:50:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt\_newberg](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/matt_newberg/32/13325_2.png) [@matt\_newberg](https://discourse.mcneel.com/u/matt_newberg)\
**Post date:** [May 27, 2015, 3:29pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/5 "2015-05-27T15:29:13Z")

</div>

> [@Dani\_Abalde](#):
>
> save image

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.

> [@Dani\_Abalde](#):
>
> load material

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

> [@Dani\_Abalde](#):
>
> apply materials

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.

> [@Dani\_Abalde](#):
>
> a function that determines if the render is complete or is in process

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](https://global.discourse-cdn.com/mcneel/uploads/default/original/6/e/6ee9e0c308380b037026c872f606bd6acb0a7aa6.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](https://global.discourse-cdn.com/mcneel/uploads/default/original/8/4/8435ea55618d51121eb2d3a6fd06d29cc7022c01.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

---

<div class="post-metadata">

**Author:** ![Dani\_Abalde](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dani_abalde/32/759411_2.png) [@Dani\_Abalde](https://discourse.mcneel.com/u/Dani_Abalde)\
**Post date:** [May 27, 2015, 7:55pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/6 "2015-05-27T19:55:32Z")

</div>

Wow, I did not expect so much contribution! Thank you so much, I have a lot to learn here 🙂  
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.

> **[GH + V-Ray Render animation.](http://www.grasshopper3d.com/profiles/blogs/gh-v-ray-render-animation)**
>
> Hello everyone,
> 
> here you have a script to make animations with V-Ray for Rhino plugin, using materials in .vismat or .vrmat formats.
> 
> My thanks and appreci…

---

<div class="post-metadata">

**Author:** ![krsnadas](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/krsnadas/32/46010_2.png) [@krsnadas](https://discourse.mcneel.com/u/krsnadas)\
**Post date:** [June 23, 2015, 7:01pm UTC](https://discourse.mcneel.com/t/a-bit-more-about-v-ray-scripting/20663/7 "2015-06-23T19:01:43Z")

</div>

@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!
