Trying to create a Random Matrix for Grasshopper, but NO result

54 Views Asked by At

I´ve just using Python for creating a Random Matrix, but no results.Width and Height are defined in outside in Grasshopper. Any suggestions?

#imports

import rhinoscriptsyntax as rs
import math 
import random as rnd

#for accesing GH classes - JUST IN CASE

import clr
clr.AddReference("Grasshopper") 
from Grasshopper.Kernel.Data import *
from Grasshopper import DataTree

#create a Matrix

def initMatrix(rndSeed):
    global A0
    global B0
    
    tempVals=[]
    rnd.seed(rndSeed)
    for i in range (height):
        thisA = []  # empty list
        thisB = []  # empty list
        for j in range (width):
            thisA.append(rnd.random()*12+ rnd.random()*2)
            thisB.append(rnd.random()*12+ rnd.random()*2)
        tempVals.append(thisA)
        
#       A0.append(thisA)
#       B0.append(thisB)

    return tempVals


# Call the initMatrix function

    a=initMatrix(0.1)

enter image description here

"RESOLUTION" bar will give visible results in the lower yellow panel connected to "A".

1

There are 1 best solutions below

0
urbancomputing207 On

It looks like you just have invalid indentations in your script. The comments above the return tempVals will cause it to be interpreted outside your function. The indentation of a=initMatrix(0.1) also looks incorrect and should not be indented. Additionally, if you are creating a multi-dimensional array, your output will not be interpretable by Grasshopper, it will read as IronPython.Runtime.List. I suggest formatting your matrix as a native grasshopper tree like the example below.

#imports
import random as rnd
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree

# Create a Matrix
def initMatrix(rndSeed, width, height):
    rnd.seed(rndSeed)
    tree = DataTree[object]()  # Create a DataTree object

    for i in range(height):
        path = GH_Path(i)  # Create a new path for each row
        for j in range(width):
            a_value = rnd.random()*12 + rnd.random()*2
            b_value = rnd.random()*12 + rnd.random()*2
            # Add values to the tree at the specified path
            tree.Add(a_value, path)
            tree.Add(b_value, path)
    
    return tree

# Call the initMatrix function
a = initMatrix(0.1, 10, 10)  # Example with width = 10, height = 10**strong text**