Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 54
[Prefabs] dataclasses to pydantic BaseModel#607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:25_04_work_on_new_prefabs
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d6d07948df6143778d91231ee7a759bb89b21c0af2ec2b2c373849a386e9a0df8050b3e597d65ee70863a30190f63b2f72690d6c971232f4cdea52468962844b3df06763b4ac4352fc2ade7fa7151684037aee533b98ae42a55a36d40d019666d8e81c32b92879b59d7b62fb2c64a3f65d232154ce1fbaab456dfaaa8c1d2465a82d06629380be8cbae77763266a6ce12baea2d850a24850999192fc7fbec840188b239c6aee9423b2eff829ee6042559753a63966a57e288d4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated but fine by me. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| import Sofa.Core | ||
| from baseParameters import BaseParameters | ||
| import Sofa | ||
| from stlib.core.baseParameters import BaseParameters | ||
| class BaseEntity(Sofa.Core.Prefab): | ||
| class BaseEntity(Sofa.Prefab): | ||
| parameters : BaseParameters | ||
| def __init__(self): | ||
| Sofa.Core.Prefab.__init__(self) | ||
| Sofa.Prefab.__init__(self) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,34 @@ | ||
| import dataclasses | ||
| from splib.core.utils import DEFAULT_VALUE | ||
| import dataclasses | ||
| from pydantic import BaseModel, ValidationError | ||
| from dynapydantic import SubclassTrackingModel | ||
| from typing import Callable, Optional, Any | ||
| import Sofa | ||
| class BaseParameters(SubclassTrackingModel, | ||
| discriminator_field="type", | ||
| discriminator_value_generator=lambda t: t.__name__,): | ||
| @dataclasses.dataclass | ||
| class BaseParameters(object): | ||
| name : str = "Object" | ||
| kwargs : dict = dataclasses.field(default_factory=dict) | ||
| kwargs : dict = {} | ||
| @classmethod | ||
| def fromYaml(self, data: str): | ||
| import yaml | ||
| dataDict = yaml.safe_load(data) | ||
| return self.fromDict(dataDict) | ||
| @classmethod | ||
| def fromDict(self, data: dict): | ||
| try: | ||
| return self.model_validate(data, strict=True) | ||
| except ValidationError as exc: | ||
| for error in exc.errors(): | ||
| loc = error.get("loc") | ||
| message = "" | ||
| for locPart in loc: | ||
| message += locPart.__str__() + ": " | ||
| message += error.get("msg") | ||
| Sofa.msg_error(self.__name__, message) | ||
| def toDict(self): | ||
| return dataclasses.asdict(self) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,29 @@ | ||
| import copy | ||
| import Sofa | ||
| import Sofa.Core | ||
| from stlib.core.basePrefabParameters import BasePrefabParameters | ||
| from stlib.core.baseParameters import BaseParameters | ||
| class BasePrefabParameters(BaseParameters): | ||
| name : str = "object" | ||
| kwargs : dict = {} | ||
| # Transformation information | ||
| # TODO: these data are going to be added in Node in SOFA (C++ implementation) | ||
| translation : list[float] = [0., 0., 0.] | ||
| rotation : list[float] = [0., 0., 0.] | ||
| scale : list[float] = [1., 1., 1.] | ||
| class BasePrefab(Sofa.Core.Node): | ||
| """ | ||
| A Prefab is a Sofa.Node that assembles a set of components and nodes | ||
| """ | ||
| parameters : BasePrefabParameters | ||
| def __init__(self, parameters: BasePrefabParameters): | ||
| Sofa.Core.Node.__init__(self, name=parameters.name) | ||
| self.parameters = parameters | ||
| def init(self): | ||
| raise NotImplemented("To be overridden by child class") | ||
| raise NotImplemented("This method should be implemented in the child class to initialize the prefab's components and nodes based on the provided parameters.") | ||
| def localToGlobalCoordinates(pointCloudInput, pointCloudOutput): | ||
| raise NotImplemented("Send an email to Damien, he will help you. Guaranteed :)") | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from stlib.entities import Entity, EntityParameters | ||
| from stlib.visual import VisualParameters | ||
| from stlib.geometries.file import FileParameters | ||
| from stlib.materials.deformable import DeformableMaterialParameters | ||
| from splib.core.enum_types import ElementType | ||
| from typing import Optional | ||
| class BunnyParameters(EntityParameters): | ||
| name : str = "Bunny" | ||
| deformable : bool = False | ||
| visual : Optional[VisualParameters] = VisualParameters(geometry = FileParameters(filename="mesh/Bunny.stl")) | ||
| def model_post_init(self, _): | ||
| # TODO: | ||
| # 1. apply size as scale in geometry, material, collision and visual | ||
| if self.deformable: | ||
| self.geometry = FileParameters(filename="mesh/Bunny.vtk", elementType=ElementType.TETRAHEDRA) | ||
| self.material = DeformableMaterialParameters() | ||
| return | ||
| class Bunny(Entity): | ||
| def __init__(self, parameters: BunnyParameters): | ||
| super().__init__(parameters) |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot that we wanted to have this. At one point we'll need to merge this with the node modifier part.