Skip to content

Commit e49f30f

Browse files
generalise update handler (#721)
* generalise update handler * cleanup * Update handlers.py * fix updating
1 parent 0df1889 commit e49f30f

File tree

4 files changed

+36
-44
lines changed

4 files changed

+36
-44
lines changed

molecularnodes/addon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import bpy
1515
from bpy.app.handlers import frame_change_pre, load_post, save_post
1616
from bpy.props import PointerProperty, CollectionProperty
17-
from .handlers import update_trajectories
17+
from .handlers import update_entities
1818
from . import entities, operators, props, session, ui
1919
from .utils import add_current_module_to_path
2020
from .ui import pref
@@ -58,7 +58,7 @@ def register():
5858

5959
save_post.append(session._pickle)
6060
load_post.append(session._load)
61-
frame_change_pre.append(update_trajectories)
61+
frame_change_pre.append(update_entities)
6262

6363
bpy.types.Scene.MNSession = session.MNSession() # type: ignore
6464
bpy.types.Object.uuid = props.uuid_property # type: ignore
@@ -83,7 +83,7 @@ def unregister():
8383

8484
save_post.remove(session._pickle)
8585
load_post.remove(session._load)
86-
frame_change_pre.remove(update_trajectories)
86+
frame_change_pre.remove(update_entities)
8787
del bpy.types.Scene.MNSession # type: ignore
8888
del bpy.types.Scene.mn # type: ignore
8989
del bpy.types.Object.mn # type: ignore

molecularnodes/entities/trajectory/dna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _create_object(self, style: str = "oxdna", name: str = "NewUniverseObject"):
5656

5757
return self.object
5858

59-
def _update_positions(self, frame: int) -> None:
59+
def set_frame(self, frame: int) -> None:
6060
super()._update_positions(frame)
6161
self._update_timestep_values()
6262

molecularnodes/handlers.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
import bpy
22
from bpy.app.handlers import persistent
3-
# from .session import update_trajectories
4-
5-
# from .session import MNSession
63

74

85
# this update function requires a self and context input, as funcitons with these inputs
96
# have ot be passed to the `update` arguments of UI properties. When the UI is updated,
107
# the function is called with the UI element being `self` and the current context being
118
# passed into the function
12-
def _update_trajectories(self, context: bpy.types.Context) -> None:
13-
"""
14-
Function for being called at various points in the updating of the UI, to ensure
15-
positions and selections of the trajectories are udpated with the new inputs
16-
"""
17-
update_trajectories(context.scene)
18-
19-
20-
def _update_trajectories_on_frame_change(self, context: bpy.types.Context) -> None:
9+
def _udpate_entities(self, context: bpy.types.Context) -> None:
2110
"""
2211
Function for being called at various points in the updating of the UI, to ensure
2312
positions and selections of the trajectories are udpated with the new inputs
2413
"""
25-
update_trajectories(context.scene)
14+
update_entities(context.scene)
2615

2716

2817
def _selection_update_trajectories(self, context: bpy.types.Context) -> None:
@@ -34,7 +23,7 @@ def _selection_update_trajectories(self, context: bpy.types.Context) -> None:
3423
if self.immutable:
3524
return
3625
else:
37-
update_trajectories(context.scene)
26+
update_entities(context.scene)
3827

3928

4029
# this is the 'perisisent' function which can be appended onto the
@@ -43,25 +32,28 @@ def _selection_update_trajectories(self, context: bpy.types.Context) -> None:
4332
# use the `frame_change_pre` handler as we want the frame to change first, then we update
4433
# the universe based on the current frame value
4534
@persistent
46-
def update_trajectories(scene):
47-
"Call the set_frame method of all trajectories in the current session"
35+
def update_entities(scene):
36+
"Call the `set_frame()` method of all entities in the current session"
4837
session = scene.MNSession
49-
for traj in session.trajectories.values():
38+
for entity in session.entities.values():
5039
# use the updated method if it exists but otherwise fallback on the old method
5140
# of updating the trajectories
52-
if hasattr(traj, "update_with_scene"):
53-
if traj.update_with_scene:
54-
traj.set_frame(scene.frame_current)
41+
42+
if hasattr(entity, "update_with_scene"):
43+
if entity.update_with_scene:
44+
frame_to_set = scene.frame_current
5545
else:
56-
traj.set_frame(traj.frame)
46+
frame_to_set = entity.frame
47+
48+
# do the entity setting, if the method isn't implemented, just pass
49+
try:
50+
entity.set_frame(frame_to_set)
51+
except NotImplementedError:
52+
pass
5753

5854
else:
59-
traj._update_positions(scene.frame_current)
60-
traj._update_selections()
61-
traj._update_calculations()
62-
63-
# except NotImplementedError:
64-
# pass
65-
# except Exception as e:
66-
# # print(f"Error updating {traj}: {e}")
67-
# raise e
55+
# this is the old method of updating the trajectories and is maintained for
56+
# backwards compatibility # TODO: takeout for later release
57+
entity._update_positions(scene.frame_current)
58+
entity._update_selections()
59+
entity._update_calculations()

molecularnodes/props.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bpy
22
from bpy.types import PropertyGroup
33
from bpy.props import IntProperty, BoolProperty, EnumProperty, StringProperty
4-
from .handlers import _selection_update_trajectories, _update_trajectories
4+
from .handlers import _selection_update_trajectories, _udpate_entities
55
from .style import STYLE_ITEMS
66

77

@@ -117,47 +117,47 @@ class MolecularNodesObjectProperties(PropertyGroup):
117117
name="Frame",
118118
description="Frame of the loaded trajectory",
119119
default=0,
120-
update=_update_trajectories,
120+
update=_udpate_entities,
121121
min=0,
122122
)
123123
update_with_scene: BoolProperty( # type: ignore
124124
name="Update with Scene",
125125
description="Update the trajectory with the scene frame",
126126
default=True,
127-
update=_update_trajectories,
127+
update=_udpate_entities,
128128
)
129129
subframes: IntProperty( # type: ignore
130130
name="Subframes",
131131
description="Number of subframes to insert between frames of the loaded trajectory",
132132
default=0,
133-
update=_update_trajectories,
133+
update=_udpate_entities,
134134
min=0,
135135
)
136136
offset: IntProperty( # type: ignore
137137
name="Offset",
138138
description="Offset the starting playback for the trajectory on the timeine. Positive starts the playback later than frame 0, negative starts it earlier than frame 0",
139139
default=0,
140-
update=_update_trajectories,
140+
update=_udpate_entities,
141141
)
142142
interpolate: BoolProperty( # type: ignore
143143
name="Interpolate",
144144
description="Whether to interpolate when using subframes",
145145
default=True,
146-
update=_update_trajectories,
146+
update=_udpate_entities,
147147
)
148148
average: IntProperty( # type: ignore
149149
name="Average",
150150
description="Average the position this number of frames either side of the current frame",
151151
default=0,
152-
update=_update_trajectories,
152+
update=_udpate_entities,
153153
min=0,
154154
soft_max=5,
155155
)
156156
correct_periodic: BoolProperty( # type: ignore
157157
name="Correct",
158158
description="Correct for periodic boundary crossing when using interpolation or averaging. Assumes cubic dimensions and only works if the unit cell is orthorhombic",
159159
default=False,
160-
update=_update_trajectories,
160+
update=_udpate_entities,
161161
)
162162
filepath_trajectory: StringProperty( # type: ignore
163163
name="Trajectory",
@@ -265,7 +265,7 @@ def execute(self, context):
265265
i = int(len(obj.mn_trajectory_selections) - 1)
266266
obj.mn_trajectory_selections[i].name = f"selection_{i + 1}"
267267
obj.mn["list_index"] = i
268-
_update_trajectories(self, context)
268+
_udpate_entities(self, context)
269269

270270
return {"FINISHED"}
271271

@@ -286,7 +286,7 @@ def execute(self, context):
286286
sel_list = obj.mn_trajectory_selections
287287
sel_list.remove(index)
288288
obj.mn.trajectory_selection_index = len(sel_list) - 1
289-
_update_trajectories(self, context)
289+
_udpate_entities(self, context)
290290

291291
return {"FINISHED"}
292292

0 commit comments

Comments
 (0)