ar0551 / Wasp

Combinatorial Design with Grasshopper plug-in (LGPL) initiated by Andrea Rossi
https://linktr.ee/gh_wasp
GNU Lesser General Public License v3.0
54 stars 14 forks source link

Feature Request: Serialize Aggregation for use with Hops components #16

Closed s7uvx closed 6 months ago

s7uvx commented 6 months ago

Hi Andrea, it would be tremendously beneficial to be able to send aggregations to and from Hops components, as this would open up parallel computing for Wasp - I know that there is a save to file method that I could use, but thought I would ask you to expose the JSON encoded aggregation which can then be used as a String to send to a hops component?

ar0551 commented 6 months ago

Hi,

each class in Wasp has a serialization and deserialization method (with the exception of Attributes), respectively called "to_data" and "from_data". This returns a python dictionary already formatted to be converted to a JSON file. So, it should be possible to use a python component and call the .to_data() method on the aggregation object in order to serialize. Let me know if you need help with setting that up.

s7uvx commented 6 months ago

Thanks Andrea, I'll try this.

`

s7uvx commented 6 months ago

OK, I think I have something workable: PART_OUT to TXT:

import sys
import os
import Rhino.Geometry as rg
import Grasshopper as gh
import json

## add Wasp install directory to system path
wasp_loaded = False
ghcompfolder = gh.Folders.DefaultAssemblyFolder
if ghcompfolder not in sys.path:
    sys.path.append(ghcompfolder)
try:
    from wasp import __version__
    wasp_loaded = True
except:
    msg = "Cannot import Wasp. Is the wasp folder available in " + ghcompfolder + "?"
    ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Error, msg)

## if Wasp is installed correctly, load the classes required by the component
if wasp_loaded:
    from wasp.core import Aggregation

TXT = json.dumps(PART_OUT.to_data())

TXT to PART_OUT

import sys
import os
import Rhino.Geometry as rg
import Grasshopper as gh
import json

## add Wasp install directory to system path
wasp_loaded = False
ghcompfolder = gh.Folders.DefaultAssemblyFolder
if ghcompfolder not in sys.path:
    sys.path.append(ghcompfolder)
try:
    from wasp import __version__
    wasp_loaded = True
except:
    msg = "Cannot import Wasp. Is the wasp folder available in " + ghcompfolder + "?"
    ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Error, msg)

## if Wasp is installed correctly, load the classes required by the component
if wasp_loaded:
    from wasp.core import AdvancedPart

PART_OUT = AdvancedPart.from_data(json.loads(TXT))
s7uvx commented 6 months ago

Just FYI I can't recover some of the attributes on the other end, any pointers would be appreciated.

ar0551 commented 6 months ago

As mentioned above, Wasp Attributes are currently the only Wasp class that does not support serialization. That means that Attributes will be ignored when serializing a Part or an Aggregation. This is due to the fact that Wasp Attributes support virtually any kind of data, which makes it challenging to have a serialization method able to support all these types.