Grasshopper

algorithmic modeling for Rhino

Is there a way to use Python to extract the left side values from a Value List component?

Views: 2840

Replies to This Discussion

#fridayNight

list = []

for obj in ghdoc.ActiveObjects():

    if obj.ToString()=="Grasshopper.Kernel.Special.GH_ValueList":

        list = obj.ListItems

lefts = []

for item in list:

    lefts.Add(item.Name)

a = lefts

this version will only look at a value list that's actually plugged in to your python script:


list = []
for obj in ghdoc.ActiveObjects():
    if ghenv.Component.DependsOn(obj):
        try:
          list = obj.ListItems
        except:
            pass
lefts = []
for item in list:
   lefts.Add(item.Name)
a = lefts

Cool thanks Andrew. They didn't work as is. I had to poke the document with a stick on line 1.

ghdoc = ghenv.Component.OnPingDocument()

Gh def attached.

Attachments:

Oops I broke it when I saved it as a user object.

Runtime error (MissingMemberException): 'list' object has no attribute 'Add'

Fixed it by adding "import Grasshopper" on line 1.

Userobj attached.

Attachments:

What would be interesting would be to limit the output to the selected list index.

Is there a method for the object, like ListItems, that would extract the currently selected index from the component?

Basically I want to be able to use the Value List component as a Key/Value search component because it's a better interface.

Man, someone should really work on some custom UI stuff for Grasshopper...

Hey Brain,

I did some tinkering and found you can call 'SelectedItems' on your obj which will return a list of the currently checked keys. You can use this to get the index(s) that are selected by comparing it to your 'ListItems'.

selectedKeys = [item.Name for item in valList.SelectedItems]

keys = [item.Name for item in valList.ListItems]

values = [item.Value for item in valList.Values]

You should be able to put together a dict (key/value) from that.

*Note the values are a mixed bag as they come out as either GH_integers or GH_Numbers but shouldn't matter once you've assigned it to your component output.

Hey everyone thanks for your help with this - credit given to Matt and Andrew in the script header.

Please see attached for the latest Wombat Value List Data user object.

Here's the user object.

Attachments:

Hi Brian,

I was searching for how to extract the data from the left side of the Value list and I bumped into your post here.

I have a more specific request as I only want to extract the current(or the selected/checked) value of the left side instead of all the left side values.

I personally don't know much about Python, so I thought I could use the power of all the smart brains on the forum here.

Could anyone help me with that?

Many many thanks in advance!

Hi Wenyao, that's the "Value List Selected Value" output shown in the image above, which is available in the *.ghuser download link above.

Hi Brian,

When I dragged the Wombat ghuser file in, it says The User Object could not be created as the base type is missing.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service