Grasshopper

algorithmic modeling for Rhino

Recently i have started to re-script the code, i originally wrote in java, into grasshopper and i am having trouble on how to create arrays in grasshopper...is there a specific command in grasshopper for array or using VB and C#??

 

i have attached the part of java code that i am trying to re-script...its very simple, if there is a way of creating a string of array in grasshopper...

 

Views: 20775

Replies to This Discussion

Hi Khizer,

 

you mean "array" as in Rhino or "array" as in "a list of data"?

 

All data in Grasshopper is already stored in lists. And you can make lists from scratch using for example the Range and Series components. These two create numeric lists. The Sequence component creates a list of textual strings.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

By array, i mean, 2d list of data, sort of a matrix, to which i can assign data values, and then read those values using 'IF THEN ELSE statements' to generate a growth sequence....

 

you'll get a better idea of what i intend to do in the JavaScript file attached...

I don't read java. Grasshopper doesn't support 2D arrays, but you can try and solve it using 4 approaches:

 

1) Serialize your 2D array to a String and pass the string around. This means you can only operate on the data from your own components.

2) Put your 2D array data inside a Grasshopper.Kernel.Types.GH_ObjectWrapper instance, which is a class that can be used to transmit non-standard data through wires. Again, you'll only be able to use this from your own components.

3) Create your own data-type (implement IGH_Goo) as a 2D array.

4) (and my favourite) store your 2D data in a DataTree instead. All grasshopper data is stored in trees and it's possible to mimic a 2D array this way. For example, you could create a tree like this:

 

{0} N = 10

{1} N = 10

{2} N = 10

{3} N = 10

{4} N = 10

 

This would be analogous to a 2D matrix of 5 x 10.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

So now i have been able to convert part of the code in Vb for test, the array is working just fine...

 

The next thing would be to connect the information from that array to a grid, where each square in the grid represent a data block in that array... can you guide me how would i do that, i am sending you the grasshopper file, you can go through the Vb code in it too...

 

And can you explain me, in a bit detail, 4th option in your comment, like what buttons to use to create data trees etc.

Attachments:

You can use a function like this to convert a 2D array into a DataTree:

 

'This function converts a 2D array into a Grasshopper Data Tree structure. 

'It's a generic function (that's what the "(Of T)" means), meaning it will 

'match the type of DataTree to the type of Array. I.e. if you have an array 

'strings, you'll get a DataTree of strings. 

Function Array2Tree(Of T)(ByVal arr(,) As T) As DataTree(Of T)

  'Declare a new DataTree

  Dim tree As New DataTree(Of T)


  'Iterate over the first dimension of the array

  For i As Int32 = 0 To arr.GetUpperBound(0)

    'Iterate over the second dimension of the array

     For j As Int32 = 0 To arr.GetUpperBound(1)

      'Insert the current value into the DataTree.

      'The first dimension ends up as the Path,

      'the second dimension ends up as the index.

        tree.Insert(arr(i, j), New GH_Path(i), j)

    Next

  Next


  Return tree

End Function

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

 

I made some changes to the script. I removed all calls to Randomize() and Rnd() as these are legacy functions. In .NET you should be using a unique instance of the System.Random class.

 

Instead of using "-" to annotate empty cells, I use the constant String.Empty. I then replace all occurrences of String.Empty with "-" at the end of the script.

 

I used Select...Case statements to handle the Random switches. I could have used If...Then as well, but if you want to add additional random behaviours it's now easier to extend the logic.

 

I renamed local variables and made them all start with a lower case letter (fairly typical convention, see: http://msdn.microsoft.com/en-us/library/ms229002.aspx)

 

I think there are some bugs in the huge If...ElseIf...Then structures. It seems like logically sometimes you should be checking for an empty cell when you are in fact doing the opposite, but it's hard to be sure. At any rate, I removed all the tests from the conditional and instead compute the empty-ness of adjacent cells first, then use variables like emptyLeft, emptyUp etc. I think it makes the code a bit more readable.

 

I also noted that you do not perform any bounds checking prior to accessing array elements. Since the script doesn't crash I assume it's impossible (at least under the current conditions) that you would overstep array bounds, but it does make me pretty itchy to see stuff like:

 

arr1(i - 1, j)

 

without a:

 

If (i > 0) Then

 

in front of it. Anyway, hope this is useful feedback.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Attachments:

Well i am a rookie in programming, with no background whatsoever of any language, and new to grasshopper too, although i am trying to get a hold of them... You would have guessed it after seeing the script...

 

I looked at the changes you made in my code, understood some.I have a bound check in java, not here, since it was a test if this could be converted to grasshopper...

 

I changed a little mistake, in line 179-183 i think, replacing i,j with x,y 

 

Well, i still dont understand how to assign these values to a 2d grid, in grasshopper, i have added the button now,in the file. Positions from the array having a,b,c will be represented in the grid with cubes (in different color for legibility...)

Attachments:

If you're going to use the "a", "b", "c", "-" data to assign colours, it's probably easier to use Integers instead of Strings. I've replaced all string literals with integers in the attached file:

 

"-" = 0

"a" = 1

"b" = 2

"c" = 3

 

and then I use a List Index to convert these numbers to colours.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Attachments:

How can i extrude these squares in the grid, with 1,2,3 values without extruding squares with 0 value (extruded geometry having the same color as the square i.e. red,blue and yellow)?

 

 

Attachments:

Instead of using the integer to access a colour from a list, you'd use the integer to access an extrusion vector/distance.

 

You can remove items by culling them using a Cull Pattern component.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

I tried using Integer to access an extrusion, it worked but now they are all the same...  All colors are gone... how could i add those colors to the extrusions now?? i am attaching the file for you to have a look...
Attachments:

Hi khizer,

 

the Custom Preview component needs Geometry and Shaders as input. You can use Colours instead of Shaders, you just won't have control over transparency and highlights and stuff.

 

However the Custom Preview component in your file hasn't got any geometry hooked up to it and where the colours are supposed to go you're putting strings.

 

You replaced my original list of colour names with "0", "1", "1" and "1".

 

Fix these three problems and you should get colours again.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

 

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service