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)
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?
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:
When I open it in R8 I see this:
- 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.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)
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
Same here (R8).
Related to this?
What Service Release are you running? @customgamebits and @Joseph_Oster
Iâm on 8.7.24101
8.5 - Argh
What a pain. Even upgrading isnât easy.
Well at least we now know itâs a bug thatâs been fixed and not something unsolvable
fwiw I have:
Version 8 SR6
(8.6.24101.5001, 2024-04-10)
Commercial
After installing 8.7 I tried to open a GH file I was working on and Rhino (R8) has now frozen, Time to reboot.
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
Utterly pathetic. Apparently when I do the same thing as I did before, I now get 8.7
Make sure you have SRC selected in the Update Frequency of your Help>Check For Updates.
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
OK, wait⌠I stumbled across 8.7 waiting to be installed. Working on it
FINALLY. 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.gh (14.8 KB)
I feel this way often. IDEA = simplicity, EXECUTION = complexity