# GetPlugInObject not working for VBScript?

**URL:** <https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136>\
**Category:** Scripting\
**Created:** [June 27, 2015, 1:42pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136 "2015-06-27T13:42:23Z")\
**Posts on this page:** 20\
**Page:** 2

<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 29, 2015, 1:35pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/21 "2015-06-29T13:35:38Z")

</div>

Sorry @clement, I didn’t realize. I just tried GetSceneMaterialNames (in VB); I am getting a type mismatch error, whoa

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 29, 2015, 2:10pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/22 "2015-06-29T14:10:36Z")

</div>

> [@matt\_newberg](#):
>
> GetSceneMaterialNamesScript stores the array of strings slightly differently. I believe I wrote it to get around an issue with PythonScript/Rhinoscript.

Hi Matt, i do not get both to work, GetSceneMaterialsScript gives me “unsupported property or method” and using GetSceneMaterials i get a VarType of 8200:

```vbScript
    materials = Null
    Call vRay.GetSceneMaterialNames(materials)
    MsgBox VarType(materials)

```

![](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/d/cd69948740c7b88f2bc7db4d28f944991ed7ecf2.png)

If i try to access materials(0) i get a “type conflict”. What VarType is 8200 ?

- 

c.

---

<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:** [June 29, 2015, 2:16pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/23 "2015-06-29T14:16:17Z")

</div>

8200 would be VarType ( VT\_ARRAY | VT\_BSTR ). While GetSceneMaterialNamesScript outputs VarType (VT\_ARRAY | VT\_VARIANT). This was the reason I wrote up this different function to get around that error.

Which version of V-Ray For Rhino / V-Ray For Matrix are you running and getting the said error with GetSceneMaterialNamesScript?

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 29, 2015, 2:20pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/24 "2015-06-29T14:20:56Z")

</div>

Hi Matt, V-Ray For Rhino 1.5 Edu. I remeber to have searched how to access ByteString in rvb in the past without success.

- 

c.

---

<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:** [June 29, 2015, 2:26pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/25 "2015-06-29T14:26:29Z")

</div>

@clement

GetSceneMaterialNamesScript was added in the builds from Mid July 2014.You will have to get a newer build to use that function.

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 29, 2015, 2:52pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/26 "2015-06-29T14:52:51Z")

</div>

> [@krsnadas](#):
>
> vRay.ApplyVRayMaterialToObjects thisStr, 1, strObjects

Possible typo, strObjects should be strObject. If i understand it correctly, it should be an array of objects but i cannot test it as i do not get past the GetSceneMaterialNamesScript method at this point.

Btw. you might use `Option Explicit` to avoid writing variable names which have not been declared before.

- 

c.

---

<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 29, 2015, 3:17pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/27 "2015-06-29T15:17:36Z")

</div>

@clement can you try running this code instead (which uses Rhino.MaterialName instead)

```
    f = "C:\WhiteReflective.vismat"
    Call Rhino.Command("-visLoadVismat " + f)
    Dim arrMaterials: arrMaterials = Rhino.MaterialIndices
    Dim strObject: strObject = Rhino.GetObject("select")
    Dim vRay, mat_name, i, name, f, mat_index, materials, str_name, thisStr, obj_index, newIndex
    Set vRay = Rhino.GetPluginObject("{E8CFE179-B60C-411A-8416-62A893334519}")
    mat_name = vRay.GetMaterialNameFromVismat(f, False)
    str_name = "WhiteReflective"
    For i = 0 to UBound(arrMaterials)
        thisStr = Rhino.MaterialName(arrMaterials(i))
        If InStr(thisStr, str_name) <> 0 Then
            Exit For
        End If
    Next
    obj_index = Rhino.ObjectMaterialIndex(strObject)
    vRay.ApplyVRayMaterialToObjects mat_name, 1, strObject
```

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 29, 2015, 5:26pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/28 "2015-06-29T17:26:57Z")

</div>

Can you describe what are you trying to accomplish with your script ?

I´ve tried to run it and get the same “unsupported property or method” error using VRay.ApplyMaterialToObjects. But i guess this is because i´m using the older VRay version.

- 

c.

---

<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 29, 2015, 7:32pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/29 "2015-06-29T19:32:50Z")

</div>

Ah, that’s a shame. Well, what I’m trying to achieve is the supremely elusive and seemingly vindictive task of assigning a material to an object. In this case, I have added a ground plane right before this code snippet and here I’m trying to assign add the WhiteReflective material to it.

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 29, 2015, 8:35pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/30 "2015-06-29T20:35:07Z")

</div>

Well, it is clear that you finally want to assign the material 😄 what i do not understand in your last script is:

1. Why are you getting the index and do not use it in your script ?
2. Do you get the same type error like i have, when using ApplyVRayMaterialToObjects ?
3. Why the for loop when you do not use the variable thisStr ?

- 

c.

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 29, 2015, 8:47pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/31 "2015-06-29T20:47:20Z")

</div>

Here is my workaround when ApplyVRayMaterialToObjects is not working:

```auto
Option Explicit

Call Main()
Sub Main()
    
    Dim f, strObject, rid, vRay, mat_name, cmd, result
    
    f = "C:\White Plastic.vismat"
    Call Rhino.Command("-visLoadVismat " + CHR(34) + f + CHR(34))
    
    strObject = Rhino.GetObject("select object", 8+16+32, True, True)
    If IsNull(strObject) Then Exit Sub
    
    rid = "{E8CFE179-B60C-411A-8416-62A893334519}"
    
    Set vRay = Rhino.GetPluginObject(rid)
    If Not IsObject(vRay) Then Exit Sub
    
    mat_name = vRay.GetMaterialNameFromVismat(f, True)
    If len(mat_name) = 0 Then Exit Sub
    
    cmd = "-visApplyMaterial " + CHR(34) + mat_name + CHR(34)
    result = Rhino.Command(cmd, False)
    If result = False Then Rhino.Print "error assigning material"
    
End Sub

```

Note that i have a different material and that i put the material name (mat\_name) in extra quotes. If you would not do this it would fail using a material name with spaces in the name.

- 

c.

---

<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 30, 2015, 8:20am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/32 "2015-06-30T08:20:38Z")

</div>

@clement I don’t know if I’m doing something wrong but visApplyMaterial does not seem to do the job for me. The script executes fine, but the material does not seem to apply in the render. So far, I’ve only been able to assign material successfully with rs.CopyMaterial (that was in Rhino Python). Weirdly, when I try Rhino.CopyMaterial (or Rhino.MatchMaterial), I get this error message ‘Type mismatch in parameter. Float required’. Float required?! Aren’t the indices of integer type?

---

<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 30, 2015, 9:21am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/33 "2015-06-30T09:21:12Z")

</div>

Alright, got this to work. The issue was that Rhino.MaterialIndex needs a material identifier (Rhino.MaterialId) as parameter and not a material name.

```
   Dim vRay, mat_name, i, name, f, mat_index, materials, str_name, thisStr
   f = "C:\WhiteReflective.vismat"
   Call Rhino.Command("-visLoadVismat " + f)
   Dim idMaterials: idMaterials = Rhino.MaterialIds
   Dim indexMaterials: indexMaterials = Rhino.MaterialIndices
   Dim strObject: strObject = Rhino.GetObject("select")
   Set vRay = Rhino.GetPluginObject("{E8CFE179-B60C-411A-8416-62A893334519}")
   mat_name = vRay.GetMaterialNameFromVismat(f, False)
   str_name = "WhiteReflective"
   For i = 0 to UBound(indexMaterials)
        thisStr = Rhino.MaterialName(indexMaterials(i))
        If InStr(thisStr, str_name) <> 0 Then
            mat_index = Rhino.MaterialIndex(idMaterials(i))
            Exit For
        End If
    Next
    If IsNull(mat_index) Then
        Rhino.Print("Wow, Null")
        Rhino.Print("But looks like we have" + thisStr)
    Else
        Rhino.Print("I'm Neo" + CStr(mat_index))
    End If
    Rhino.ObjectMaterialIndex strObject, mat_Index
```

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 30, 2015, 9:53am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/34 "2015-06-30T09:53:35Z")

</div>

> [@krsnadas](#):
>
> visApplyMaterial does not seem to do the job for me

I forgot to mention that -visApplyMaterial only works if your object(s) are selected beforehand. If nothing is selected it will just run through doing nothing. If it assigns the material properly, -visApplyMaterial reports the success to the commandline as well.

Yesterday i´ve tried something which was identical using your last code snippet above but i´ve found something strange: If i get the material index using RhinoScript methods and assign that index to the object, the material name gets assigned as usual, the plugin box gets checked in properties BUT: the edit Button stays greyed out. Somehow this cannot be right. So i´ve done the version above using -visApplyMaterial which seems to fix that.

So as a last comment to your last code, i do not think that it is required to get the index, then compare the material name with all scene material names and finaly assign it. The VRay material name is enough to assign it, at least with the version using -visApplyMaterial. Downside is, it requires the object selection.

- 

c.

---

<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 30, 2015, 10:26am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/35 "2015-06-30T10:26:50Z")

</div>

I’ll have to check that; I haven’t observed the edit button. Somehow, visApplyMaterial doesn’t work for me and I’m not going to investigate it further. Much thanks for your inputs, @clement. Before we seal this thread for good, do you know of any way of assigning a material from the material library without having to call visLoadVismat?

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [June 30, 2015, 10:43am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/36 "2015-06-30T10:43:43Z")

</div>

Please try this first:

1. Create a few objects in a new scene
2. Assign a few different vray materials to them
3. Create a surface and select it
4. type visApplyMaterial (without the - sign in front)
5. A dialog pops up, showing all scene materials
6. Choose one to assign it to the surface, click ok
7. Look into properties / material and check for:

A: Is the material name assigned ?  
B: Is the plugin box checked ?  
C: Is the edit button clickable or grayed out ?

In my case, A B C all work using `visApplyMaterial`. I can do the same using a script, if i put the - sign in front of `-visApplyMaterial`, the dialog is suppressed, then i can simply type the material name eg. “\White Plastic”. If the material has spaces in it, i need to type this in quotes. Note the \ in front of the material name. In your last script, you´re just missing this, you might print out the results of your variables to find such errors faster and handle them.

If i run your last posted script, i do not get any of A, B, C, no material gets assigned at all. Using `visApplyMaterial` is the easiest method to get all things in one go. If you do not use it, you have to control (script) the material source (ByObject, ByLayer) as well. Your script does not do that.

In general, it is required to understand what material indices are. A material index can be any positive integer, including zero.

To assign one material from the scene materials, you might use the vRay.GetSceneMaterialNamesScript method and pop up a listbox filled with the `materials` array. Then assign the choosen material using visApplyMaterial. Note that the object(s) to assign to need to be selected beforehand. If not, it will not work. Just enter “visApplyMaterial” to the commandline and read what it prints to the command line.

- 

c.

---

<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:** [September 7, 2015, 8:37am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/37 "2015-09-07T08:37:14Z")

</div>

Thanks for your reply, @clement. I am just getting around to trying this out.

---

<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:** [September 9, 2015, 10:42am UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/38 "2015-09-09T10:42:54Z")

</div>

@clement have you had any success working with V-Ray scripting methods for Rhinoscript/Python? I seem to be having the same issues still, so I have stopped wrangling with them and I’m using Rhinoscript methods for material calling.

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [September 9, 2015, 2:58pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/39 "2015-09-09T14:58:43Z")

</div>

> [@krsnadas](#):
>
> any success working with V-Ray scripting methods for Rhinoscript/Python?

Yes, below one example how to apply a **loaded** VRay materials using material indices. If no materials have been loaded, nothing will happen. Short description:

- Get all non deleted material names and their material indices
- put them in dictionary to remove duplicates
- open listbox to select one material by name
- ask for objects to apply picked material (ByObject) using its index

```python
import rhinoscriptsyntax as rs
import scriptcontext

def ApplyLoadedVRayMaterial():
    vRay = rs.GetPlugInObject("E8CFE179-B60C-411A-8416-62A893334519")
    if not vRay:
        print "VRAY not found or not loaded !"
        return
    
    d = {}
    materials = scriptcontext.doc.Materials
    for m in materials:
        if not m.IsDeleted: 
            d[m.Name] = m.MaterialIndex
    if len(d) == 0: return
    
    mat_name = rs.ListBox(d.keys(), "Select", "Title")
    if not mat_name: return
    
    ids = rs.GetObjects("Objects", 8+16+32, False, False, False)
    if not ids: return
    
    rs.ObjectMaterialSource(ids, 1)
    for id in ids: rs.ObjectMaterialIndex(id, d[mat_name])
    
    print "Applied {0} to {1} objects".format(mat_name, len(ids))

if __name__ ==" __main__":
    ApplyLoadedVRayMaterial()

```

## tested with Rhino5 SR12 and VRay 2.00.25962 (x64).

c.

- 

c.

---

<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:** [September 9, 2015, 6:45pm UTC](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136/40 "2015-09-09T18:45:43Z")

</div>

This looks great! But you haven’t used any of the actual vRay scripting methods, have you? I mean like GetSceneMaterialNames and others listed in the VRayforRhinoExports file?

[Previous page](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136.md?page=1)

[Next page](https://discourse.mcneel.com/t/getpluginobject-not-working-for-vbscript/22136.md?page=3)
