Hello
Is there a way to change between Item;List;Tree … Access from GhPython?
from Grasshopper.Kernel import GH_ParamAccess
if x == 0:
access = GH_ParamAccess.item
if x == 1:
access = GH_ParamAccess.list
if x == 2:
access = GH_ParamAccess.tree
if (access != ghenv.Component.Params.Input[1].Access):
ghenv.Component.Params.Input[1].Access = access
ghenv.Component.ExpireSolution(True)
ParamAccess.gh (9 KB)
Thank you @Mahdiyar , unfortunately didn’t work with GH_ParamAccess(x); i make a little change and i believe with enum will be simpler
and this change of the used component , it will need it for other things but in this case i want switch between item/list/tree inside ghpython.
the idea is to group similar items and i need to show list from IronPython.Runtime.List without lose data tree
The second python component can do that by choose item access and use a = x, but i want to do that in the same script, that’s why i want switch between them if it is possible
from Grasshopper.Kernel import GH_ParamAccess
access = [GH_ParamAccess.item,GH_ParamAccess.list,GH_ParamAccess.tree]
if access[x] != ghenv.Component.Params.Input[1].Access:
ghenv.Component.Params.Input[1].Access = access[x]
ghenv.Component.ExpireSolution(True)
group.gh (9.5 KB)
Hi @seghierkhaled,
maybe something like below?
And here’s my code:
"""Provides a scripting component.
Inputs:
x: The x script variable
y: The y script variable
Output:
a: The a output variable"""
__author__ = "bogdan"
__version__ = "2020.09.20"
import rhinoscriptsyntax as rs
from itertools import groupby
from itertools import chain
import ghpythonlib.treehelpers as th
total = 0
listChunks = []
second_list =y
for j in range(len(second_list)):
chunk_mylist = x[total:total+second_list[j]]
listChunks.append(chunk_mylist)
total += second_list[j]
#After running this, listChunks is a list containing sublists with the lengths found in second_list.
#print listChunks
endlist =[]
for j in range(len(listChunks)):
lists2 = [list(listChunks[j]) for i, listChunks[j] in groupby(listChunks[j])]
print lists2
endlist.append(lists2)
#a= th.list_to_tree(listChunks)
b= th.list_to_tree(endlist)
200920BC preserve tree structure.gh (10.0 KB)
Thank you @Bogdan_Chipara for help, i definitively learn from it.
I already found this solution
Cool, yours is more elegant! I will also learn from it.