2023-11-28 23:50:50 -08:00
|
|
|
from typing import Any, Dict, Type, TypeVar, Union
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
import attr
|
2023-11-28 23:50:50 -08:00
|
|
|
from pydantic import BaseModel, GetCoreSchemaHandler
|
|
|
|
from pydantic_core import CoreSchema, core_schema
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
class default_scene(BaseModel):
|
|
|
|
"""Visit the default scene."""
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
type: str = "default_scene"
|
|
|
|
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
class scene_by_index(BaseModel):
|
|
|
|
"""Visit the indexed scene."""
|
2023-11-27 16:01:20 -08:00
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
index: int
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
type: str = "scene_by_index"
|
|
|
|
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
class scene_by_name(BaseModel):
|
|
|
|
"""Visit the first scene with the given name."""
|
2023-11-27 16:01:20 -08:00
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
name: str
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
type: str = "scene_by_name"
|
|
|
|
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
class mesh_by_index(BaseModel):
|
|
|
|
"""Visit the indexed mesh."""
|
2023-11-27 16:01:20 -08:00
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
index: int
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
type: str = "mesh_by_index"
|
|
|
|
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
class mesh_by_name(BaseModel):
|
|
|
|
"""Visit the first mesh with the given name."""
|
2023-11-27 16:01:20 -08:00
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
name: str
|
2023-11-27 16:01:20 -08:00
|
|
|
|
|
|
|
type: str = "mesh_by_name"
|
|
|
|
|
|
|
|
|
2023-11-28 14:29:16 -08:00
|
|
|
GY = TypeVar("GY", bound="Selection")
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
2023-11-28 14:16:05 -08:00
|
|
|
class Selection:
|
|
|
|
|
|
|
|
"""Data item selection."""
|
|
|
|
|
|
|
|
type: Union[
|
|
|
|
default_scene,
|
|
|
|
scene_by_index,
|
|
|
|
scene_by_name,
|
|
|
|
mesh_by_index,
|
|
|
|
mesh_by_name,
|
2023-11-28 14:29:16 -08:00
|
|
|
]
|
2023-11-28 14:16:05 -08:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
type: Union[
|
2023-11-28 14:29:16 -08:00
|
|
|
default_scene,
|
|
|
|
scene_by_index,
|
|
|
|
scene_by_name,
|
|
|
|
mesh_by_index,
|
|
|
|
mesh_by_name,
|
2023-11-28 14:16:05 -08:00
|
|
|
],
|
|
|
|
):
|
|
|
|
self.type = type
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
def model_dump(self) -> Dict[str, Any]:
|
2023-11-28 14:16:05 -08:00
|
|
|
if isinstance(self.type, default_scene):
|
2023-11-28 23:50:50 -08:00
|
|
|
JO: default_scene = self.type
|
|
|
|
return JO.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
elif isinstance(self.type, scene_by_index):
|
2023-11-28 23:50:50 -08:00
|
|
|
TE: scene_by_index = self.type
|
|
|
|
return TE.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
elif isinstance(self.type, scene_by_name):
|
2023-11-28 23:50:50 -08:00
|
|
|
WY: scene_by_name = self.type
|
|
|
|
return WY.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
elif isinstance(self.type, mesh_by_index):
|
2023-11-28 23:50:50 -08:00
|
|
|
QV: mesh_by_index = self.type
|
|
|
|
return QV.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
elif isinstance(self.type, mesh_by_name):
|
2023-11-28 23:50:50 -08:00
|
|
|
BP: mesh_by_name = self.type
|
|
|
|
return BP.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
|
|
|
|
raise Exception("Unknown type")
|
|
|
|
|
2023-11-28 14:29:16 -08:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls: Type[GY], d: Dict[str, Any]) -> GY:
|
2023-11-28 14:16:05 -08:00
|
|
|
if d.get("type") == "default_scene":
|
2023-11-28 23:50:50 -08:00
|
|
|
OF: default_scene = default_scene(**d)
|
|
|
|
return cls(type=OF)
|
2023-11-28 14:16:05 -08:00
|
|
|
elif d.get("type") == "scene_by_index":
|
2023-11-28 23:50:50 -08:00
|
|
|
OV: scene_by_index = scene_by_index(**d)
|
|
|
|
return cls(type=OV)
|
2023-11-28 14:16:05 -08:00
|
|
|
elif d.get("type") == "scene_by_name":
|
2023-11-28 23:50:50 -08:00
|
|
|
FK: scene_by_name = scene_by_name(**d)
|
|
|
|
return cls(type=FK)
|
2023-11-28 14:16:05 -08:00
|
|
|
elif d.get("type") == "mesh_by_index":
|
2023-11-28 23:50:50 -08:00
|
|
|
PE: mesh_by_index = mesh_by_index(**d)
|
|
|
|
return cls(type=PE)
|
2023-11-28 14:16:05 -08:00
|
|
|
elif d.get("type") == "mesh_by_name":
|
2023-11-28 23:50:50 -08:00
|
|
|
FP: mesh_by_name = mesh_by_name(**d)
|
|
|
|
return cls(type=FP)
|
2023-11-28 14:16:05 -08:00
|
|
|
|
|
|
|
raise Exception("Unknown type")
|
2023-11-28 23:50:50 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __get_pydantic_core_schema__(
|
|
|
|
cls, source_type: Any, handler: GetCoreSchemaHandler
|
|
|
|
) -> CoreSchema:
|
|
|
|
return core_schema.no_info_after_validator_function(
|
|
|
|
cls,
|
|
|
|
handler(
|
|
|
|
Union[
|
|
|
|
default_scene,
|
|
|
|
scene_by_index,
|
|
|
|
scene_by_name,
|
|
|
|
mesh_by_index,
|
|
|
|
mesh_by_name,
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|