ParticlePool
Module: terminaltexteffects.engine.effect_support.particles
New in 0.16.0
ParticlePool and ParticleReset are available on development main and are not in the published 0.15.0
package.
ParticlePool reuses helper characters for short-lived visuals such as sparks, smoke, or fragments. The pool creates
characters with Terminal.add_character(), shows and activates them when emitted, and returns them to the pool when
they are reclaimed. Particles remain ordinary EffectCharacter objects, so the effect configures their scenes, paths,
and events with the usual APIs.
Use the initializer callback for setup that survives reuse, such as building scenes and registering a reclaim event.
Use the callback passed to emit() for per-emission setup, such as paths and scene resets. ParticleReset selects
which state is cleared each time a particle is reused; by default, paths are cleared and any active path or scene is
deactivated, while scenes and event registrations are kept.
The following snippet runs inside an effect iterator, where self.terminal and self.active_characters are available:
from terminaltexteffects import Coord, EffectCharacter, ParticlePool
def initialize_spark(particle: EffectCharacter) -> None:
scene = particle.animation.new_scene(scene_id="spark")
for symbol in "*+.":
scene.add_frame(symbol, 4)
self.sparks.reclaim_on_event(particle, caller=scene) # (1)
self.sparks = ParticlePool(
self.terminal,
self.active_characters,
"*",
max_size=32,
initializer=initialize_spark,
)
def launch(particle: EffectCharacter) -> None:
scene = particle.animation.query_scene("spark")
scene.reset_scene()
path = particle.motion.new_path(speed=0.5)
path.new_waypoint(Coord(origin.column, origin.row + 3))
particle.motion.activate_path(path)
particle.animation.activate_scene(scene)
self.sparks.emit(origin, launch) # (2)
- Returns the particle to the pool when its scene completes. Registering it in the initializer keeps the registration across reuse.
- Returns
Noneinstead of a particle when every particle is in use and the pool has reachedmax_size.
Reusable particle helpers for effect-owned helper characters.
The particle API wraps a group of EffectCharacter instances created with
Terminal.add_character() and lets an effect reuse them as short-lived visual
elements such as smoke, sparks, fragments, or other transient decorations.
Particles are still normal characters: effects configure their animation
scenes, motion paths, event callbacks, layer, and appearance with the regular
EffectCharacter APIs.
ParticlePool intentionally manages only the pooling lifecycle. It tracks
which helper characters are available, creates new ones when permitted, moves
emitted particles to the requested origin, adds visible particles to the
iterator's active_characters set, and returns released particles to the
available collection. Effects retain ownership of particle behavior by passing
configuration callbacks to emit() and by choosing which state should be
cleared through ParticleReset.
ParticlePool
Manage a pool of reusable helper characters for effects.
A pool owns a set of added EffectCharacter objects and divides them into
two practical states:
- available particles, which may be acquired or emitted again
- active particles, which have been emitted and added to the effect
iterator's
active_charactersset
Particles are created with one of the configured symbols at the pool's
default coordinate. They are hidden by the terminal until emitted. emit()
positions an acquired particle, calls an effect-provided configuration
callback, sets visibility, and marks it active. reclaim() reverses that
lifecycle by optionally hiding and deactivating the character, removing it
from active_characters, and returning it to the available queue.
The pool does not define particle visuals or motion. Effects should use the
initializer callback for one-time setup, such as reusable scenes,
persistent event callbacks, or layer assignment, and use the on_emit
callback passed to emit() for per-emission setup, such as coordinates,
paths, scene resets, random targets, or scene activation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terminal
|
Terminal
|
Terminal used to create helper characters and control their visibility. |
required |
active_characters
|
MutableSet[EffectCharacter]
|
The owning effect iterator's active character set. Emitted particles are added to this set and released particles are removed from it. |
required |
symbols
|
Sequence[str] | str
|
One symbol or a sequence of symbols to use when creating
particles. When a sequence is provided, new particles choose a
symbol randomly unless |
required |
initial_count
|
int
|
Number of helper characters to create immediately. |
0
|
max_size
|
int | None
|
Maximum number of particles the pool may own. |
None
|
coord
|
Coord | None
|
Initial coordinate for newly created particles. Defaults to
|
None
|
initializer
|
Callable[[EffectCharacter], None] | None
|
Optional callback invoked once for each newly created particle. It is the right place for setup that should survive reuse. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Attributes:
| Name | Type | Description |
|---|---|---|
terminal |
Terminal that owns the added particle characters. |
|
active_characters |
Active character set supplied by the owning iterator. |
|
symbols |
Tuple of candidate symbols for lazily created particles. |
|
max_size |
Optional cap on the number of owned particles. |
|
coord |
Default creation coordinate for new particles. |
|
initializer |
Optional one-time setup callback for new particles. |
|
available |
deque[EffectCharacter]
|
Queue of particles ready to be reused. |
particles |
list[EffectCharacter]
|
All particles owned by the pool, both active and available. |
Source code in terminaltexteffects/engine/effect_support/particles.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
__init__(terminal, active_characters, symbols, *, initial_count=0, max_size=None, coord=None, initializer=None)
Initialize a particle pool and optionally preallocate particles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terminal
|
Terminal
|
Terminal used to create helper characters and control their visibility. |
required |
active_characters
|
MutableSet[EffectCharacter]
|
The effect iterator's active character set. |
required |
symbols
|
Sequence[str] | str
|
Symbol or symbols used when creating new particles. |
required |
initial_count
|
int
|
Number of particles to create immediately and place in the available queue. |
0
|
max_size
|
int | None
|
Maximum number of particles the pool may own. |
None
|
coord
|
Coord | None
|
Initial coordinate for newly created particles. Defaults to |
None
|
initializer
|
Callable[[EffectCharacter], None] | None
|
Optional callback run once for each newly created particle. Use it for persistent setup such as reusable scenes, reclaim callbacks, or layer assignment. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the pool cannot be constructed with the requested symbol set or size constraints. |
Source code in terminaltexteffects/engine/effect_support/particles.py
__len__()
Return the number of particles owned by the pool.
This includes particles currently available for reuse and particles currently active in the effect.
Source code in terminaltexteffects/engine/effect_support/particles.py
acquire(*, symbol=None, reset=DEFAULT_PARTICLE_RESET)
Check out a particle for manual configuration.
acquire() returns an available particle when possible. If the
available queue is empty and the pool has not reached max_size, a new
particle is created lazily. If the pool is capped and all particles are
checked out, None is returned.
The selected ParticleReset is applied before the particle is
returned. Passing symbol updates the particle's input_symbol and
current appearance so the new symbol is used for subsequent resets and
rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol override for the acquired particle. |
None
|
reset
|
ParticleReset
|
State reset policy to apply before returning the particle. |
DEFAULT_PARTICLE_RESET
|
Returns:
| Type | Description |
|---|---|
EffectCharacter | None
|
An |
EffectCharacter | None
|
pool has no available capacity. |
Source code in terminaltexteffects/engine/effect_support/particles.py
emit(origin, on_emit, *, symbol=None, visible=True, reset=DEFAULT_PARTICLE_RESET)
Acquire, position, prepare, show, and activate a particle.
This is the high-level particle lifecycle helper most effects should
use. It checks out a particle with acquire(), moves it to origin,
calls on_emit so the effect can create or activate paths and
scenes, sets terminal visibility, and adds the particle to
active_characters so the iterator updates it on subsequent frames.
If the pool is exhausted, on_emit is not called and None is
returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
Coord
|
Coordinate where the particle should start this emission. |
required |
on_emit
|
Callable[[EffectCharacter], None]
|
Callback that receives the acquired particle and applies per-emission behavior, such as paths, scene resets, random targets, or active scene/path selection. |
required |
symbol
|
str | None
|
Optional symbol override passed through to |
None
|
visible
|
bool
|
Whether the terminal should render the particle immediately after configuration. |
True
|
reset
|
ParticleReset
|
State reset policy passed through to |
DEFAULT_PARTICLE_RESET
|
Returns:
| Type | Description |
|---|---|
EffectCharacter | None
|
The emitted particle, or |
Source code in terminaltexteffects/engine/effect_support/particles.py
extend(particles)
Add existing helper characters to the pool as available particles.
This method adopts characters that were created elsewhere and makes them available for future reuse. It does not hide, deactivate, reset, or run the pool initializer on those characters; callers should prepare them before extending the pool if that state matters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Iterable[EffectCharacter]
|
Existing |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a particle is already owned by a pool or is listed
more than once in |
Source code in terminaltexteffects/engine/effect_support/particles.py
reclaim(character, *, hide=True, deactivate=True)
Reclaim a particle into the available queue.
reclaim() removes the particle from the effect iterator's active
set, optionally hides it, optionally deactivates its current path and
scene, and makes it eligible for future acquire() or emit()
calls. Stored paths, scenes, and event registrations are not cleared by
reclaiming; use ParticleReset on the next acquire to clear that state.
Calling reclaim() more than once for the same particle will not add
duplicate entries to the available queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
character
|
EffectCharacter
|
Particle character to return to the pool. |
required |
hide
|
bool
|
Hide the character in the terminal before making it available. |
True
|
deactivate
|
bool
|
Deactivate any active path and scene before making the character available. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in terminaltexteffects/engine/effect_support/particles.py
reclaim_on_event(character, caller, *, event=EventHandler.Event.SCENE_COMPLETE, hide=True, deactivate=True)
Register an event callback that reclaims a particle to the pool.
The registered callback calls reclaim() when character triggers
event for caller. This is useful for particles whose lifecycle is
naturally owned by a scene or path, such as smoke that should return to
the pool when its fade scene completes.
The callback registration itself is stored on the particle's
event_handler. If future acquisitions use ParticleReset with
clear_events=True, the reclaim callback is removed and must be
registered again before it can fire.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
character
|
EffectCharacter
|
Particle character that should be reclaimed. |
required |
caller
|
Scene | Path | str
|
Scene, path, or caller ID whose completion event should trigger reclamation. |
required |
event
|
Event
|
Event to listen for. Defaults to scene completion. |
SCENE_COMPLETE
|
hide
|
bool
|
Passed to |
True
|
deactivate
|
bool
|
Passed to |
True
|
Source code in terminaltexteffects/engine/effect_support/particles.py
ParticleReset
dataclass
Select mutable particle state to clear before reuse.
A particle can carry paths, scenes, event registrations, active motion,
active animation, and its last rendered appearance from a previous emission.
ParticleReset lets callers choose which of that state is discarded when
a particle is acquired from a pool.
The default reset is conservative for common particle effects: it deactivates any active path or scene and clears old paths, while preserving scenes and event registrations. This supports the typical pattern of creating reusable particle scenes and recycling callbacks once in an initializer, then building short-lived paths for each emission.
Attributes:
| Name | Type | Description |
|---|---|---|
clear_paths |
bool
|
Remove all stored motion paths from the particle. Enable this when paths are generated per emission. |
clear_scenes |
bool
|
Remove all stored animation scenes from the particle. Leave this disabled when scenes are prebuilt once and reused. |
clear_events |
bool
|
Remove all registered event actions from the particle. Leave this disabled when lifecycle callbacks, such as reclaim-on- event registrations, should survive across emissions. |
deactivate_path |
bool
|
Deactivate the currently active path, if any, before the particle is configured again. |
deactivate_scene |
bool
|
Deactivate the currently active scene, if any, before the particle is configured again. |
reset_appearance |
bool
|
Restore the particle's current visual to its
|