Here's the modified class that meets your requirements:
import copy
class ObjectState:
def __init__(self, position_tuple, rotation_tuple, scale, callback):
self._callback = callback
# Convert tuples to dicts with x, y, z keys
self._position = {'x': position_tuple[0], 'y': position_tuple[1], 'z': position_tuple[2]}
self._rotation = {'x': rotation_tuple[0], 'y': rotation_tuple[1], 'z': rotation_tuple[2]}
self._scale = scale
@property
def position(self):
return self._position
@position.setter
def position(self, value):
self._position = value
self._callback()
@property
def rotation(self):
return self._rotation
@rotation.setter
def rotation(self, value):
self._rotation = value
self._callback()
@property
def scale(self):
return self._scale
@scale.setter
def scale(self, value):
self._scale = value
self._callback()
def deepcopy_from(self, other):
"""Create a deep copy from another ObjectState instance"""
self._position = copy.deepcopy(other.position)
self._rotation = copy.deepcopy(other.rotation)
self._scale = other.scale
# Note: callback is triggered for each assignment
self._callback()
Key changes made:
- Removed the
@dataclass decorator and converted to a regular class
- Added
__init__ method that takes two tuples, a float, and a callback function
- Converts the input tuples to dictionaries with 'x', 'y', 'z' keys
- Uses properties with setters to trigger the callback whenever
position, rotation, or scale are modified
- Added
deepcopy_from method that creates a deep copy from another ObjectState instance
- Uses private attributes (
_position, _rotation, _scale) to store the actual data while exposing them through properties
Usage example:
def my_callback():
print("State changed!")
# Create an instance
obj = ObjectState((1.0, 2.0, 3.0), (0.0, 90.0, 0.0), 1.5, my_callback)
# Modify values (triggers callback)
obj.position = {'x': 5.0, 'y': 6.0, 'z': 7.0}
obj.scale = 2.0
# Copy from another instance
other_obj = ObjectState((10.0, 20.0, 30.0), (45.0, 0.0, 180.0), 0.5, my_callback)
obj.deepcopy_from(other_obj)