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
Tags:
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:
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.
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.
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:
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
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!
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by