Grasshopper

algorithmic modeling for Rhino

Hi everyone,

 

I am developing a component for which I need at some point to get the VolatileData of a referenced parameter. Now, if I try the param.VolatileData property, I assume that a reference to the parameter's actual VolatileData object is returned, so if e.g. the object gets cleared, the data is gone.

Is this correct? If yes, what would be a good practice of getting a copy of the volatiledata object and, say, keep it in persistent storage (a variable in a custom component or so)?

 

Thank you.

Views: 645

Replies to This Discussion

Hi Yiannis,

 

correct the VolatileData property returns the actual data inside the parameter. If you want to make a copy of it, you can use the Duplicate() or ShallowDuplicate() methods:

 

Dim copy As IGH_Structure = param.VolatileData.Duplicate()

 

Duplicate will copy the data structure and the data items, ShallowDuplicate only copies the structure. Note that unless you call Duplicate() the data items inside your local IGH_Structure might be shared with any number of parameters. Data is never supposed to be changed, but the SDK offers methods to do this. You should be able to get away with a shallow duplicate, provided you don't change the actual values of your IGH_Goo instances.

 

Not all data can be stored as persistent data. Breps, Curves and Meshes for example cannot be (de)serialized by Grasshopper, so you cannot store these types as persistent data. However, once you've made a copy of it, you can keep those instances around until Grasshopper+Rhino close down.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David,

 

Thank you for the propmpt and detailed answer.

 

I am trying to find the Duplicate() method, but it seems that it's not there. Meaning:

 

IGH_Structure copy = doc.FindParameter(guid).VolatileData.Duplicate();

 

will not work cause there doesn't seem to be such a method as Duplicate (or at least VS2010 doesn't want to show it!). Is there a specific type of VolatileData I should refer to?

 

I was using 8.0004 and upgraded to 8.0007 but still doesn't work.

 

Thanks again very much.

Ah yes, my bad. IGH_Structure doesn't define Duplicate and DuplicateShallow, these are defined only in the abstract class GH_Structure(Of T). So you'll need to cast your data to the proper type.

 

This type will vary depending on which parameter it is. If you know for example that you always link to a Param_Number, you can do this:

 

Dim data As GH_Structure(Of GH_Number) = DirectCast(param.VolatileData, GH_Structure(Of GH_Number)

data = data.DuplicateShallow()

 

Every parameter contains a unique data type, so if you want this to work on all parameters, I think you're better off writing a custom duplication function. Something along the lines of (just typing this from memory):

 

Dim volData As IGH_Structure = param.VolatileData

Dim dupData As New GH_Structure(Of IGH_Goo)

For Each p As GH_Path in volData.Paths

  Dim list As List(Of IGH_Goo)

  For Each item As IGH_Goo in volData.Branch(p)

    If (item Is Nothing) Then

      list.Add(Nothing)

    Else

      list.Add(item) 'Or list.Add(item).Duplicate()

    End If

  Next 

  dupData.AppendRange(list, p)

Next

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

That's brilliant!! Huge thanks, works perfectly.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service