Grasshopper

algorithmic modeling for Rhino

Any ideas how to implement switch statement in grasshopper?

Currently I'm using this python script that I hacked together:

ghenv.Component.Message = 'a = Switch(x)'
# TypeHint for x should be str
input_ids = [_.NickName for _ in ghenv.Component.Params.Input]
input_objs = [_.VolatileData for _ in ghenv.Component.Params.Input]
case_dict = dict(zip(input_ids, input_objs))
a = case_dict.get(x)

Any ideas how to implement it in pure grasshopper, or how to improve my script?

Views: 1058

Replies to This Discussion

something like this? 

Thanks, it works great and it's faster...

I altered my script to avoid iterating the parameter list twice:

#input_ids = [_.NickName for _ in ghenv.Component.Params.Input]
#input_objs = [_.VolatileData for _ in ghenv.Component.Params.Input]
#case_dict = dict(zip(input_ids, input_objs))

case_dict = dict([(i.NickName, i.VolatileData) for i in ghenv.Component.Params.Input])

Good idea!

I improved it a little bit:

a = next((_.VolatileData for _ in ghenv.Component.Params.Input if _.NickName == x), [])

That only gets the desired data instead of building the hole dictionary.

Also set the inputs (1,2,3,...) to List Access.

Attachments:

Thanks, great ideas!

I also tried this, to avoid iterating through input parameters:

index = ghenv.Component.Params.IndexOfInputParam(x) #???
a = ghenv.Component.Params.Input[index].VolatileData

But it doesn't work. I get index = -1 for some values of x (eg. for x='4'), even though I shouldn't. Any idea why?

I'm not sure but probably IndexOfInputParam checks the Name instead of the NickName of the parameter.

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