-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from GazzolaLab/feat/pose
[Feat] Pose
- Loading branch information
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
__doc__ = """ | ||
Pose class for creating and updating poses in Blender | ||
""" | ||
__all__ = ["Pose"] | ||
|
||
import bpy | ||
from numpy.typing import NDArray | ||
|
||
from bsr.geometry.primitives.simple import Cylinder, Sphere | ||
from bsr.tools.keyframe_mixin import KeyFrameControlMixin | ||
|
||
|
||
class Pose(KeyFrameControlMixin): | ||
""" | ||
Pose class for managing visualization and rendering in Blender | ||
Parameters | ||
---------- | ||
position : NDArray | ||
The position of pose. Expected shape is (n_dim,). | ||
n_dim = 3 | ||
directors : NDArray | ||
The directors of the pose. Expected shape is (n_dim, n_dim). | ||
n_dim = 3 | ||
""" | ||
|
||
input_states = {"position", "directors"} | ||
|
||
def __init__( | ||
self, | ||
position: NDArray, | ||
directors: NDArray, | ||
unit_length: float = 1.0, | ||
thickness_ratio: float = 0.1, | ||
) -> None: | ||
# create sphere and cylinder objects | ||
self.spheres: list[Sphere] = [] | ||
self.cylinders: list[Cylinder] = [] | ||
self._bpy_objs: dict[str, bpy.types.Object] = { | ||
"spheres": self.spheres, | ||
"cylinders": self.cylinders, | ||
} | ||
self.__unit_length = unit_length | ||
self.__ratio = thickness_ratio | ||
|
||
self._build(position, directors) | ||
|
||
@property | ||
def object(self) -> dict[str, bpy.types.Object]: | ||
""" | ||
Return the dictionary of Blender objects: spheres and cylinders | ||
""" | ||
return self._bpy_objs | ||
|
||
def _build(self, position: NDArray, directors: NDArray) -> None: | ||
""" | ||
Build the pose object from the given position and directors | ||
""" | ||
# create the sphere object at the position | ||
sphere = Sphere( | ||
position, | ||
self.__unit_length * self.__ratio, | ||
) | ||
self.spheres.append(sphere) | ||
|
||
# create cylinder and sphere objects for each director | ||
for i in range(directors.shape[1]): | ||
tip_position = position + directors[:, i] * self.__unit_length | ||
cylinder = Cylinder( | ||
position, | ||
tip_position, | ||
self.__unit_length * self.__ratio, | ||
) | ||
self.cylinders.append(cylinder) | ||
|
||
sphere = Sphere( | ||
tip_position, | ||
self.__unit_length * self.__ratio, | ||
) | ||
self.spheres.append(sphere) | ||
|
||
def update_states(self, position: NDArray, directors: NDArray) -> None: | ||
""" | ||
Update the states of the pose object | ||
""" | ||
self.spheres[0].update_states(position) | ||
|
||
for i, cylinder in enumerate(self.cylinders): | ||
tip_position = position + directors[:, i] * self.__unit_length | ||
cylinder.update_states(position, tip_position) | ||
|
||
sphere = self.spheres[i + 1] | ||
sphere.update_states(tip_position) | ||
|
||
def set_keyframe(self, keyframe: int) -> None: | ||
""" | ||
Set the keyframe for the pose object | ||
""" | ||
for shperes in self.spheres: | ||
shperes.set_keyframe(keyframe) | ||
|
||
for cylinder in self.cylinders: | ||
cylinder.set_keyframe(keyframe) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import math | ||
|
||
import bpy | ||
import pytest | ||
|
||
from bsr.frame import FrameManager | ||
|
||
|
||
class TestFrameManager: | ||
|
||
def test_frame_manager_singleton(self): | ||
assert FrameManager() is FrameManager() | ||
|
||
def test_frame_manager_current_frame(self): | ||
frame_manager = FrameManager() | ||
assert frame_manager.current_frame == 0 | ||
|
||
def test_frame_manager_current_frame_setter(self): | ||
frame_manager = FrameManager() | ||
frame_manager.current_frame = 10 | ||
assert frame_manager.current_frame == 10 | ||
|
||
def test_frame_manager_current_frame_setter_with_wrong_frame(self): | ||
frame_manager = FrameManager() | ||
with pytest.raises(AssertionError): | ||
frame_manager.current_frame = -1 | ||
|
||
def test_frame_manager_update(self): | ||
frame_manager = FrameManager() | ||
frame_manager.current_frame = 0 | ||
frame_manager.update(10) | ||
assert frame_manager.current_frame == 10 | ||
|
||
def test_frame_manager_update_with_wrong_forwardframe(self): | ||
frame_manager = FrameManager() | ||
with pytest.raises(AssertionError): | ||
frame_manager.update(-1) | ||
|
||
def test_frame_manager_set_frame_end(self): | ||
frame_manager = FrameManager() | ||
frame_manager.set_frame_end(100) | ||
assert bpy.context.scene.frame_end == 100 | ||
|
||
def test_frame_manager_set_frame_end_with_none(self): | ||
frame_manager = FrameManager() | ||
frame_manager.current_frame = 0 | ||
frame_manager.update(250) | ||
frame_manager.set_frame_end() | ||
assert bpy.context.scene.frame_end == 250 | ||
|
||
def test_frame_manager_set_frame_end_with_wrong_frame(self): | ||
frame_manager = FrameManager() | ||
with pytest.raises(AssertionError): | ||
frame_manager.set_frame_end(-1) | ||
|
||
def test_frame_manager_get_set_frame_rate(self): | ||
frame_manager = FrameManager() | ||
frame_manager.set_frame_rate(30) | ||
assert frame_manager.get_frame_rate() == 30 | ||
frame_manager.set_frame_rate(29.97) | ||
assert math.isclose( | ||
frame_manager.get_frame_rate(), 29.97, abs_tol=0.001 | ||
) |