Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

fix(export): Selection as Project exports a selected bin's contents

Max Murphy5b6e1ce720bd
diff --git a/crates/engine/src/project_tools.rs b/crates/engine/src/project_tools.rs
index 0dce9fdcd..c0390d9dc 100644
--- a/crates/engine/src/project_tools.rs
+++ b/crates/engine/src/project_tools.rs
@@ -24,7 +24,7 @@
use std::collections::BTreeSet;
-use filmcraft_project::{BinEntry, FindOp, FindQuery, FindRow, ItemId, ItemKind, MarkerKind, MediaRef, Project, SearchBin, TrackKind};
+use filmcraft_project::{BinEntry, BinId, FindOp, FindQuery, FindRow, ItemId, ItemKind, MarkerKind, MediaRef, Project, SearchBin, TrackKind};
use filmcraft_time::{Tick, TimeDisplay, TimeRange};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -699,14 +699,30 @@ fn new_from_template(s: &mut Session, p: &Value) -> Result<Value> {
/// The selected items plus everything they need: sequence contents (recursively), subclip
/// parents, multi-camera / merged-clip sources and graphic canvases.
+///
+/// Bins and items share one id space, so a named bin contributes every item it holds (and
+/// everything those items need), as `project.delete` does. The project's own top bin does not,
+/// since no command acts on it (#244), and only ids that name a real item end up in the result.
pub fn closure(p: &Project, roots: &[ItemId]) -> BTreeSet<ItemId> {
let mut keep: BTreeSet<ItemId> = BTreeSet::new();
let mut todo: Vec<ItemId> = roots.to_vec();
+ let mut seen_bins: BTreeSet<u64> = BTreeSet::new();
while let Some(id) = todo.pop() {
- if !keep.insert(id) {
+ let bin = BinId(id.0);
+ if bin != p.root.id && p.root.find_bin(bin).is_some() {
+ if seen_bins.insert(id.0)
+ && let Some(b) = p.root.find_bin(bin)
+ {
+ let mut items = Vec::new();
+ b.all_items(&mut items);
+ todo.extend(items);
+ }
continue;
}
let Some(it) = p.item(id) else { continue };
+ if !keep.insert(id) {
+ continue;
+ }
match &it.kind {
ItemKind::Subclip { parent, .. } => todo.push(*parent),
ItemKind::Sequence(q) => {
@@ -754,6 +770,9 @@ fn export_selection(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad("file.exportSelectionProject", "select items in the Project panel"));
}
let keep = closure(&s.project, &roots);
+ if keep.is_empty() {
+ return Err(bad("file.exportSelectionProject", "the selection has no items to export"));
+ }
let mut sub = project_subset(&s.project, &keep);
let name = std::path::Path::new(&path).file_stem().map(|x| x.to_string_lossy().to_string()).unwrap_or_else(|| sub.name.clone());
sub.name = name.clone();
diff --git a/crates/engine/src/project_tools_tests.rs b/crates/engine/src/project_tools_tests.rs
index 11b7bd497..e00af7326 100644
--- a/crates/engine/src/project_tools_tests.rs
+++ b/crates/engine/src/project_tools_tests.rs
@@ -220,6 +220,59 @@ fn export_selection_project_and_ale() {
assert!(doc.rows.iter().any(|r| r[0] == "Ambient_Score.wav" && r[1] == "A1A2"));
}
+#[test]
+fn export_selection_project_of_a_bin_exports_its_contents() {
+ let mut s = demo();
+ let (footage_bin, bin_items) = s
+ .project
+ .root
+ .children
+ .iter()
+ .find_map(|c| match c {
+ filmcraft_project::BinEntry::Bin(b) if b.name == "Footage" => {
+ let mut items = Vec::new();
+ b.all_items(&mut items);
+ Some((b.id.0, items))
+ }
+ _ => None,
+ })
+ .expect("Footage bin");
+ assert_eq!(bin_items.len(), 6);
+
+ // Selecting the bin (its id, not its items) must export the bin's contents, not an empty project.
+ s.execute("project.select", json!({"items": [footage_bin]})).unwrap();
+ let dir = tmp_dir("export-bin");
+ let path = dir.join("Footage.fcproj").to_string_lossy().to_string();
+ let r = s.execute("file.exportSelectionProject", json!({"path": path})).unwrap();
+ let p = filmcraft_format::decode(&std::fs::read(&path).unwrap()).unwrap().project;
+ assert_eq!(r["items"].as_array().unwrap().len(), p.items.len(), "reported items match the saved project");
+ let reported: std::collections::BTreeSet<ItemId> = r["items"].as_array().unwrap().iter().map(|v| ItemId(v.as_u64().unwrap())).collect();
+ assert_eq!(reported, bin_items.iter().copied().collect(), "the bin's items are the exported items");
+ for n in ["Ocean_Sunset.mp4", "Neon_Loop.mov", "Misty_Forest.mp4"] {
+ assert!(p.items.values().any(|i| i.name == n), "{n}");
+ }
+ assert!(!p.items.values().any(|i| i.name == "Ambient_Score.wav"), "the Audio bin stayed behind");
+ let mut listed = Vec::new();
+ p.root.all_items(&mut listed);
+ assert!(p.items.keys().all(|k| listed.contains(k) || matches!(p.item(*k).unwrap().kind, ItemKind::Graphic { .. })));
+
+ // A mixed bin + item request must not report the bin id, which names no item in the output.
+ let explicit = dir.join("Mixed.fcproj").to_string_lossy().to_string();
+ let r = s.execute("file.exportSelectionProject", json!({"items": [footage_bin, bin_items[0].0], "path": explicit})).unwrap();
+ let p = filmcraft_format::decode(&std::fs::read(&explicit).unwrap()).unwrap().project;
+ for v in r["items"].as_array().unwrap() {
+ let id = ItemId(v.as_u64().unwrap());
+ assert!(p.item(id).is_some(), "reported item {id:?} is in the output");
+ }
+
+ // An empty bin has nothing to export: an actionable error, no misleading empty file.
+ let empty_bin = s.execute("file.newBin", json!({"name": "Empty"})).unwrap()["bin"].as_u64().unwrap();
+ s.execute("project.select", json!({"items": [empty_bin]})).unwrap();
+ let empty_path = dir.join("Empty.fcproj").to_string_lossy().to_string();
+ assert!(s.execute("file.exportSelectionProject", json!({"path": empty_path})).is_err());
+ assert!(!std::path::Path::new(&empty_path).exists(), "no empty project written");
+}
+
#[test]
fn media_properties_settings_and_scratch_disks() {
let dir = tmp_dir("props");