Populate Value List updated to Python 3

A useful ironpython script to populate a Value List from a list of strings (keys) and values fails to work when converted to python 3. 2to3 utility makes no changes. Here is the code:

PopulateValueList_1.ghuser (2.5 KB)

How does one open that file?

How is it different from this?

1 Like

Sorry. Here is a better presentation.

PVL3.gh (10.3 KB)

There are two PVL python components.One version is Ironpython and the other is a Python 3 implementation.

With the Ironpython version enabled (and the Python 3 disabled), when the panel “LayerC” is modified, the Valuelist “LayerVL” updates.

With the Python 3 version enabled (and the Ironpython disabled), when the panel “LayerC” is modified, the Valuelist “LayerVL” DOES NOT update.

I have tried debugging this but could not find documenation for grasshopper.kernel.special

This is not stopping my work as the Ironpython module works. My concern is for the future and also to understand the changes needed to go from Ironpython to Python 3.

Thanks for your help with this.

-Steve

When I open that file in R7 I see this:

PVL_R7_2024Apr16a

When I open it in R8 I see this:

PVL_R8_2024Apr16a

  1. Error running script: ‘list’ object has no attribute ‘split’ [20:1]

Appears to be broken in both. I’ll stick to the one I linked to, which has practical enhancements, though it requires the text panel ‘LayerC’ to be ‘Multiline Data’.

PVL_2024Apr16a
PVL_2024Apr16a.gh (14.1 KB)

The white boxes in Rhino 7 make sense because I do not think it supports python3.

To be clear, I am using Rhino 8.

Thanks for pointing out the multiline data option. However it doesn’t work for me. I found I had to add double quotes to the Values to make the valuelist work, otherwise it produces only NULL values. I have included an updated version with this fix.

Not sure why split doesn’t work for you as it’s a standard builtin string op for python 3 at least.

PVL3.gh (10.5 KB)

It would be good if there was a refrence (with examples) that showed the differences between Ironpython and Python3 to help guide anyone, like me, who is upgrading/adapting their old scripts from earlier versions (I’m coming from V6).

I know there are trivial differences like the print statemtments and imports, and more subtle ones like for strings and the use of super.

As I say, it’s not holding me up right now but it would be good to find out why identical code is acting so differently.

Hi @customgamebits ,

I did some quick, light digging in your Py3 code and issue appears to be in your conditional statement where you have “if obj type is value list”, this is returning false.

If you “cheat” and cast both comparisons as strings the script works properly.

This will work… but isn’t obviously the correct way to do it. I need to dig in more to see what has changed type wise aka what the comparison actually should be for the correct conditional statement.

Hope this helps in the mean time:

ghenv.Component.Name = "PopulateValueList"
ghenv.Component.NickName = "PVL"

import Grasshopper as gh

for obj in ghenv.Component.OnPingDocument().Objects:
    print((obj))
    if str(obj) == str("Grasshopper.Kernel.Special.GH_ValueList"):
        print("obj is value list")
        if obj.NickName == Name:
            print(obj.NickName)
            obj.ListItems.Clear()
            for k,v in zip(Keys,Values):
                #vli = gh.Kernel.Special.GH_ValueListItem(str(k),str(v))
                vli = gh.Kernel.Special.GH_ValueListItem("".join(k),"".join(v))
                obj.ListItems.Add(vli)
            obj.ExpireSolution(True)
    else:
        print("obj not value list")

20240416_PVL3_Response_01a.gh (12.1 KB)

EDIT:

Okay here you are, type(obj) returns a class, you need to see the obj is an instance of GH_ValueList.

Here’s the updated working code for Python 3:

"""Grasshopper Script"""
"""
Populate Value List
    Inputs:
        Name: {item,str}
        Keys: {list,str}
        Values: {list,float}
    Outputs:
    Remarks:
        Author: Anders Holden Deleuran (BIG IDEAS)
        Rhino: 6.30.20266.14531
        Version: 200923

        Modified By: Michael Vollrath (TOYBLOCK)
        Version: 20240416
        Notes: Updated for Python 3
"""

ghenv.Component.Name = "PopulateValueList"
ghenv.Component.NickName = "PVL"

import Grasshopper as gh

for obj in ghenv.Component.OnPingDocument().Objects:
    if isinstance(obj, gh.Kernel.Special.GH_ValueList):
        if obj.NickName == Name:
            print(obj.NickName)
            obj.ListItems.Clear()
            for k,v in zip(Keys,Values):
                #vli = gh.Kernel.Special.GH_ValueListItem(str(k),str(v))
                vli = gh.Kernel.Special.GH_ValueListItem("".join(k),"".join(v))
                obj.ListItems.Add(vli)
            obj.ExpireSolution(True)
    else:
        pass

Graph Space:

20240416_PVL3_Response_01b.gh (13.3 KB)

1 Like

Very strange, when I run this code,

if isinstance(obj, gh.Kernel.Special.GH_ValueList):

Never tests true even though there are Grasshopper.Kernel.Special.GH_ValueList objects in the loop.

Could it be something in configuration that is causing the difference?

Thanks all the help you’ve provided.

-Steve

1 Like

Same here (R8).

Related to this?

2 Likes

What Service Release are you running? @customgamebits and @Joseph_Oster

I’m on 8.7.24101

8.5 - Argh :bangbang: :man_facepalming: What a pain. Even upgrading isn’t easy.

1 Like

Well at least we now know it’s a bug that’s been fixed and not something unsolvable :slight_smile:

fwiw I have:

Version 8 SR6
(8.6.24101.5001, 2024-04-10)
Commercial

1 Like

After installing 8.7 I tried to open a GH file I was working on and Rhino (R8) has now frozen, Time to reboot. :rage:

If you can’t upgrade… my string hack above should work until you are ready to upgrade.

Otherwise, give it an upgrade, test again and it should be working.

It’s probable that a component was upgraded and you now have disconnected or misfiring logic somewhere. I would lock the canvas before opening the script and check for any “Orange blob upgraded component notices”

It’s painful sometimes… but thankfully more recently there hasn’t been as many breaking changes.

Damn, damn, damn. After going through all of that, I apparently installed 8.6 instead of 8.7.

How do I get 8.7 :interrobang: :man_facepalming:

Utterly pathetic. Apparently when I do the same thing as I did before, I now get 8.7 :question: :question: :rage:

Make sure you have SRC selected in the Update Frequency of your Help>Check For Updates.

image

If nothing is available you can click “Check Now…” and sometimes a more recent release is available than what is shown in Rhino.

No, there must be more to it than that. 8.6 is the only thing available to me.

OUTRAGEOUS :bangbang:

OK, wait… I stumbled across 8.7 waiting to be installed. Working on it

1 Like

FINALLY. :rofl: Way harder than it should be.

This GH file has both the version I linked to and the same thing “translated” to Python 3 using the fix provided by @michaelvollrath (thank you). With a switch between the two. Both seem to work.

if isinstance(obj, gh.Kernel.Special.GH_ValueList):

PVL_2024Apr16b
PVL_2024Apr16b.gh (14.8 KB)

2 Likes

I feel this way often. IDEA = simplicity, EXECUTION = complexity