2024-02-19 14:28:35 +01:00
|
|
|
import typing as typ
|
|
|
|
|
2024-03-10 11:56:37 +01:00
|
|
|
import bpy
|
|
|
|
import sympy as sp
|
2024-02-19 14:28:35 +01:00
|
|
|
import pydantic as pyd
|
|
|
|
|
2024-03-10 11:56:37 +01:00
|
|
|
from .....utils.pydantic_sympy import SympyExpr
|
2024-02-19 14:28:35 +01:00
|
|
|
from .. import base
|
2024-03-10 11:56:37 +01:00
|
|
|
from ... import contracts as ct
|
2024-02-19 14:28:35 +01:00
|
|
|
|
|
|
|
####################
|
|
|
|
# - Blender Socket
|
|
|
|
####################
|
2024-03-10 11:56:37 +01:00
|
|
|
class RationalNumberBLSocket(base.MaxwellSimSocket):
|
|
|
|
socket_type = ct.SocketType.RationalNumber
|
2024-02-19 14:28:35 +01:00
|
|
|
bl_label = "Rational Number"
|
|
|
|
|
2024-03-10 11:56:37 +01:00
|
|
|
####################
|
|
|
|
# - Properties
|
|
|
|
####################
|
|
|
|
raw_value: bpy.props.IntVectorProperty(
|
|
|
|
name="Rational Number",
|
|
|
|
description="Represents a rational number (int / int)",
|
|
|
|
size=2,
|
|
|
|
default=(1, 1),
|
|
|
|
subtype='NONE',
|
|
|
|
update=(lambda self, context: self.sync_prop("raw_value", context)),
|
|
|
|
)
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - UI
|
|
|
|
####################
|
|
|
|
def draw_value(self, col: bpy.types.UILayout) -> None:
|
|
|
|
col_row = col.row(align=True)
|
|
|
|
col_row.prop(self, "raw_value", text="")
|
|
|
|
|
2024-02-19 14:28:35 +01:00
|
|
|
####################
|
|
|
|
# - Default Value
|
|
|
|
####################
|
|
|
|
@property
|
2024-03-10 11:56:37 +01:00
|
|
|
def value(self) -> sp.Rational:
|
|
|
|
p, q = self.raw_value
|
|
|
|
return sp.Rational(p, q)
|
2024-02-19 14:28:35 +01:00
|
|
|
|
2024-03-10 11:56:37 +01:00
|
|
|
@value.setter
|
|
|
|
def value(self, value: float | tuple[int, int] | SympyExpr) -> None:
|
|
|
|
if isinstance(value, float):
|
|
|
|
approx_rational = sp.nsimplify(value)
|
|
|
|
self.raw_value = (approx_rational.p, approx_rational.q)
|
|
|
|
elif isinstance(value, tuple):
|
|
|
|
self.raw_value = value
|
|
|
|
else:
|
|
|
|
self.raw_value = (value.p, value.q)
|
2024-02-19 14:28:35 +01:00
|
|
|
|
|
|
|
####################
|
|
|
|
# - Socket Configuration
|
|
|
|
####################
|
|
|
|
class RationalNumberSocketDef(pyd.BaseModel):
|
2024-03-10 11:56:37 +01:00
|
|
|
socket_type: ct.SocketType = ct.SocketType.RationalNumber
|
2024-02-19 14:28:35 +01:00
|
|
|
|
|
|
|
def init(self, bl_socket: RationalNumberBLSocket) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Blender Registration
|
|
|
|
####################
|
|
|
|
BL_REGISTER = [
|
|
|
|
RationalNumberBLSocket,
|
|
|
|
]
|