Skip to content

Add BHoMObject class to python - #233

Open
Tom-Kingstone wants to merge 15 commits into
developfrom
Python_Toolkit-#220-BHoMObject
Open

Add BHoMObject class to python#233
Tom-Kingstone wants to merge 15 commits into
developfrom
Python_Toolkit-#220-BHoMObject

Conversation

@Tom-Kingstone

Copy link
Copy Markdown
Contributor

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:

from python_toolkit.bhom.bhom_object import BHoMObject

class SomeClass(BHoMObject):
    def __init__(self, other_property: str, **kwargs):
        _t = "BH.oM.Python.SomeClass"
        super().__init__(self, _t, other_property, **kwargs)
        
sc = SomeClass("this is an other property")

print(sc)
>>> "SomeClass of type BH.oM.Python.SomeClass, name: '', version: 'Unknown', id: '8be4df61-93ca-11d2-aa0d-00e098032b8c'"

j = sc.to_json() # creates json of the class in the BHoM style with PascalCase property names.
sc2 = SomeClass.from_json(j) # automatically converts PascalCase to camel_case, and creates an object of the correct type

print(sc2.other_property)
>>> "this is an other property"

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

  • Added BHoMObject base class to python_toolkit.bhom.bhom_object
    • Added BHoMJSONDecoder and BHoMJSONEncoder to convert between BHoM and python objects
    • Added a decorator that allows functions/methods to be called with a json document/string, which automatically fills in the required arguments.

Additional comments

…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
…low subclassing of the BHoM serialisers, and changed to_dict() mthod to deepcopy
@Tom-Kingstone Tom-Kingstone self-assigned this Jul 30, 2026
@Tom-Kingstone Tom-Kingstone added the type:feature New capability or enhancement label Jul 30, 2026

@jamesramsden-bh jamesramsden-bh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with run_tests.bat. All relevant tests passing.

@jamesramsden-bh

Copy link
Copy Markdown
Contributor

@BHoMBot check required

@bhombot-ci

bhombot-ci Bot commented Jul 30, 2026

Copy link
Copy Markdown

@jamesramsden-bh to confirm, the following actions are now queued:

  • check code-compliance
  • check documentation-compliance
  • check project-compliance
  • check core
  • check null-handling
  • check serialisation
  • check versioning
  • check installer

There are 350 requests in the queue ahead of you.

@jamesramsden-bh

Copy link
Copy Markdown
Contributor

@BHoMBot check copyright-compliance

@bhombot-ci

bhombot-ci Bot commented Jul 30, 2026

Copy link
Copy Markdown

@jamesramsden-bh to confirm, the following actions are now queued:

  • check copyright-compliance

There are 367 requests in the queue ahead of you.

@jamesramsden-bh

Copy link
Copy Markdown
Contributor

@BHoMBot check ready-to-merge

@bhombot-ci

bhombot-ci Bot commented Jul 30, 2026

Copy link
Copy Markdown

@jamesramsden-bh to confirm, the following actions are now queued:

  • check ready-to-merge

There are 381 requests in the queue ahead of you.

…ich are only accessible if the file contained is imported)
@Tom-Kingstone Tom-Kingstone added the status:WIP PR in progress and still in draft, not ready for formal review label Jul 31, 2026
@Tom-Kingstone

Copy link
Copy Markdown
Contributor Author

apologies I should have added WIP to this PR

@Tom-Kingstone Tom-Kingstone added the status:do-not-merge For instance, test PR, requires further discussion, or dependant PRs not ready for merge label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:do-not-merge For instance, test PR, requires further discussion, or dependant PRs not ready for merge status:WIP PR in progress and still in draft, not ready for formal review type:feature New capability or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BHoMObject python implementation

2 participants