@@ -449,9 +449,8 @@ static bool IsPluginActive(
449
449
450
450
mjCModel& mjCModel::operator +=(const mjCModel& other) {
451
451
// create global lists
452
- mjCBody *world = bodies_[0 ];
453
452
ResetTreeLists ();
454
- MakeLists (world );
453
+ MakeTreeLists ( );
455
454
ProcessLists (/* checkrepeat=*/ false );
456
455
457
456
// copy all elements not in the tree
@@ -500,11 +499,6 @@ mjCModel& mjCModel::operator+=(const mjCModel& other) {
500
499
nq = nv = na = nu = nmocap = 0 ;
501
500
}
502
501
503
- // restore to the original state
504
- if (!compiled) {
505
- ResetTreeLists ();
506
- }
507
-
508
502
PointToLocal ();
509
503
return *this ;
510
504
}
@@ -609,13 +603,11 @@ mjCModel& mjCModel::operator-=(const mjCBody& subtree) {
609
603
610
604
// create global lists in the old model if not compiled
611
605
if (!oldmodel.IsCompiled ()) {
612
- oldmodel.MakeLists (oldmodel.bodies_ [0 ]);
613
606
oldmodel.ProcessLists (/* checkrepeat=*/ false );
614
607
}
615
608
616
609
// create global lists in this model if not compiled
617
610
if (!IsCompiled ()) {
618
- MakeLists (bodies_[0 ]);
619
611
ProcessLists (/* checkrepeat=*/ false );
620
612
}
621
613
@@ -629,7 +621,7 @@ mjCModel& mjCModel::operator-=(const mjCBody& subtree) {
629
621
630
622
// update global lists
631
623
ResetTreeLists ();
632
- MakeLists (world );
624
+ MakeTreeLists ( );
633
625
ProcessLists (/* checkrepeat=*/ false );
634
626
635
627
// check if we have to remove anything else
@@ -641,11 +633,6 @@ mjCModel& mjCModel::operator-=(const mjCBody& subtree) {
641
633
RemoveFromList (sensors_, oldmodel);
642
634
RemovePlugins ();
643
635
644
- // restore to the original state
645
- if (!compiled) {
646
- ResetTreeLists ();
647
- }
648
-
649
636
return *this ;
650
637
}
651
638
@@ -742,18 +729,16 @@ void deletefromlist(std::vector<T*>* list, mjsElement* element) {
742
729
743
730
// discard all invalid elements from all lists
744
731
void mjCModel::DeleteElement (mjsElement* el) {
745
- mjCBody *world = nullptr ;
746
- if (compiled) {
747
- world = bodies_[0 ];
748
- ResetTreeLists ();
749
- }
732
+ ResetTreeLists ();
750
733
751
734
switch (el->elemtype ) {
752
735
case mjOBJ_BODY:
736
+ MakeTreeLists (); // rebuild lists that were reset at the beginning of the function
753
737
throw mjCError (nullptr , " bodies cannot be deleted, use detach instead" );
754
738
break ;
755
739
756
740
case mjOBJ_DEFAULT:
741
+ MakeTreeLists (); // rebuild lists that were reset at the beginning of the function
757
742
throw mjCError (nullptr , " defaults cannot be deleted, use detach instead" );
758
743
break ;
759
744
@@ -818,11 +803,9 @@ void mjCModel::DeleteElement(mjsElement* el) {
818
803
break ;
819
804
}
820
805
821
- if (compiled) {
822
- ResetTreeLists (); // in case of a nested delete
823
- MakeLists (world);
824
- ProcessLists (/* checkrepeat=*/ false );
825
- }
806
+ ResetTreeLists (); // in case of a nested delete
807
+ MakeTreeLists ();
808
+ ProcessLists (/* checkrepeat=*/ false );
826
809
}
827
810
828
811
@@ -1020,15 +1003,6 @@ void mjCModel::Clear() {
1020
1003
nconmax = -1 ;
1021
1004
nmocap = 0 ;
1022
1005
1023
- // pointer lists created by Compile
1024
- bodies_.clear ();
1025
- joints_.clear ();
1026
- geoms_.clear ();
1027
- sites_.clear ();
1028
- cameras_.clear ();
1029
- lights_.clear ();
1030
- frames_.clear ();
1031
-
1032
1006
// internal variables
1033
1007
hasImplicitPluginElem = false ;
1034
1008
compiled = false ;
@@ -1486,7 +1460,11 @@ mjSpec* mjCModel::GetSourceSpec() const {
1486
1460
// ------------------------------- COMPILER PHASES --------------------------------------------------
1487
1461
1488
1462
// make lists of objects in tree: bodies, geoms, joints, sites, cameras, lights
1489
- void mjCModel::MakeLists (mjCBody* body) {
1463
+ void mjCModel::MakeTreeLists (mjCBody* body) {
1464
+ if (body == nullptr ) {
1465
+ body = bodies_[0 ];
1466
+ }
1467
+
1490
1468
// add this body if not world
1491
1469
if (body != bodies_[0 ]) {
1492
1470
bodies_.push_back (body);
@@ -1501,7 +1479,7 @@ void mjCModel::MakeLists(mjCBody* body) {
1501
1479
for (mjCFrame *frame : body->frames ) frames_.push_back (frame);
1502
1480
1503
1481
// recursive call to all child bodies
1504
- for (mjCBody* body : body->bodies ) MakeLists (body);
1482
+ for (mjCBody* body : body->bodies ) MakeTreeLists (body);
1505
1483
}
1506
1484
1507
1485
@@ -3657,21 +3635,12 @@ template void mjCModel::RestoreState<mjtNum>(
3657
3635
3658
3636
// resolve keyframe references
3659
3637
void mjCModel::StoreKeyframes (mjCModel* dest) {
3660
- bool resetlists = false ;
3661
-
3662
3638
if (this != dest && !key_pending_.empty ()) {
3663
3639
mju_warning (
3664
3640
" Child model has pending keyframes. They will not be namespaced correctly. "
3665
3641
" To prevent this, compile the child model before attaching it again." );
3666
3642
}
3667
3643
3668
- // create tree lists if they are empty, occurs if an uncompiled model is attached
3669
- if (bodies_.size () == 1 && geoms_.empty () && sites_.empty () && joints_.empty () &&
3670
- cameras_.empty () && lights_.empty () && frames_.empty ()) {
3671
- MakeLists (bodies_[0 ]);
3672
- resetlists = true ;
3673
- }
3674
-
3675
3644
// do not change compilation quantities in case the user wants to recompile preserving the state
3676
3645
if (!compiled) {
3677
3646
SaveDofOffsets (/* computesize=*/ true );
@@ -3719,10 +3688,6 @@ void mjCModel::StoreKeyframes(mjCModel* dest) {
3719
3688
key->spec_mpos_ .data (), key->spec_mquat_ .data ());
3720
3689
}
3721
3690
3722
- if (resetlists) {
3723
- ResetTreeLists ();
3724
- }
3725
-
3726
3691
if (!compiled) {
3727
3692
nq = nv = na = nu = nmocap = 0 ;
3728
3693
}
@@ -4054,11 +4019,7 @@ static void warninghandler(const char* msg) {
4054
4019
// compiler
4055
4020
mjModel* mjCModel::Compile (const mjVFS* vfs, mjModel** m) {
4056
4021
if (compiled) {
4057
- // clear kinematic tree
4058
- mjCBody* world = bodies_[0 ];
4059
- ResetTreeLists ();
4060
4022
Clear ();
4061
- bodies_.push_back (world);
4062
4023
}
4063
4024
4064
4025
CopyFromSpec ();
@@ -4105,9 +4066,7 @@ mjModel* mjCModel::Compile(const mjVFS* vfs, mjModel** m) {
4105
4066
// deallocate everything allocated in Compile
4106
4067
mj_deleteModel (model);
4107
4068
mj_deleteData (data);
4108
- mjCBody* world = bodies_[0 ];
4109
4069
Clear ();
4110
- bodies_.push_back (world);
4111
4070
4112
4071
// save error info
4113
4072
errInfo = err;
@@ -4354,9 +4313,6 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) {
4354
4313
AddKey ();
4355
4314
}
4356
4315
4357
- // make lists of objects created in kinematic tree
4358
- MakeLists (bodies_[0 ]);
4359
-
4360
4316
// clear subtreedofs
4361
4317
for (int i=0 ; i < bodies_.size (); i++) {
4362
4318
bodies_[i]->subtreedofs = 0 ;
0 commit comments