pybevy 0.2.1

PyBevy: A Python Real-Time Engine Built on Bevy
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
use std::sync::Arc;

use bevy::ecs::{entity::Entity, ptr::OwningPtr, world::World};
use numpy::{PyReadonlyArray1, PyReadonlyArray2};
use pybevy_core::{BatchComponent, registry::global_registry};
use pyo3::{
    exceptions::{PyTypeError, PyValueError},
    prelude::*,
    types::{PyDict, PyType},
};

use super::{
    component_layout::{ComponentLayout, FieldInfo, PrimitiveType},
    component_type::register_custom_component,
    component_wrapper::*,
    helpers::type_utils::get_python_type_name,
};

/// Batch component for custom Python @component classes.
///
/// Created via `MyComponent.from_numpy(x=xs, y=ys)` where xs, ys are numpy arrays.
/// Stores the component layout and per-field numpy arrays, then bulk-inserts into
/// wrapper storage during spawn_batch.
#[pyclass(name = "CustomComponentBatch")]
pub struct PyCustomComponentBatch {
    /// Python class for type identity and registration
    component_cls: Py<PyType>,
    /// Layout describing field offsets, types, and wrapper size
    layout: ComponentLayout,
    /// (field_index, numpy array) pairs for each specified field
    field_arrays: Vec<(usize, Py<PyAny>)>,
    /// Number of entities to spawn
    count: usize,
    /// Qualified name for the component (retained for debugging)
    #[allow(dead_code)]
    qualified_name: String,
}

#[pymethods]
impl PyCustomComponentBatch {
    #[new]
    #[pyo3(signature = (cls, **kwargs))]
    fn new(
        py: Python,
        cls: &Bound<'_, PyType>,
        kwargs: Option<&Bound<'_, PyDict>>,
    ) -> PyResult<Self> {
        // Validate: must be @component decorated
        let has_decorator = cls
            .getattr("__pybevy_component_decorated__")
            .ok()
            .and_then(|marker| marker.is_truthy().ok())
            .unwrap_or(false);
        if !has_decorator {
            return Err(PyTypeError::new_err(format!(
                "Class '{}' must be decorated with @component",
                cls.name()?
            )));
        }

        // Validate: must not be PyObject storage
        let has_pyobject_storage = cls
            .getattr("__pybevy_storage__")
            .ok()
            .and_then(|attr| attr.extract::<String>().ok())
            .map(|s| s == "pyobject")
            .unwrap_or(false);
        if has_pyobject_storage {
            return Err(PyTypeError::new_err(format!(
                "from_numpy() is not supported for components with storage=\"python\". \
                 '{}' uses PyObject storage which cannot be batch-spawned from numpy arrays.",
                cls.name()?
            )));
        }

        // Compute layout
        let layout = ComponentLayout::from_annotations(cls)?;

        let kwargs = kwargs.ok_or_else(|| {
            PyValueError::new_err("from_numpy() requires at least one keyword argument")
        })?;

        if kwargs.is_empty() {
            return Err(PyValueError::new_err(
                "from_numpy() requires at least one keyword argument",
            ));
        }

        // Validate and normalize each kwarg
        let np = py.import("numpy")?;
        let mut field_arrays = Vec::new();
        let mut expected_count: Option<(usize, String)> = None;

        for (key, value) in kwargs.iter() {
            let field_name: String = key.extract()?;

            // Find field in layout
            let (field_idx, field_info) = layout
                .fields
                .iter()
                .enumerate()
                .find(|(_, f)| f.name == field_name)
                .ok_or_else(|| {
                    PyValueError::new_err(format!(
                        "Unknown field '{}' for component '{}'. Valid fields: {:?}",
                        field_name,
                        layout.name,
                        layout.field_names()
                    ))
                })?;

            // Validate array shape based on field type
            let ndim: usize = value.getattr("ndim")?.extract()?;
            let length: usize;

            if field_info.field_type.is_composite() {
                // Vec3/Vec2: require 2D array with shape (N, 3) or (N, 2)
                let expected_cols = field_info.field_type.element_count();
                if ndim != 2 {
                    return Err(PyValueError::new_err(format!(
                        "Field '{}' ({}): expected 2D array with shape (N, {}), got {}D array",
                        field_name,
                        format!("{:?}", field_info.field_type),
                        expected_cols,
                        ndim
                    )));
                }
                let shape: Vec<usize> = value.getattr("shape")?.extract()?;
                if shape[1] != expected_cols {
                    return Err(PyValueError::new_err(format!(
                        "Field '{}' ({}): expected shape (N, {}), got (N, {})",
                        field_name,
                        format!("{:?}", field_info.field_type),
                        expected_cols,
                        shape[1]
                    )));
                }
                length = shape[0];
            } else {
                // Scalar: require 1D array
                if ndim != 1 {
                    return Err(PyValueError::new_err(format!(
                        "Field '{}' must be a 1D array, got {}D",
                        field_name, ndim
                    )));
                }
                length = value.len()?;
            }
            if let Some((prev_count, ref prev_name)) = expected_count {
                if length != prev_count {
                    return Err(PyValueError::new_err(format!(
                        "Array length mismatch: '{}' has {} elements but '{}' has {}",
                        prev_name, prev_count, field_name, length
                    )));
                }
            } else {
                expected_count = Some((length, field_name.clone()));
            }

            // Cast to correct numpy dtype
            let target_dtype = field_info.field_type.to_numpy_dtype();
            let dtype_obj = np.call_method1("dtype", (target_dtype,))?;
            let arr = np.call_method1("ascontiguousarray", (&value,))?;
            let arr = arr.call_method1("astype", (&dtype_obj,))?;

            field_arrays.push((field_idx, arr.unbind()));
        }

        let count = expected_count
            .map(|(c, _)| c)
            .ok_or_else(|| PyValueError::new_err("No arrays provided"))?;

        // Qualified name for registration
        let module = cls
            .getattr("__module__")
            .ok()
            .and_then(|m| m.extract::<String>().ok())
            .unwrap_or_default();
        let qualname = cls
            .getattr("__qualname__")
            .ok()
            .and_then(|q| q.extract::<String>().ok())
            .unwrap_or_default();
        let qualified_name = format!("{}.{}", module, qualname);

        Ok(PyCustomComponentBatch {
            component_cls: cls.clone().unbind(),
            layout,
            field_arrays,
            count,
            qualified_name,
        })
    }
}

/// Enum to hold borrowed numpy arrays of different dtypes,
/// keeping the PyReadonlyArray1 alive during the entity insertion loop.
enum ReadonlyArrayHolder<'py> {
    F32(PyReadonlyArray1<'py, f32>),
    F64(PyReadonlyArray1<'py, f64>),
    I32(PyReadonlyArray1<'py, i32>),
    I64(PyReadonlyArray1<'py, i64>),
    U32(PyReadonlyArray1<'py, u32>),
    U64(PyReadonlyArray1<'py, u64>),
    Bool(PyReadonlyArray1<'py, u8>),
    /// 2D array (N, 3) for Vec3 fields
    Vec3(PyReadonlyArray2<'py, f32>),
    /// 2D array (N, 2) for Vec2 fields
    Vec2(PyReadonlyArray2<'py, f32>),
}

/// A borrowed slice from a ReadonlyArrayHolder, typed by primitive type.
enum FieldSlice<'a> {
    F32(&'a [f32]),
    F64(&'a [f64]),
    I32(&'a [i32]),
    I64(&'a [i64]),
    U32(&'a [u32]),
    U64(&'a [u64]),
    Bool(&'a [u8]),
    /// Flat slice of f32 from (N, 3) array — stride 3 per entity
    Vec3(&'a [f32]),
    /// Flat slice of f32 from (N, 2) array — stride 2 per entity
    Vec2(&'a [f32]),
}

impl<'a> FieldSlice<'a> {
    /// Write the value at `index` into `buffer` at the given byte offset.
    #[inline(always)]
    fn write_to_buffer(&self, index: usize, buffer: &mut [u8], offset: usize) {
        match self {
            FieldSlice::F32(s) => {
                buffer[offset..offset + 4].copy_from_slice(&s[index].to_le_bytes());
            }
            FieldSlice::F64(s) => {
                buffer[offset..offset + 8].copy_from_slice(&s[index].to_le_bytes());
            }
            FieldSlice::I32(s) => {
                buffer[offset..offset + 4].copy_from_slice(&s[index].to_le_bytes());
            }
            FieldSlice::I64(s) => {
                buffer[offset..offset + 8].copy_from_slice(&s[index].to_le_bytes());
            }
            FieldSlice::U32(s) => {
                buffer[offset..offset + 4].copy_from_slice(&s[index].to_le_bytes());
            }
            FieldSlice::U64(s) => {
                buffer[offset..offset + 8].copy_from_slice(&s[index].to_le_bytes());
            }
            FieldSlice::Bool(s) => {
                buffer[offset] = s[index];
            }
            FieldSlice::Vec3(s) => {
                // 3 contiguous f32 per entity
                let base = index * 3;
                buffer[offset..offset + 4].copy_from_slice(&s[base].to_le_bytes());
                buffer[offset + 4..offset + 8].copy_from_slice(&s[base + 1].to_le_bytes());
                buffer[offset + 8..offset + 12].copy_from_slice(&s[base + 2].to_le_bytes());
            }
            FieldSlice::Vec2(s) => {
                // 2 contiguous f32 per entity
                let base = index * 2;
                buffer[offset..offset + 4].copy_from_slice(&s[base].to_le_bytes());
                buffer[offset + 4..offset + 8].copy_from_slice(&s[base + 1].to_le_bytes());
            }
        }
    }
}

/// Bridge for CustomComponentBatch that implements BatchComponent.
pub struct CustomComponentBatchBridge;

impl BatchComponent for CustomComponentBatchBridge {
    fn name(&self) -> &'static str {
        "CustomComponentBatch"
    }

    fn count(&self, _py: Python, batch: &Bound<PyAny>) -> PyResult<usize> {
        let batch = batch.extract::<PyRef<PyCustomComponentBatch>>()?;
        Ok(batch.count)
    }

    fn insert_bulk(
        &self,
        py: Python,
        batch: &Bound<PyAny>,
        entities: &[Entity],
        world: &mut World,
    ) -> PyResult<()> {
        let batch = batch.extract::<PyRef<PyCustomComponentBatch>>()?;
        let layout = &batch.layout;

        // Register the component and get its ComponentId
        let type_ptr = batch.component_cls.bind(py).as_type_ptr();
        let name = get_python_type_name(py, type_ptr);
        let component_id = register_custom_component(world, type_ptr, name);

        // Borrow all field arrays as typed slices — zero-copy, held alive for loop
        let mut holders: Vec<ReadonlyArrayHolder<'_>> =
            Vec::with_capacity(batch.field_arrays.len());
        for (_, arr) in &batch.field_arrays {
            let arr_bound = arr.bind(py);
            let field_idx = batch
                .field_arrays
                .iter()
                .position(|(_, a)| std::ptr::eq(a, arr))
                .unwrap();
            let (actual_field_idx, _) = batch.field_arrays[field_idx];
            let field_info = &layout.fields[actual_field_idx];

            let holder = match field_info.field_type {
                PrimitiveType::F32 => {
                    ReadonlyArrayHolder::F32(arr_bound.extract::<PyReadonlyArray1<f32>>()?)
                }
                PrimitiveType::F64 => {
                    ReadonlyArrayHolder::F64(arr_bound.extract::<PyReadonlyArray1<f64>>()?)
                }
                PrimitiveType::I32 => {
                    ReadonlyArrayHolder::I32(arr_bound.extract::<PyReadonlyArray1<i32>>()?)
                }
                PrimitiveType::I64 => {
                    ReadonlyArrayHolder::I64(arr_bound.extract::<PyReadonlyArray1<i64>>()?)
                }
                PrimitiveType::U32 => {
                    ReadonlyArrayHolder::U32(arr_bound.extract::<PyReadonlyArray1<u32>>()?)
                }
                PrimitiveType::U64 => {
                    ReadonlyArrayHolder::U64(arr_bound.extract::<PyReadonlyArray1<u64>>()?)
                }
                PrimitiveType::Bool => {
                    ReadonlyArrayHolder::Bool(arr_bound.extract::<PyReadonlyArray1<u8>>()?)
                }
                PrimitiveType::Vec3 => {
                    ReadonlyArrayHolder::Vec3(arr_bound.extract::<PyReadonlyArray2<f32>>()?)
                }
                PrimitiveType::Vec2 => {
                    ReadonlyArrayHolder::Vec2(arr_bound.extract::<PyReadonlyArray2<f32>>()?)
                }
            };
            holders.push(holder);
        }

        // Extract slices from holders
        let mut field_slices: Vec<(&FieldInfo, FieldSlice<'_>)> = Vec::with_capacity(holders.len());
        for (idx, holder) in holders.iter().enumerate() {
            let (field_idx, _) = batch.field_arrays[idx];
            let field_info = &layout.fields[field_idx];
            let slice =
                match holder {
                    ReadonlyArrayHolder::F32(a) => FieldSlice::F32(a.as_slice().map_err(|e| {
                        PyValueError::new_err(format!("Array not contiguous: {}", e))
                    })?),
                    ReadonlyArrayHolder::F64(a) => FieldSlice::F64(a.as_slice().map_err(|e| {
                        PyValueError::new_err(format!("Array not contiguous: {}", e))
                    })?),
                    ReadonlyArrayHolder::I32(a) => FieldSlice::I32(a.as_slice().map_err(|e| {
                        PyValueError::new_err(format!("Array not contiguous: {}", e))
                    })?),
                    ReadonlyArrayHolder::I64(a) => FieldSlice::I64(a.as_slice().map_err(|e| {
                        PyValueError::new_err(format!("Array not contiguous: {}", e))
                    })?),
                    ReadonlyArrayHolder::U32(a) => FieldSlice::U32(a.as_slice().map_err(|e| {
                        PyValueError::new_err(format!("Array not contiguous: {}", e))
                    })?),
                    ReadonlyArrayHolder::U64(a) => FieldSlice::U64(a.as_slice().map_err(|e| {
                        PyValueError::new_err(format!("Array not contiguous: {}", e))
                    })?),
                    ReadonlyArrayHolder::Bool(a) => {
                        FieldSlice::Bool(a.as_slice().map_err(|e| {
                            PyValueError::new_err(format!("Array not contiguous: {}", e))
                        })?)
                    }
                    ReadonlyArrayHolder::Vec3(a) => {
                        FieldSlice::Vec3(a.as_slice().map_err(|e| {
                            PyValueError::new_err(format!("Array not contiguous: {}", e))
                        })?)
                    }
                    ReadonlyArrayHolder::Vec2(a) => {
                        FieldSlice::Vec2(a.as_slice().map_err(|e| {
                            PyValueError::new_err(format!("Array not contiguous: {}", e))
                        })?)
                    }
                };
            field_slices.push((field_info, slice));
        }

        let wrapper_size = layout.wrapper_size;
        let data_size = layout.data_size;

        // Tight loop: write field bytes into wrapper buffer and insert via insert_by_id
        for (i, &entity_id) in entities.iter().enumerate() {
            // Create zero-initialized buffer
            let mut buffer = vec![0u8; wrapper_size.size_bytes()];

            // Write each field's value
            for (field_info, slice) in &field_slices {
                slice.write_to_buffer(i, &mut buffer, field_info.offset);
            }

            // Insert using the appropriate wrapper type
            macro_rules! insert_wrapper {
                ($size:expr, $wrapper_type:ty) => {
                    if wrapper_size == $size {
                        let mut wrapper = <$wrapper_type>::default();
                        let copy_len = data_size.min(wrapper.data.len());
                        wrapper.data[..copy_len].copy_from_slice(&buffer[..copy_len]);

                        OwningPtr::make(wrapper, |ptr| unsafe {
                            world.entity_mut(entity_id).insert_by_id(component_id, ptr);
                        });
                    }
                };
            }

            insert_wrapper!(WrapperSize::W8, ComponentWrapper8);
            insert_wrapper!(WrapperSize::W16, ComponentWrapper16);
            insert_wrapper!(WrapperSize::W32, ComponentWrapper32);
            insert_wrapper!(WrapperSize::W64, ComponentWrapper64);
            insert_wrapper!(WrapperSize::W128, ComponentWrapper128);
            insert_wrapper!(WrapperSize::W256, ComponentWrapper256);
            insert_wrapper!(WrapperSize::W512, ComponentWrapper512);
            insert_wrapper!(WrapperSize::W1024, ComponentWrapper1024);
        }

        Ok(())
    }
}

/// Register the CustomComponentBatch bridge so spawn_batch can detect it.
pub fn register_custom_batch_bridge() {
    Python::attach(|py| {
        let ptr = <PyCustomComponentBatch as pyo3::PyTypeInfo>::type_object(py).as_type_ptr();
        global_registry::register_batch_bridge(ptr, Arc::new(CustomComponentBatchBridge));
    });
}