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
use crate::utils::{self, Property, LibraryId, ObjectId};
use crate::mesh::Mesh;
use blender;
use crate::catmullclark;
use crate::enum_set::*;
use na::Vec3;
use crate::loader;
use crate::scene::SceneData;
use crate::modifiers;
use crate::curves;
use crate::trimesh::{self, TriMesh};
use hashbrown::HashMap;

use std::mem;



#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(u16)]
pub enum RigidBodyType{
    Active=0,
    Passive,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(u16)]
pub enum RigidBodyShape{
    Cuboid,
    Sphere,
    Capsule,
    Cylinder,
    Cone,
    ConvexHull,
    Mesh,
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone,Debug,Copy)]
#[repr(C)]
pub struct RigidBody{
    pub ty: RigidBodyType,
    pub shape: RigidBodyShape,
    pub friction: f32,
}


/// Model holds the original blender Mesh for an object with data of Mesh type
///
/// It also contains:
/// - references to a skeleton if the original object referenced one
/// - vertex groups (used to apply skinning)
/// - flattened trimeshes
/// - original material name for each flattened trimesh
///
/// The original blender mesh format can specify materials per face this class
/// flattens that into several trimeshes which can be of 2 types:
///
/// - One buffer of vertices accesible through original_vertices() or submeshes()[0]
///   plus several submesh indices groups accesible thorugh submeshes_indices
/// - Several buffers of vertices accesible through submeshes() without indices
///
/// To check which type the model provides check if submeshes_indices is Some/None
/// respectively
///
/// The flattened meshes can also be accessed using submeshes which return an iterator
/// of SubMesh with vertices, indices (if any) and material. In this case when the
/// flattened trimesh is one buffer of vertices + indices the returned vertices in each
/// Submesh will be always the same
///
/// For skinning all vertices in the mesh you can access original_vertices() which contains
/// all the vertices in the correct order
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone)]
pub struct Model{
    name: ObjectId,
    node: utils::Transformations,
    blend_mesh_id: ObjectId,
    skeleton_name: Option<ObjectId>,
    vertex_groups: Vec<String>,
    vertex_groups_index: Vec<Option<usize>>,
    default_group: Option<String>,
    rigid_body: Option<RigidBody>,
    selectable: bool,
    visible: bool,
    default_action_name: Option<ObjectId>,
    custom_properties: Vec<Property>,
    animated_vertices: Vec<trimesh::Vertex>,
    drivers: Vec<curves::FCurve>,
    deformflag: ArmatureDeformFlag,
}


// fn find_armature_parent(obj: &blender::Object) -> Option<String>{
//     if let Ok(parent) = obj.get_object("parent"){
//         if blender::ObjectType::Armature == *parent.get("type").unwrap(){
//             parent.name().ok().map(|name| name.to_string())
//         }else{
//             find_armature_parent(&parent)
//         }
//     }else{
//         None
//     }
// }


bitflags!{
    #[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
    pub struct ArmatureDeformFlag: u16 {
        const VGROUP        = 1;
        const ENVELOPE      = 2;
        const QUATERNIONS   = 4;
        const INVERT_VGROUP = 8;
    }
}

#[repr(u16)]
#[allow(dead_code)]
#[derive(Copy,Debug,Clone,PartialEq)]
pub enum ParentType{
	Type       = (1 << 4) - 1,
	Object     = 0,
	Skeleton   = 4,
	Vert1      = 5,
	Vert3      = 6,
	Bone       = 7,

	/* slow parenting - is not threadsafe and/or may give errors after jumping  */
	Slow       = 16,
}


#[repr(u16)]
#[derive(Copy,Debug,Clone,PartialEq)]
pub enum SubdivisionTy {
    CatmullClark,
    Simple,
}

fn modifiers_suffix(modifiers: &blender::List, has_skeleton: bool) -> String{
    modifiers.iter().fold("".to_owned(), |mut modifiers_suffix, modifier|{
        let modifier_type = modifier.structure().name();
        match modifier_type{
            "MirrorModifierData" => {
                let flag: u16 = *modifier.get("flag").unwrap();
                if flag & modifiers::MirrorFlags::AxisX as u16 != 0 {
                    modifiers_suffix += "_mx";
                }
                if flag & modifiers::MirrorFlags::AxisY as u16 != 0 {
                    modifiers_suffix += "_my";
                }
                if flag & modifiers::MirrorFlags::AxisZ as u16 != 0 {
                    modifiers_suffix += "_mz";
                }
            }
            "SubsurfModifierData" => {
                if !has_skeleton {
                    let flag: u16 = *modifier.get("flags").unwrap();
                    let subdivision_ty: SubdivisionTy = *modifier.get("subdivType").unwrap();
                    let subdivide_uvs = flag & modifiers::SubsurfModifierFlag::SubsurfUv as u16 != 0;
                    let subdivide_levels = *modifier.get::<u16>("levels").unwrap();
                    if subdivide_uvs {
                        modifiers_suffix += &format!("_ssuv{}_{:?}", subdivide_levels, subdivision_ty);
                    }else{
                        modifiers_suffix += &format!("_ss{}_{:?}", subdivide_levels, subdivision_ty);
                    }
                }
            }
            "ArrayModifierData" => {
                let offset: Vec3 = *modifier.get("offset").unwrap();
                let scale: Vec3 = *modifier.get("scale").unwrap();
                let length: f32 = *modifier.get("length").unwrap();
                let merge_dist: f32 = *modifier.get("merge_dist").unwrap();
                let fit_type: i32 = *modifier.get("fit_type").unwrap();
                let offset_type: i32 = *modifier.get("offset_type").unwrap();
                let flags: i32 = *modifier.get("flags").unwrap();
                let count: u32 = *modifier.get("count").unwrap();
                modifiers_suffix += &format!("_array{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}",
                    offset.x, offset.y, offset.z,
                    scale.x, scale.y, scale.z,
                    length,
                    merge_dist,
                    fit_type,
                    offset_type,
                    flags,
                    count);
            }
            _ => ()
        }
        modifiers_suffix
    })
}

fn apply_modifiers(obj: &blender::Object, mut mesh: Mesh, modifiers: &blender::List, vertex_groups: &[String], has_skeleton: bool) -> Mesh{
    for modifier in modifiers.iter(){
        let modifier_type = modifier.structure().name().to_string();

        if modifier_type == "MirrorModifierData"{
            // TODO: This uses the object matrix and vertex groups so
            // event with the same parameters we probably can't cache
            // the mesh like we are doing here
            let flag: u16 = *modifier.get("flag").unwrap();
            if flag & modifiers::MirrorFlags::AxisX as u16 != 0{
                modifiers::mirror_mesh(&modifier, obj, &mut mesh, vertex_groups, modifiers::MirrorAxis::X);
            }
            if flag & modifiers::MirrorFlags::AxisY as u16 != 0{
                modifiers::mirror_mesh(&modifier, obj, &mut mesh, vertex_groups, modifiers::MirrorAxis::Y);
            }
            if flag & modifiers::MirrorFlags::AxisZ as u16 != 0{
                modifiers::mirror_mesh(&modifier, obj, &mut mesh, vertex_groups, modifiers::MirrorAxis::Z);
            }
        }else if modifier_type == "SubsurfModifierData"{
            time!("Subdivide", {
                //TODO: this won't work with animation or in real time
                if !has_skeleton {
                    let flag: u16 = *modifier.get("flags").unwrap();
                    let subdivide_uvs = flag & modifiers::SubsurfModifierFlag::SubsurfUv as u16 != 0;
                    let subdivision_ty: SubdivisionTy = *modifier.get("subdivType").unwrap();
                    if subdivision_ty == SubdivisionTy::CatmullClark {
                        for _ in 0u16..*modifier.get("levels").unwrap(){
                            catmullclark::subdivide(&mut mesh, subdivide_uvs);
                        }
                    }else{
                        for _ in 0u16..*modifier.get("levels").unwrap(){
                            mesh.subdivide_simple(subdivide_uvs);
                        }
                    }
                    time!("recalculate normals", {
                        mesh.recalculate_normals();
                    });
                }
            });
        }else if modifier_type == "ArrayModifierData"{
            modifiers::array_mesh(&modifier, &mut mesh);
        }
    }

    mesh
}

impl Model{
    pub fn parse(
        obj: &blender::Object,
        library_id: LibraryId,
        blend_mesh: &blender::Object,
        visible: bool,
        scene_data: &mut SceneData,
        libraries: &HashMap<LibraryId, blender::File>) -> blender::Result<Model>
    {
        let ty = *obj.get("type").unwrap();
        let name = ObjectId::new(library_id.clone(), obj.name().unwrap());
        if blender::ObjectType::Mesh != ty{
            return Err(blender::Error(format!("Object {:?} is not a mesh", name)));
        }
        let rigid_body = obj.get_object("rigidbody_object").ok().map(|rigid|{
            RigidBody{
                ty: *rigid.get("type").unwrap(),
                shape: *rigid.get("shape").unwrap(),
                friction: *rigid.get("friction").unwrap(),
            }
        });
        let restrictflag: u8 = *obj.get("restrictflag").unwrap();
        let selectable = (restrictflag & loader::ObjectRestrict::Select as u8) == 0;

        // let mut skeleton_name = find_armature_parent(obj);
        // let has_armature_parent = skeleton_name.is_some();

        let vertex_groups: Vec<String> = obj.get_list("defbase").unwrap().iter()
            .map(|deformation| {
                let vertex_group = deformation.name().unwrap().to_string();
                vertex_group
            }).collect();

        let trafos = utils::transformations(&obj);

        let modifiers = obj.get_list("modifiers").unwrap();
        let skeleton_group = modifiers.iter()
            .find(|modifier| modifier.structure().name() == "ArmatureModifierData")
            .and_then(|modifier| {
                let deformflag = *modifier.get::<ArmatureDeformFlag>("deformflag").unwrap();
                modifier.get_object("object").ok()
                    .map(|armature|{
                        let lib_id = if let Some(lib_id) = library_id.linked_library_id(&armature) {
                            lib_id
                        }else{
                            library_id.clone()
                        };
                        let skeleton_name = armature.name().ok()
                            .map(|n| ObjectId::new(lib_id.clone(), n));
                        let default_group = modifier
                            .get_str("defgrp_name")
                            .ok()
                            .map(|n| n.to_owned());
                        // let deformflag: u16 = *modifier.get("deformflag").unwrap();
                        // let deformflag: EnumSet<ArmatureDeformFlag> = unsafe{EnumSet::from_bits(deformflag as usize)};
                        //armature_mat = utils::to_mat4(armature.get_slice::<f32>("obmat").unwrap());
                        //let deformflag = modifier.get::<u16>("deformflag").unwrap();
                        //use_envelop = deformflag & (1<<1) > 0;
                        //use_quat = deformflag & (1<<2) > 0;
                        //invert_vgroup = deformflag & (1<<4) > 0;
                        (skeleton_name, default_group, deformflag)
                    })
            });
        let (skeleton_name, default_group, deformflag) = if let Some(sg) = skeleton_group {
            sg
        }else{
            (None, None, ArmatureDeformFlag::empty())
        };


        let modifiers_suffix = modifiers_suffix(&modifiers, skeleton_name.is_some());
        let mesh_id = library_id.object_id( blend_mesh);
        let mesh_name = blend_mesh.name().unwrap().to_owned() + &modifiers_suffix;
        let blend_mesh_id = ObjectId::new(mesh_id.source_file.clone(), &mesh_name);
        if scene_data.meshes.get(&blend_mesh_id).is_none(){
            let mesh = scene_data.meshes[&mesh_id].clone();
            let mesh = apply_modifiers(
                obj,
                mesh,
                &modifiers,
                &vertex_groups,
                skeleton_name.is_some()
            );
            let trimesh = TriMesh::from(
                &blend_mesh,
                mesh_id.source_file.clone(),
                &mesh,
                libraries,
                scene_data,
            );
            scene_data.trimeshes.insert(blend_mesh_id.clone(), trimesh);
            scene_data.meshes.insert(blend_mesh_id.clone(), mesh);
        }
        let animated_vertices = if skeleton_name.is_some() || blend_mesh.get_object("key").is_ok(){
            scene_data.trimeshes[&blend_mesh_id].original_vertices().to_vec()
        }else{
            vec![]
        };

        let default_action_name = curves::default_action_name_for(
            obj,
            &library_id,
            libraries
        );

        let drivers = curves::parse_drivers_for(obj);

        Ok(Model{
            name,
            visible,
            node: trafos,
            animated_vertices,
            //animation_node: animation_node,
            skeleton_name,
            blend_mesh_id,
            vertex_groups,
            vertex_groups_index: vec![],
            default_group,
            rigid_body,
            selectable,
            default_action_name,
            custom_properties: utils::custom_properties(&blend_mesh),
            drivers,
            deformflag,
        })
    }


    // pub fn update_vertices(&mut self, skeleton: &bones::Skeleton){
    //     if !skeleton.changed() { return };
    //     if self.vertex_groups_index.is_empty() {
    //         let index = skeleton.animated_index();
    //         let vertex_groups_index = self.vertex_groups.iter().map(|name|{
    //             index.get(name).map(|i| *i)
    //         }).collect();
    //         self.vertex_groups_index = vertex_groups_index;
    //     }
    //
    //     let object_mat = self.node.global_transformation();
    //     let object_inv = self.node.inv_global_transformation();
    //     let _premat = object_mat.clone();
    //     let postmat = object_inv.fast_mul(&skeleton.global_transformation());
    //     let premat = postmat.fast_affine_inverse().unwrap();
    //     let postmat3 = Mat3::from_iterator(postmat.columns(0,3).rows(0,3).iter().map(|v| *v));
    //     let premat3 = Mat3::from_iterator(premat.columns(0,3).rows(0,3).iter().map(|v| *v));
    //     let num_dverts = self.blend_mesh.dvert.len();
    //     for (idx,vertex) in self.original_vertices.iter().enumerate(){
    //         if (vertex.original_idx as usize) < num_dverts {
    //             let mut vertex = vertex.clone();
    //             let deform_vert = &self.blend_mesh.dvert[vertex.original_idx as usize];
    //             let vpos = premat.fast_mul(&vec4(vertex.position.x, vertex.position.y, vertex.position.z, 1.0));
    //             let vnor = premat3.fast_mul(&vertex.normal);
    //             let mut position: Vec4 = zero();
    //             let mut normal: Vec3 = zero();
    //             let mut total_weight = 0.;
    //             let mut default_weight = 1.0;
    //             for weight in deform_vert.dw.iter(){
    //                 if self.vertex_groups.len()>weight.def_nr as usize{
    //                     let vertex_group = &self.vertex_groups[weight.def_nr as usize];
    //                     let vertex_group_id = self.vertex_groups_index[weight.def_nr as usize];
    //                     if let Some(mat) = vertex_group_id.and_then(|id| skeleton.deform_mat(id)) {
    //                         let cpos = mat.fast_mul(&vpos);
    //                         position += (cpos - vpos) * weight.weight;
    //                         total_weight += weight.weight;
    //                     }
    //                     if let Some(mat) = vertex_group_id.and_then(|id| skeleton.deform_normal_mat(id)){
    //                         let cnor = mat.fast_mul(&vnor);
    //                         normal = normal + (cnor - vnor) * weight.weight;
    //                     }
    //                     if vertex_group == &self.default_group{
    //                         default_weight = weight.weight;
    //                     }
    //                 }
    //             }
    //             if total_weight>0.{
    //                 let weight_factor = default_weight/total_weight;
    //                 position *= weight_factor;
    //                 position += vpos;
    //                 position = postmat.fast_mul(&position);
    //                 vertex.position = vec3(position.x, position.y, position.z);
    //
    //                 normal *= weight_factor;
    //                 normal += vnor;
    //                 normal = postmat3.fast_mul(&normal);
    //                 vertex.normal = normal;
    //             }
    //             self.animated_vertices[idx] = vertex;
    //         }
    //     }
    //
    //     self.position_buffer.update(self.animated.vertices());
    // }

    pub fn skeleton(&self) -> Option<&ObjectId> {
        self.skeleton_name.as_ref()
    }

    pub fn default_action(&self) -> Option<&ObjectId>{
        self.default_action_name.as_ref()
    }

    pub fn name(&self) -> &ObjectId{
        &self.name
    }

    // pub fn normals(&self, length: f32) -> Ref<gl::SimpleVao<Vertex3DColor>>{
    //     let normal_mat = self.inv_global_transformation().transpose();
    //     let normals_data: Vec<Vertex3DColor> = self.blend_mesh.mvert.iter().flat_map(|v|{
    //         let pos = self.global_transformation().fast_mul(&vec4(v.position.x, v.position.y, v.position.z, 1.0)).xyz();
    //         let norm = normal_mat.fast_mul(&vec4(v.normal.x, v.normal.y, v.normal.z, 1.0)).xyz();
    //         vec![
    //             vertex3dcolor(pos, &WHITE),
    //             vertex3dcolor(pos + norm * length, &WHITE),
    //         ]
    //     }).collect();
    //     let mut mesh = Mesh::from_vertices(normals_data);
    //     mesh.set_primitive_type(PrimitiveType::Lines);
    //     let initialized = self.normals.borrow().is_some();
    //     if !initialized{
    //         let vao = gl::SimpleVao::from_data_bindings(
    //                         &mesh,
    //                         &gl::default_attribute_bindings(),
    //                         gl::STATIC_DRAW)
    //             .unwrap();
    //
    //         *self.normals.borrow_mut() = Some(vao);
    //     }else{
    //         self.normals.borrow_mut().as_mut().unwrap().load_vertices(&mesh, gl::STATIC_DRAW);
    //     }
    //     Ref::map(self.normals.borrow(), |opt| opt.as_ref().unwrap())
    // }

    pub fn mesh(&self) -> &ObjectId {
        &self.blend_mesh_id
    }

    pub fn rigid_body(&self) -> Option<&RigidBody>{
        self.rigid_body.as_ref()
    }

    pub fn is_selectable(&self) -> bool{
        self.selectable
    }

    pub fn is_visible(&self) -> bool{
        self.visible
    }

    pub fn animated_vertices(&self) -> &[trimesh::Vertex]{
        &self.animated_vertices
    }

    pub fn transformations(&self) -> &utils::Transformations{
        &self.node
    }

    pub fn vertex_groups(&self) -> &[String]{
        &self.vertex_groups
    }

    pub fn default_group(&self) -> Option<&String>{
        self.default_group.as_ref()
    }

    pub fn custom_properties(&self) -> &[Property]{
        &self.custom_properties
    }

    pub fn drivers(&self) -> &[curves::FCurve] {
        &self.drivers
    }

    pub fn skeleton_deformflag(&self) -> ArmatureDeformFlag {
        self.deformflag
    }
}


pub struct TriModel<'a>{
    model: &'a Model,
    trimesh: &'a TriMesh,
    mesh: &'a Mesh,
}

impl<'a> TriModel<'a>{
    pub(crate) fn from(model: &'a Model, scene_data: &'a SceneData) -> TriModel<'a>{
        TriModel{
            trimesh: &scene_data.trimeshes[&model.mesh()],
            mesh: &scene_data.meshes[&model.mesh()],
            model,
        }
    }

    pub fn skeleton(&self) -> Option<&ObjectId>{
        self.model.skeleton()
    }

    pub fn default_action(&self) -> Option<&ObjectId>{
        self.model.default_action()
    }

    pub fn name(&self) -> &ObjectId{
        self.model.name()
    }

    pub fn mesh_name(&self) -> &ObjectId {
        self.model.mesh()
    }

    pub fn mesh(&self) -> &'a Mesh{
        self.mesh
    }

    pub fn rigid_body(&self) -> Option<&RigidBody>{
        self.model.rigid_body()
    }

    pub fn is_selectable(&self) -> bool{
        self.model.is_selectable()
    }

    pub fn is_visible(&self) -> bool{
        self.model.is_visible()
    }

    pub fn animated_vertices(&self) -> &[trimesh::Vertex]{
        self.model.animated_vertices()
    }

    pub fn animated_mesh(&self) -> (&[trimesh::Vertex], &[u32]){
        (self.model.animated_vertices(), self.trimesh.original_indices())
    }

    pub fn transformations(&self) -> &utils::Transformations{
        self.model.transformations()
    }

    pub fn vertex_groups(&self) -> &[String]{
        self.model.vertex_groups()
    }

    pub fn default_group(&self) -> Option<&String>{
        self.model.default_group()
    }

    pub fn custom_properties(&self) -> &[Property]{
        self.model.custom_properties()
    }


    pub fn original_vertices(&self) -> &[trimesh::Vertex]{
        self.trimesh.original_vertices()
    }

    pub fn original_indices(&self) -> &[u32]{
        self.trimesh.original_indices()
    }

    pub fn submeshes(&self) -> trimesh::SubMeshIter{
        self.trimesh.submeshes()
    }

    pub fn submeshes_vertices<'s>(&self) -> &[Vec<trimesh::Vertex>]{
        self.trimesh.submeshes_vertices()
    }

    pub fn submeshes_indices<'s>(&self) -> Option<&Vec<Vec<u32>>>{
        self.trimesh.submeshes_indices()
    }

    pub fn materials(&self) -> &[Option<ObjectId>]{
       self.trimesh.materials()
    }

    pub fn skeleton_deformflag(&self) -> ArmatureDeformFlag {
        self.model.deformflag
    }
}