Grasshopper

algorithmic modeling for Rhino

Still a beginner in VB.NET.  Trying to write some tools to augment Giulio's great rendering component.  I have gotten as far as extracting VRay materials/names from the document object.  

Now I would like to use a single VRay material as a template for creating multiple identical materials with different colors within the GH environment (instead of creating manually in the document).  

I have gotten as far as creating the materials.  Now I need to add them to the document material table so that they can be used with Giulio's rendering component (which looks for either Rhino.Display.DisplayMaterial or a String that references a document object).  I'm not going to learn C# to modify his script, so I am catering to its demands.  

Private Sub RunScript(ByVal M As Object, ByVal C As Color, ByRef Mat As Object)


Dim mTemp As Rhino.DocObjects.Material
mTemp = CType(M, Rhino.DocObjects.Material)
If mTemp.Name.Length > 0 Then
mTemp.DiffuseColor = C
Dim nTemp As String = mTemp.Name & "_" & C.R & "_" & C.G & "_" & C.B
mTemp.Name = nTemp
End If

Rhino.DocObjects.Tables.MaterialTable.Add(mTemp)
Mat = mTemp

End Sub

The code throws the error: Reference to a non-shared member requires an object reference. (line 96)

Do I understand that the material has to be assigned to a particular object in order to enter the Material Table?  Can I assign it to a Layer instead? Any ideas?  A better way to do this?

Thanks,

Marc

Views: 9537

Replies to This Discussion

Also, I noticed that Giulio's component has a type hint defined as a Material, even though that does not appear in the type hint list outside the component.  How was that done?  I can't figure out how to cast the input as a Rhino.DocObjects.Material, so you can see that I have cast it as a compatible type in the first 2 lines... is there a cleaner way?


Thanks,

Marc

Hi Marc,

 

the script was written a looong time ago. It would be fair to call it "dinosaur old" in Grasshopper ages. I've just fixed the broken parts now and then, when it was necessary.

There are quite some implications and questions so I will go one by one:

 

  1. "Now I would like to use a single VRay material as a template for creating multiple identical materials"
    I hope this will work, but as VRay does not expose any SDK, I would not guarantee any specific result.

  2. "Now I need to add them to the document material table"
    This is done with a reference to a document instance, such as the one you get with the code doc.Materials (both in C# and Vb.Net).

  3. "I'm not going to learn C# to modify his script"
    That's a pity, it would be nice to pass on this troublemaker to somebody else! :)
    Btw, C# and Vb.Net are very very similar. This script could be written in Vb.Net too.

  4. "Reference to a non-shared member requires an object reference. (line 96)"
    This only means that you need to access the Materials property on an instance, not on the type (class) name. Change that line using what is written at point 2.

  5. "Do I understand that the material has to be assigned to a particular object in order to enter the Material Table?"
    No it does not. But if you call the _Purge command it will be removed if it does not have an object that references it.

  6. "Can I assign it to a Layer instead?"
    You do not need to. But this would be achieved with doc.Layers[whichLayer].RenderMaterialIndex = materialIndex; in C# or
    doc.Layers(whichLayer).RenderMaterialIndex = materialIndex in Vb.Net.

  7. "Any ideas? A better way to do this?"
    If you found a way to bypass the VRay SDK not being there, this should work.

  8. "Giulio's component has a type hint defined as a Material"
    It does not any longer. The hint was there in earlier versions of Grasshopper, but now the hint has disappeared. This is not so bad, and it is also the only way you would be able to use either a Material instance already or a string for a material name.

  9. "How was that done?"
    Probably it was done in an older version of Grasshopper. But which version are you using?

  10. "I can't figure out how to cast the input as a Rhino.DocObjects.Material, so you can see that I have cast it as a compatible type in the first 2 lines... is there a cleaner way?"
    That sounds like a good way actually. Be sure your component responds properly when something wrong is inputted, though.
    Dim mTemp As Rhino.DocObjects.Material = CType(M, Rhino.DocObjects.Material)
    in one line might also work. See msdn for more conversion operators and functions.

I hope this helps,
 

- Giulio
_______________
giulio@mcneel.com

Giulio --

Thank you for the amazingly detailed reply.  I have gotten the script working *nearly flawlessly* with your notes.  The problem now is that all of the materials appear to be generating correctly except for the first material in a series.  See attached image... Here is my code:

Private Sub RunScript(ByVal M As Object, ByVal C As Color, ByRef AddName As Object, ByRef AddMat As Object, ByRef AddBool As Object, ByRef baseName As Object, ByRef newMatName As Object)

Dim z As String = "newMatName"
Dim y As String = "BaseName"
Dim x As Integer = 0
Dim nRestore As String
Dim mTemp As Rhino.DocObjects.Material

mTemp = CType(M, Rhino.DocObjects.Material)
y = mTemp.Name
Dim nTemp As String


If mTemp.Name.Contains("_MOD_R") = False Then

nRestore = mTemp.Name
nTemp = mTemp.Name & "_MOD_R" & C.R & "_G" & C.G & "_B" & C.B
mTemp.Name = nTemp
z = nTemp
mTemp.DiffuseColor = C

If Doc.Materials.Find(nTemp, True) < 0 Then

Doc.Materials.Add(mTemp)
x = x + 1
AddName = nTemp
AddMat = mTemp


End If

mTemp.Name = nRestore

End If


newMatName = z

AddBool = x
BaseName = y

End Sub

1) I have checked that all of the materials I am calling by name exist in the document and that data matching is correct.  There doesn't seem to be anything special about the offending material except that it is always the first material that was added to the document by my script. 

2) The main thing I was missing in the previous script was the "doc.Materials.Add()" -- how on earth should I have known that existed?  Even a search for "doc.Materials" in the Rhinocommon SDK doesn't turn that up.  I'm having a very hard time using the SDK to my advantage, it seems not to correlate to the actual code I need to write. 

2b) Perfect example... now I am trying to rewrite my other component (which exposes all of the document materials) to set a few objects manually in Rhino with the Materials I want to use as templates.  Now I am trying to find out how to access the material assigned to an object. Seems easy, but it's clearly not a Property, and I can't find an appropriate Method in either the Objects or Materials classes.

3) One of my problems originally, when feeding the component one material and multiple colors, was that the nTemp variable was not resetting properly for the second color.  Same thing if I duplicated the material to match the list of colors.  It would create a material on the first pass but concatenate "_MOD_R_G_B" in each subsequent pass and be caught by my String checker.  Why is that?  I thought that the nTemp Name variable would be reset in each pass by the line "mTemp = CType(M, Rhino.DocObjects.Material)" and "nTemp = mTemp.Name" combination.  

Does the mTemp material somehow carry over its properties in each successive pass?  That's why I added the nRestore to be sure each pass reset the name back to the original.

Still, I wonder if there is some problem with the way I am conceptualizing this that is causing the first material to be the same as the input material.

Thanks for your help on this...

Cheers,

Marc

Hey Marc,

You did not answer this: "But which version [of Grasshopper] are you using?"
Also, can you please post the file with the sample you have so far?

doc is a member (a field) of the script, it should not be in the SDK. Look in the script code to see where and how it is defined.

Thanks,

- Giulio
________________
giulio@mcneel.com

Sorry... I'm using .0051

Attached is the definition with some notes, since it is a really messy work in progress.  For the animation portion, you will need Centipede installed.  Attached.

Attachments:

Also the 3DM so you can see exactly which materials I'm working with... I'm using Vray 1.5 for Rhino 5 64-bit, FYI.

Attachments:

Hi Giulio --

I spent a lot of time this weekend trying to get this right.  I upgraded to GH0.8.0063, among other things (for a number of reasons), and I can add some new information:

1) This script only remotely works with Rhino 4 Vray 1.0 and Rhino 5 64-bit + Vray 1.5.  Something about the naming convention in Rhino 5 32-bit + Vray 1.5 is not registering properly with the way I am naming/calling materials.

2) I got it to work for all generated materials, but only sporadically.  There's no rhyme or reason to when or why it works.  Here's a link to the results:

http://vimeo.com/34728433

3) There's obviously an issue with the material index numbers in the document.  See the attached definition for added notes (renders previous definition obsolete).  Index numbers don't seem to match up between my components and your component, yet I get positive results.  Also, after purging the document many times during testing, material index numbers can get up into the tens of thousands.  I see performance degradation eventually as well.

Anyway, thanks for any help you could give me on this.  I'm really hoping to get this code tightened up so I can use the tools in a production environment and not have to worry about it being unreliable...

Marc

Attachments:

Hey Giulio... did you have a chance to check out my code?  I burned a lot of hours trying to troubleshoot, but didn't get any further.  Any ideas?

Thanks,

Marc

Hey Marc, would be interested to know if you managed to resolve the few issues?

Hi Wiktor

I hope Marc will find a moment and tell you how if he managed to make this work, but I am not very sure about this myself. I remember I had emailed twice VRay support to get to talk to someone able to help, but I received no answer till April. Then, I do not think anyone else at McNeel received anything either.

I am not sure about SDK development for VRay at this point -- maybe it will be just necessary to find some workarounds...

Well, this is what I know,

Giulio

--

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hey, just found out you were successful in handling Vray Materials. I'd like to add this to Scarab

Handling of V-Ray materials in Grasshopper would be great!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service