nucleation 0.7.0

A high-performance Minecraft schematic parser and utility library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
//! Construction-shaped animation API exposed through every generated binding.

use crate::bridge::shared::ffi::NucleationError;

fn utf8(bytes: &[u8]) -> Result<&str, NucleationError> {
    std::str::from_utf8(bytes).map_err(|_| NucleationError::InvalidArgument)
}

fn exclusions_json(bytes: &[u8]) -> Result<Vec<String>, NucleationError> {
    serde_json::from_str(utf8(bytes)?).map_err(|_| NucleationError::InvalidArgument)
}

fn property(name: &str) -> Result<crate::animation::Property, NucleationError> {
    use crate::animation::Property::*;
    match name {
        "x" => Ok(X),
        "y" => Ok(Y),
        "z" => Ok(Z),
        "rotateX" | "rotate_x" => Ok(RotX),
        "rotateY" | "rotate_y" => Ok(RotY),
        "rotateZ" | "rotate_z" => Ok(RotZ),
        "scale" => Ok(ScaleUniform),
        "scaleX" | "scale_x" => Ok(ScaleX),
        "scaleY" | "scale_y" => Ok(ScaleY),
        "scaleZ" | "scale_z" => Ok(ScaleZ),
        "opacity" => Ok(Opacity),
        "tintR" | "tint_r" => Ok(TintR),
        "tintG" | "tint_g" => Ok(TintG),
        "tintB" | "tint_b" => Ok(TintB),
        "tintA" | "tint_a" => Ok(TintA),
        "emissiveR" | "emissive_r" => Ok(EmissiveR),
        "emissiveG" | "emissive_g" => Ok(EmissiveG),
        "emissiveB" | "emissive_b" => Ok(EmissiveB),
        _ => Err(NucleationError::InvalidArgument),
    }
}

fn easing(name: &str) -> Result<crate::animation::Easing, NucleationError> {
    use crate::animation::{Easing, Power};
    match name {
        "linear" => Ok(Easing::Linear),
        "inQuad" | "in_quad" => Ok(Easing::In(Power::Quad)),
        "outQuad" | "out_quad" => Ok(Easing::Out(Power::Quad)),
        "inOutQuad" | "in_out_quad" => Ok(Easing::InOut(Power::Quad)),
        "inCubic" | "in_cubic" => Ok(Easing::In(Power::Cubic)),
        "outCubic" | "out_cubic" => Ok(Easing::Out(Power::Cubic)),
        "inOutCubic" | "in_out_cubic" => Ok(Easing::InOut(Power::Cubic)),
        "inOutSine" | "in_out_sine" => Ok(Easing::InOut(Power::Sine)),
        "outBack" | "out_back" => Ok(Easing::out_back()),
        _ => Err(NucleationError::InvalidArgument),
    }
}

#[diplomat::bridge]
pub mod ffi {
    use super::super::building::ffi::{Brush, Shape};
    #[cfg(feature = "meshing")]
    use super::super::meshing::ffi::ResourcePack;
    #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
    use super::super::rendering::ffi::RenderConfig;
    use super::super::schematic::ffi::Schematic;
    use super::super::shared::ffi::NucleationError;
    use super::{easing, exclusions_json, property, utf8};
    use diplomat_runtime::DiplomatWrite;
    use std::fmt::Write;

    /// A reusable set of property tracks, modelled after Anime.js object animations.
    #[diplomat::opaque_mut]
    pub struct AnimationEffect(pub(crate) crate::animation::AnimationEffect);

    impl AnimationEffect {
        pub fn create(duration_ms: f32) -> Box<Self> {
            Box::new(Self(crate::animation::AnimationEffect::new(duration_ms)))
        }

        pub fn instant() -> Box<Self> {
            Self::create(0.0)
        }

        pub fn pop_in(duration_ms: f32) -> Box<Self> {
            Box::new(Self(crate::animation::presets::pop_in(duration_ms).into()))
        }

        pub fn drop_in(duration_ms: f32, height: f32) -> Box<Self> {
            Box::new(Self(
                crate::animation::presets::drop_in(duration_ms, height).into(),
            ))
        }

        pub fn drop_and_pop(duration_ms: f32, height: f32) -> Box<Self> {
            Box::new(Self(
                crate::animation::presets::drop_and_pop(duration_ms, height).into(),
            ))
        }

        pub fn spin_in(duration_ms: f32, turns: f32) -> Box<Self> {
            Box::new(Self(
                crate::animation::presets::spin_in(duration_ms, turns).into(),
            ))
        }

        pub fn turntable(duration_ms: f32) -> Box<Self> {
            Box::new(Self(
                crate::animation::presets::turntable(duration_ms).into(),
            ))
        }

        /// Add a two-key property tween. Property names follow Anime.js/Three.js:
        /// `x`, `y`, `z`, `rotateX`, `rotateY`, `rotateZ`, `scale`, `opacity`,
        /// `tintR/G/B/A`, and `emissiveR/G/B`.
        pub fn add_tween(
            &mut self,
            property_name: &DiplomatStr,
            from: f32,
            to: f32,
            easing_name: &DiplomatStr,
        ) -> Result<(), NucleationError> {
            let p = property(utf8(property_name)?)?;
            let e = easing(utf8(easing_name)?)?;
            self.0 = self.0.clone().tween(p, from, to, e);
            Ok(())
        }

        /// Add a normalised keyframe (`at` in `0..=1`) to a property track.
        pub fn add_keyframe(
            &mut self,
            property_name: &DiplomatStr,
            at: f32,
            value: f32,
            easing_name: &DiplomatStr,
        ) -> Result<(), NucleationError> {
            let p = property(utf8(property_name)?)?;
            let e = easing(utf8(easing_name)?)?;
            self.0 = self.0.clone().keyframe(p, at, value, e);
            Ok(())
        }

        pub fn set_repeat_forever(&mut self) {
            self.0 = self.0.clone().repeat_forever();
        }
    }

    /// Native FFmpeg video output preset.
    #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
    #[diplomat::opaque_mut]
    pub struct VideoConfig(pub(crate) crate::rendering::VideoConfig);

    #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
    impl VideoConfig {
        /// Alpha-preserving ProRes 4444 in a MOV container.
        #[diplomat::attr(js, disable)]
        pub fn prores_4444(fps: f64) -> Result<Box<Self>, NucleationError> {
            crate::rendering::VideoConfig::prores_4444(fps)
                .map(Self)
                .map(Box::new)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        /// H.264 in an MP4 or MOV container. H.264 does not preserve alpha.
        #[diplomat::attr(js, disable)]
        pub fn h264(fps: f64) -> Result<Box<Self>, NucleationError> {
            crate::rendering::VideoConfig::h264(fps)
                .map(Self)
                .map(Box::new)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        /// Override the FFmpeg executable. The default resolves `ffmpeg` on PATH.
        #[diplomat::attr(js, disable)]
        pub fn set_ffmpeg_path(&mut self, path: &DiplomatStr) -> Result<(), NucleationError> {
            self.0.set_ffmpeg_path(utf8(path)?);
            Ok(())
        }
    }

    /// A schematic wrapper that records construction calls as animation targets.
    #[diplomat::opaque_mut]
    pub struct BuildAnimation(pub(crate) crate::animation::BuildAnimation);

    impl BuildAnimation {
        pub fn create(name: &DiplomatStr) -> Box<Self> {
            let name = std::str::from_utf8(name).unwrap_or("animation");
            Box::new(Self(crate::animation::BuildAnimation::new(name)))
        }

        /// Clone an existing schematic into one animation group. Entity-only
        /// schematics are supported; overlapping block/entity integer positions
        /// are rejected rather than silently dropping a model.
        pub fn from_schematic(schematic: &Schematic) -> Result<Box<Self>, NucleationError> {
            crate::animation::BuildAnimation::from_schematic(&schematic.0)
                .map(Self)
                .map(Box::new)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        /// Apply one effect to every existing animation group.
        pub fn animate_all(&mut self, effect: &AnimationEffect) {
            self.0.animate_all(effect.0.clone());
        }

        pub fn set_default_effect(&mut self, effect: &AnimationEffect) {
            self.0.set_default_effect(effect.0.clone());
        }

        /// Apply an effect to exactly the next recorded operation or explicit group.
        /// The returned borrowed builder enables fluent calls in every generated binding.
        pub fn with_effect<'a>(&'a mut self, effect: &AnimationEffect) -> &'a mut BuildAnimation {
            self.0.with_effect(effect.0.clone());
            self
        }

        pub fn set_step_ms(&mut self, step_ms: f32) {
            self.0.set_step_ms(step_ms);
        }

        pub fn set_stagger_total_ms(&mut self, total_ms: f32) {
            self.0.set_stagger_total_ms(Some(total_ms));
        }

        pub fn clear_stagger(&mut self) {
            self.0.set_stagger_total_ms(None);
        }

        /// Shift every construction group's start time. Negative offsets let a
        /// repeating staggered effect cross the beginning of a loop capture.
        pub fn set_stagger_offset_ms(&mut self, offset_ms: f32) {
            self.0.set_stagger_offset_ms(offset_ms);
        }

        /// Capture exactly one loop period, excluding the duplicate endpoint.
        /// The rounded frame count evenly partitions the complete period.
        pub fn set_loop_period_ms(&mut self, period_ms: f32) -> Result<(), NucleationError> {
            if !period_ms.is_finite() || period_ms <= 0.0 {
                return Err(NucleationError::InvalidArgument);
            }
            self.0.set_loop_period_ms(Some(period_ms));
            Ok(())
        }

        pub fn clear_loop_period(&mut self) {
            self.0.set_loop_period_ms(None);
        }

        pub fn begin_group(&mut self) -> Result<(), NucleationError> {
            self.0
                .begin_group(None)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn begin_keyed_group(&mut self, key: f32) -> Result<(), NucleationError> {
            self.0
                .begin_group_with_key(None, key)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn end_group(&mut self) -> Result<u32, NucleationError> {
            self.0
                .end_group()
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn set_block(
            &mut self,
            x: i32,
            y: i32,
            z: i32,
            block: &DiplomatStr,
        ) -> Result<u32, NucleationError> {
            self.0
                .set_block(x, y, z, utf8(block)?)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn create_region(
            &mut self,
            name: &DiplomatStr,
            min_x: i32,
            min_y: i32,
            min_z: i32,
            max_x: i32,
            max_y: i32,
            max_z: i32,
        ) -> Result<(), NucleationError> {
            self.0
                .create_region(utf8(name)?, (min_x, min_y, min_z), (max_x, max_y, max_z))
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn set_block_in_region(
            &mut self,
            region: &DiplomatStr,
            x: i32,
            y: i32,
            z: i32,
            block: &DiplomatStr,
        ) -> Result<u32, NucleationError> {
            self.0
                .set_block_in_region(utf8(region)?, x, y, z, utf8(block)?)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn translate(
            &mut self,
            x: i32,
            y: i32,
            z: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .translate(x, y, z, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn translate_region(
            &mut self,
            region: &DiplomatStr,
            x: i32,
            y: i32,
            z: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .translate_region(utf8(region)?, x, y, z, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn translate_all(
            &mut self,
            x: i32,
            y: i32,
            z: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .translate_all(x, y, z, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn rotate_x(&mut self, degrees: i32, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .rotate_x(degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_y(&mut self, degrees: i32, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .rotate_y(degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_z(&mut self, degrees: i32, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .rotate_z(degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_region_x(
            &mut self,
            region: &DiplomatStr,
            degrees: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .rotate_region_x(utf8(region)?, degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_region_y(
            &mut self,
            region: &DiplomatStr,
            degrees: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .rotate_region_y(utf8(region)?, degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_region_z(
            &mut self,
            region: &DiplomatStr,
            degrees: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .rotate_region_z(utf8(region)?, degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_all_x(
            &mut self,
            degrees: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .rotate_all_x(degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_all_y(
            &mut self,
            degrees: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .rotate_all_y(degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn rotate_all_z(
            &mut self,
            degrees: i32,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .rotate_all_z(degrees, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn flip_x(&mut self, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .flip_x(duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_y(&mut self, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .flip_y(duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_z(&mut self, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .flip_z(duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_region_x(
            &mut self,
            region: &DiplomatStr,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .flip_region_x(utf8(region)?, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_region_y(
            &mut self,
            region: &DiplomatStr,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .flip_region_y(utf8(region)?, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_region_z(
            &mut self,
            region: &DiplomatStr,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            self.0
                .flip_region_z(utf8(region)?, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_all_x(&mut self, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .flip_all_x(duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_all_y(&mut self, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .flip_all_y(duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }
        pub fn flip_all_z(&mut self, duration_ms: f32) -> Result<(), NucleationError> {
            self.0
                .flip_all_z(duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn stamp_region(
            &mut self,
            source: &Schematic,
            region: &DiplomatStr,
            x: i32,
            y: i32,
            z: i32,
            exclusions: &DiplomatStr,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            let excluded = exclusions_json(exclusions)?;
            self.0
                .stamp_region(&source.0, utf8(region)?, (x, y, z), &excluded, duration_ms)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn stamp_box(
            &mut self,
            source: &Schematic,
            min_x: i32,
            min_y: i32,
            min_z: i32,
            max_x: i32,
            max_y: i32,
            max_z: i32,
            x: i32,
            y: i32,
            z: i32,
            exclusions: &DiplomatStr,
            duration_ms: f32,
        ) -> Result<(), NucleationError> {
            let excluded = exclusions_json(exclusions)?;
            self.0
                .stamp_box(
                    &source.0,
                    crate::BoundingBox::new((min_x, min_y, min_z), (max_x, max_y, max_z)),
                    (x, y, z),
                    &excluded,
                    duration_ms,
                )
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn set_operation_gizmos(&mut self, enabled: bool) {
            self.0.set_operation_gizmos(enabled);
        }

        pub fn operations_json(&self, out: &mut DiplomatWrite) -> Result<(), NucleationError> {
            let json = self
                .0
                .operations_json()
                .map_err(|_| NucleationError::Serialize)?;
            write!(out, "{}", json).map_err(|_| NucleationError::Serialize)
        }

        pub fn frame_json(
            &self,
            time_ms: f32,
            out: &mut DiplomatWrite,
        ) -> Result<(), NucleationError> {
            let json = serde_json::to_string(&self.0.frame_at(time_ms))
                .map_err(|_| NucleationError::Serialize)?;
            write!(out, "{}", json).map_err(|_| NucleationError::Serialize)
        }

        /// Fill a parametric shape and record its voxels as ordered groups in
        /// the same transactional construction operation.
        pub fn fill_along_parameter(
            &mut self,
            shape: &Shape,
            brush: &Brush,
            group_count: u32,
        ) -> Result<u32, NucleationError> {
            self.0
                .fill_along_parameter(&shape.0, &brush.0, group_count as usize)
                .map(|groups| groups.len() as u32)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn add_armor_stand(
            &mut self,
            x: f64,
            y: f64,
            z: f64,
            yaw: f32,
            armor_material: &DiplomatStr,
        ) -> Result<u32, NucleationError> {
            let material = utf8(armor_material)?;
            let equipment = if material.is_empty() {
                crate::entity::ArmorStandEquipment::default()
            } else {
                crate::entity::ArmorStandEquipment::full_set(material)
            };
            self.0
                .schematic_mut()
                .add_entity(crate::entity::Entity::armor_stand(
                    (x, y, z),
                    yaw,
                    equipment,
                ));
            self.0
                .record_entity_position(x, y, z)
                .map_err(|_| NucleationError::InvalidArgument)
        }

        pub fn animate_camera(&mut self, effect: &AnimationEffect, offset_ms: f32) {
            self.0.animate_camera(effect.0.clip().clone(), offset_ms);
        }

        pub fn frame_count(&self, fps: f64, hold_ms: f32) -> u32 {
            self.0.frames(fps, hold_ms).len() as u32
        }

        /// Render directly to a looping GIF. The renderer, meshes, timeline and
        /// GIF encoder all live in the Rust core; no ffmpeg subprocess is needed.
        #[diplomat::attr(js, disable)]
        #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
        pub fn render_gif(
            &self,
            pack_zip: &[u8],
            config: &RenderConfig,
            path: &DiplomatStr,
            fps: f64,
            hold_ms: f32,
        ) -> Result<u32, NucleationError> {
            let path = utf8(path)?;
            let pack = crate::meshing::ResourcePackSource::from_bytes(pack_zip)
                .map_err(|_| NucleationError::Parse)?;
            let meshes = self
                .0
                .mesh_outputs(&pack, &crate::meshing::MeshConfig::default())
                .map_err(|_| NucleationError::Mesh)?;
            let frames = self.0.frames(fps, hold_ms);
            let pixels = crate::rendering::render_animation(&meshes, &frames, &config.0, None)
                .map_err(|_| NucleationError::Render)?;
            crate::rendering::write_animation_gif(
                &pixels,
                config.0.width,
                config.0.height,
                fps,
                path,
            )
            .map_err(|_| NucleationError::Io)?;
            Ok(frames.len() as u32)
        }

        /// Render and stream with an already parsed resource pack.
        #[diplomat::attr(js, disable)]
        #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
        pub fn render_video_with_pack(
            &self,
            pack: &ResourcePack,
            config: &RenderConfig,
            video: &VideoConfig,
            path: &DiplomatStr,
            hold_ms: f32,
        ) -> Result<u32, NucleationError> {
            let path = utf8(path)?;
            let meshes = self
                .0
                .mesh_outputs(&pack.0, &crate::meshing::MeshConfig::default())
                .map_err(|_| NucleationError::Mesh)?;
            let frames = self.0.frames(video.0.fps, hold_ms);
            crate::rendering::render_animation_to_video(
                &meshes,
                &frames,
                &config.0,
                None,
                &video.0,
                std::path::Path::new(path),
            )
            .map_err(|_| NucleationError::Render)?;
            Ok(frames.len() as u32)
        }

        /// Render and stream directly to video. The GPU renderer and meshes are
        /// reused for the complete animation and no frame sequence is retained.
        #[diplomat::attr(js, disable)]
        #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
        pub fn render_video(
            &self,
            pack_zip: &[u8],
            config: &RenderConfig,
            video: &VideoConfig,
            path: &DiplomatStr,
            hold_ms: f32,
        ) -> Result<u32, NucleationError> {
            let path = utf8(path)?;
            let pack = crate::meshing::ResourcePackSource::from_bytes(pack_zip)
                .map_err(|_| NucleationError::Parse)?;
            let meshes = self
                .0
                .mesh_outputs(&pack, &crate::meshing::MeshConfig::default())
                .map_err(|_| NucleationError::Mesh)?;
            let frames = self.0.frames(video.0.fps, hold_ms);
            crate::rendering::render_animation_to_video(
                &meshes,
                &frames,
                &config.0,
                None,
                &video.0,
                std::path::Path::new(path),
            )
            .map_err(|_| NucleationError::Render)?;
            Ok(frames.len() as u32)
        }

        /// Render numbered PNG frames (`prefix0000.png`, ...) for an external
        /// compositor while using the exact same public timeline API.
        #[diplomat::attr(js, disable)]
        #[cfg(all(feature = "rendering", not(target_arch = "wasm32")))]
        pub fn render_frames(
            &self,
            pack_zip: &[u8],
            config: &RenderConfig,
            prefix: &DiplomatStr,
            fps: f64,
            hold_ms: f32,
        ) -> Result<u32, NucleationError> {
            let prefix = utf8(prefix)?;
            let pack = crate::meshing::ResourcePackSource::from_bytes(pack_zip)
                .map_err(|_| NucleationError::Parse)?;
            let meshes = self
                .0
                .mesh_outputs(&pack, &crate::meshing::MeshConfig::default())
                .map_err(|_| NucleationError::Mesh)?;
            let frames = self.0.frames(fps, hold_ms);
            crate::rendering::render_animation_to_files(&meshes, &frames, &config.0, None, prefix)
                .map_err(|_| NucleationError::Render)?;
            Ok(frames.len() as u32)
        }

        #[diplomat::attr(js, disable)]
        pub fn save_to_file(&self, path: &DiplomatStr) -> Result<(), NucleationError> {
            use crate::formats::manager::get_manager;
            let path = utf8(path)?;
            let manager = get_manager();
            let manager = manager.lock().map_err(|_| NucleationError::Lock)?;
            let bytes = manager
                .write_auto_with_settings(path, self.0.schematic(), None, None)
                .map_err(|_| NucleationError::Serialize)?;
            std::fs::write(path, bytes).map_err(|_| NucleationError::Io)
        }

        pub fn group_count(&self) -> u32 {
            self.0.groups().len() as u32
        }

        pub fn duration_ms(&self) -> f32 {
            self.0.duration_ms()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::ffi::{AnimationEffect, BuildAnimation};
    use crate::bridge::building::ffi::{Brush, Curve3D, Shape};

    #[test]
    fn bridge_builds_and_groups_a_sampled_curve_in_one_bulk_operation() {
        let curve = Curve3D::from_points(&[0.0, 0.0, 0.0, 9.0, 0.0, 0.0], false).unwrap();
        let shape = Shape::tube_along(&curve, 0.6).unwrap();
        let brush = Brush::solid(b"minecraft:stone").unwrap();
        let mut animation = BuildAnimation::create(b"curve");

        assert_eq!(
            animation.fill_along_parameter(&shape, &brush, 4).unwrap(),
            4
        );
        animation.set_stagger_offset_ms(-1_000.0);
        animation.set_loop_period_ms(1_000.0).unwrap();
        assert_eq!(animation.frame_count(20.0, 5_000.0), 20);
    }

    #[test]
    fn bridge_exposes_transactional_region_operations_and_receipts() {
        let mut animation = BuildAnimation::create(b"operations");
        animation
            .create_region(b"wing", 10, 0, 10, 11, 0, 10)
            .unwrap();
        animation
            .set_block_in_region(b"wing", 10, 0, 10, b"minecraft:oak_stairs[facing=east]")
            .unwrap();
        animation.rotate_region_y(b"wing", -270, 250.0).unwrap();
        assert_eq!(animation.0.operation_count(), 1);
        assert_eq!(animation.0.operation_receipts()[0].duration_ms, 250.0);
        assert!(animation.rotate_region_y(b"wing", 45, 250.0).is_err());
        assert_eq!(animation.0.operation_count(), 1);
        let receipt = &animation.0.operation_receipts()[0];
        let frame = animation
            .0
            .frame_at(receipt.start_ms + receipt.duration_ms * 0.5);
        assert!(!frame.gizmos.is_empty());
    }

    #[test]
    fn bridge_builder_exposes_group_default_override_and_camera_controls() {
        let mut animation = BuildAnimation::create(b"bridge");
        let default = AnimationEffect::drop_and_pop(480.0, 4.5);
        animation.set_default_effect(&default);
        animation.begin_group().unwrap();
        animation.set_block(0, 0, 0, b"minecraft:stone").unwrap();
        animation.set_block(1, 0, 0, b"minecraft:stone").unwrap();
        assert_eq!(animation.end_group().unwrap(), 0);
        let spin = AnimationEffect::spin_in(600.0, 1.0);
        assert_eq!(
            animation
                .with_effect(&spin)
                .set_block(0, 1, 0, b"minecraft:furnace")
                .unwrap(),
            1
        );
        let camera = AnimationEffect::turntable(2_000.0);
        animation.animate_camera(&camera, 0.0);
        assert_eq!(animation.group_count(), 2);
        assert!(animation.duration_ms() >= 2_000.0);
        assert_eq!(animation.frame_count(20.0, 1_000.0), 61);
    }
}