Skip to content

Commit afdd564

Browse files
Merge pull request #38 from GazzolaLab/feat/frame-for-loop
[Feat] frame for loop
2 parents c380fb9 + 8f906a5 commit afdd564

File tree

18 files changed

+215
-164
lines changed

18 files changed

+215
-164
lines changed

examples/br2/callbacks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ def update_states(
142142
)
143143

144144
def set_keyframe(self, keyframe: int) -> None:
145-
self.bending_actuation.set_keyframe(keyframe)
146-
self.rotation_CW_actuation.set_keyframe(keyframe)
147-
self.rotation_CCW_actuation.set_keyframe(keyframe)
145+
self.bending_actuation.update_keyframe(keyframe)
146+
self.rotation_CW_actuation.update_keyframe(keyframe)
147+
self.rotation_CCW_actuation.update_keyframe(keyframe)
148148

149149

150150
class BlenderBR2CallBack(BasicCallBackBaseClass):

examples/camera_movement.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,40 @@ def main(
1212
camera_orbiting_radius: float = 1.0,
1313
):
1414

15-
# Create a new scene
15+
# Clear all mesh objects in the new scene
1616
bsr.clear_mesh_objects()
1717

1818
# Set the camera film background to transparent
1919
bsr.camera.set_film_transparent()
2020

21+
# Set the render file path
22+
bsr.camera.set_file_path(filename + "/frame")
23+
24+
# Set resolution
25+
bsr.camera.set_resolution(1920, 1080)
26+
2127
# Set the camera look at location
2228
bsr.camera.look_at = np.array([0.0, 0.0, 0.0])
2329

24-
# Set a frame at the origin
30+
# Set a pose at the origin
2531
_ = Pose(
2632
positions=np.zeros(3),
2733
directors=np.identity(3),
2834
unit_length=0.25,
2935
)
3036

31-
# Set the current frame number
32-
bsr.frame_manager.frame_current = 0
33-
34-
# Set the initial keyframe number
35-
bsr.frame_manager.set_frame_start()
36-
37-
# Set the camera orbiting keyframes
37+
# Set the camera orbiting angles
3838
angles = np.linspace(
3939
0.0, 360.0, int(frame_rate * total_time), endpoint=False
4040
)
41-
for k, angle in enumerate(angles):
41+
42+
# Set the initial frame
43+
frame_start = 0
44+
bsr.frame_manager.frame_start = frame_start
45+
46+
for frame_current, angle in bsr.frame_manager.enumerate(
47+
angles, frame_current_init=frame_start
48+
):
4249

4350
# Set the camera location
4451
bsr.camera.location = np.array(
@@ -49,35 +56,22 @@ def main(
4956
]
5057
)
5158

52-
# Update the keyframe
53-
bsr.camera.set_keyframe(bsr.frame_manager.frame_current)
54-
55-
if k != len(angles) - 1:
56-
# Update the keyframe number
57-
bsr.frame_manager.update()
58-
else:
59-
# Set the final keyframe number
60-
bsr.frame_manager.set_frame_end()
59+
# Set and update the camera in current frame
60+
bsr.camera.update_keyframe(frame_current)
6161

6262
# Set the frame rate
63-
bsr.frame_manager.set_frame_rate(fps=frame_rate)
63+
bsr.frame_manager.frame_rate = frame_rate
6464

6565
# Set the view distance
6666
bsr.set_view_distance(distance=5)
6767

68-
# Deslect all objects
68+
# Deselect all objects
6969
bsr.deselect_all()
7070

7171
# Select the camera object
7272
bsr.camera.select()
7373

74-
# Set the render file path
75-
bsr.camera.set_file_path("render/" + filename)
76-
77-
# set resolution
78-
bsr.camera.set_resolution(1920, 1080)
79-
80-
# render the scene
74+
# Render the scene
8175
bsr.camera.render(
8276
frames=np.arange(
8377
bsr.frame_manager.frame_start, bsr.frame_manager.frame_end + 1
@@ -90,4 +84,7 @@ def main(
9084

9185
if __name__ == "__main__":
9286
main()
93-
# ffmpeg -threads 8 -r 60 -i render/camera_movement_%03d.png -b:v 90M -c:v prores -pix_fmt yuva444p10le camera_movement.mov
87+
print("\n\nTo convert the frames into a video, run the following command:")
88+
print(
89+
r"ffmpeg -threads 8 -r 60 -i camera_movement/frame_%03d.png -b:v 90M -c:v prores -pix_fmt yuva444p10le camera_movement.mov"
90+
)

examples/pose_demo.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,67 +48,61 @@ def angle_to_color(angle: float) -> np.ndarray:
4848
return np.array([r, g, b, 1.0])
4949

5050

51-
def main(filename: str = "pose_demo"):
51+
def main(
52+
filename: str = "pose_demo",
53+
frame_rate: int = 60,
54+
total_time: float = 5.0,
55+
):
5256

53-
# initial values for frame rate and total time
54-
frame_rate = 60
55-
total_time = 5
56-
57-
# calculates total number of frames in the visualization
58-
total_frames = frame_rate * total_time
59-
60-
# clears all mesh objects
57+
# Clear all mesh objects in the new scene
6158
bsr.clear_mesh_objects()
6259

63-
# initializes pose instance
64-
pose_object = Pose(
60+
# Initialize pose instance
61+
pose = Pose(
6562
positions=np.array([1, 0, 0]),
6663
directors=np.eye(3),
6764
thickness_ratio=0.1,
6865
)
6966

70-
# creates an array of angles from 0 to 360 degrees
71-
angles = np.linspace(0, 360, total_frames)
67+
# Create an array of angles from 0 to 360 degrees
68+
angles = np.linspace(
69+
0.0, 360.0, int(frame_rate * total_time), endpoint=False
70+
)
7271

73-
# Set frame start
74-
bsr.frame_manager.set_frame_start()
72+
# Set the initial frame
73+
frame_start = 0
74+
bsr.frame_manager.frame_start = frame_start
7575

76-
# iterates through each angle
77-
for angle in angles:
76+
for frame_current, angle in bsr.frame_manager.enumerate(
77+
angles, frame_current_init=frame_start
78+
):
7879

79-
# defines path of of motion for positions of pose object
80+
# Define path of of motion for positions of pose object
8081
positions = [np.cos(np.radians(angle)), np.sin(np.radians(angle)), 0.0]
8182

82-
# defines directors of pose object
83+
# Define directors of pose object
8384
d2 = [-np.sin(np.radians(angle)), np.cos(np.radians(angle)), 0.0]
8485
d3 = [0, 0, 1]
8586
d1 = np.cross(d2, d3)
8687
directors = np.column_stack((d1, d2, d3))
8788

88-
# updates positions and directors of pose object at each keyframe
89-
pose_object.update_states(
89+
# Update positions and directors of pose object at each keyframe
90+
pose.update_states(
9091
positions=np.array(positions),
9192
directors=directors,
9293
)
9394

94-
# converts angle to rgb color value at each frame
95+
# Convert angle to rgb color value at each frame
9596
color = angle_to_color(angle)
9697

97-
# updates pose object's colors
98-
pose_object.update_material(color=color)
99-
100-
# sets and updates keyframes
101-
pose_object.set_keyframe(bsr.frame_manager.frame_current)
98+
# Update pose object's colors
99+
pose.update_material(color=color)
102100

103-
if bsr.frame_manager.frame_current == total_frames - 1:
104-
# Set the final keyframe
105-
bsr.frame_manager.set_frame_end()
106-
else:
107-
# updates frame
108-
bsr.frame_manager.update()
101+
# Set and update the pose in current frame
102+
pose.update_keyframe(frame_current)
109103

110104
# Set the frame rate
111-
bsr.frame_manager.set_frame_rate(fps=frame_rate)
105+
bsr.frame_manager.frame_rate = frame_rate
112106

113107
# Set the view distance
114108
bsr.set_view_distance(distance=5)

examples/single_rigid_rod_spring_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def analytical_solution(t, y0: float, v0: float):
6161
# update the rod
6262
keyframe = int(time_index / simulation_ratio) + 1
6363
rod.update_states(positions=positions, radii=radii)
64-
rod.set_keyframe(keyframe)
64+
rod.update_keyframe(keyframe)
6565

6666
bsr.save("single_rigid_rod_spring_action.blend")
6767

src/bsr/_camera.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def select(self) -> None:
3939
"""
4040
bpy.context.view_layer.objects.active = self._camera
4141

42-
def set_keyframe(self, keyframe: int) -> None:
42+
def update_keyframe(self, keyframe: int) -> None:
4343
"""
4444
Sets a keyframe at the given frame.
4545

src/bsr/_light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def location(self, location: np.ndarray) -> None:
4646
"""
4747
self._light.location = location
4848

49-
def set_keyframe(self, keyframe: int) -> None:
49+
def update_keyframe(self, keyframe: int) -> None:
5050
"""
5151
Sets a keyframe at the given frame.
5252

0 commit comments

Comments
 (0)