Add BHoMObject class to python - #233
Open
Tom-Kingstone wants to merge 15 commits into
Open
Conversation
…properties also start JSON encoder and decoder but unsure of what to do here for decoder.
…erialiser, and improved from_json class method to use sub class initialiser to return the correct type when BHoMObject is sub classed.
…, and handle json deserialisation of lists (as BHoM serialiser in grasshopper likes to make json arrays)
…ictionary versions for converting between objects (for instance for the ladybugtools python package
…le types and fixed some bugs
…low subclassing of the BHoM serialisers, and changed to_dict() mthod to deepcopy
jamesramsden-bh
previously approved these changes
Jul 30, 2026
jamesramsden-bh
left a comment
Contributor
There was a problem hiding this comment.
Tested with run_tests.bat. All relevant tests passing.
Contributor
|
@BHoMBot check required |
|
@jamesramsden-bh to confirm, the following actions are now queued:
There are 350 requests in the queue ahead of you. |
Contributor
|
@BHoMBot check copyright-compliance |
|
@jamesramsden-bh to confirm, the following actions are now queued:
There are 367 requests in the queue ahead of you. |
Contributor
|
@BHoMBot check ready-to-merge |
|
@jamesramsden-bh to confirm, the following actions are now queued:
There are 381 requests in the queue ahead of you. |
…ich are only accessible if the file contained is imported)
Contributor
Author
|
apologies I should have added WIP to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issues addressed by this PR
Closes #220
This implementation will have updates in the future to iron out bugs, but for now this works well as a link between serialised c# BHoM objects and python ones.
Also included a decorator designed to be added to methods so that they can have auto-deserialised properties if calling from a BHoM adapter for instance (all that the BHoM adapter would then have to do is send a JSON document containing the BHoM Serialised json, and python would handle the rest)
The main purpose of this addition is to enable a tighter connection between BHoM adapters and Python methods/objects. Basically to add a replica of the BHoMObject class from c# into python, as well as a (fairly) simple JSON encoder/decoder that directly converts BHoM serialised JSON into python BHoMObjects (or IObject for serialised non-BHoMObjects). This implementation also comes with methods for converting to and from json and dictionaries for simplicity (rather than requiring direct usage of the json serialisers). The JSON decoder and encoder will automatically convert between PascalCase (C# side) and camel_case (python side) which should make it simpler to use it as a base class for objects that already attempt replication of existing BHoMObjects.
It is intended that the BHoMObject class (similar to c#) will act as a base class, so the json converter methods are designed to allow converting to the correct type after deserialisation.
Basic usage:
This can also be used to bring BHoM Objects from c# directly into python without needing to create a new class for them (for instance BHoM geometry can be pulled and manipulated as required with no need for a defined class)
I have added tests that check serialisation/deserialisation and round trips for IObject and BHoMObjects.
Test files
Run the python unit tests (
run_tests.bat) after build.Changelog
BHoMObjectbase class topython_toolkit.bhom.bhom_objectBHoMJSONDecoderandBHoMJSONEncoderto convert between BHoM and python objectsAdditional comments