@@ -430,34 +430,27 @@ Tree XMLParser::instantiateTree(const Blackboard::Ptr& root_blackboard,
430430 Tree output_tree;
431431 std::string main_tree_ID = main_tree_to_execute;
432432
433+ // use the main_tree_to_execute argument if it was provided by the user
434+ // or the one in the FIRST document opened
433435 if ( main_tree_ID.empty () )
434436 {
435- for ( const auto & doc: _p->opened_documents )
437+ XMLElement* first_xml_root = _p->opened_documents .front ()->RootElement ();
438+
439+ if (auto main_tree_attribute = first_xml_root->Attribute (" main_tree_to_execute" ))
436440 {
437- XMLElement* xml_root = doc->RootElement ();
438- if (xml_root->Attribute (" main_tree_to_execute" ))
439- {
440- if (!main_tree_ID.empty ())
441- {
442- throw RuntimeError (" The attribute [main_tree_to_execute] has been "
443- " found multiple times. You must specify explicitly the name "
444- " of the <BehaviorTree> to instantiate." );
445- }
446- main_tree_ID = xml_root->Attribute (" main_tree_to_execute" );
447- }
441+ main_tree_ID = main_tree_attribute;
442+ }
443+ else if (_p->tree_roots .size () == 1 )
444+ {
445+ // special case: there is only one registered BT.
446+ main_tree_ID = _p->tree_roots .begin ()->first ;
447+ }
448+ else
449+ {
450+ throw RuntimeError (" [main_tree_to_execute] was not specified correctly" );
448451 }
449452 }
450453
451- // special case: no name, but there is only one registered BT.
452- if ( main_tree_ID.empty () && _p->tree_roots .size () == 1 )
453- {
454- main_tree_ID = _p->tree_roots .begin ()->first ;
455- }
456-
457- if ( main_tree_ID.empty () )
458- {
459- throw RuntimeError (" [main_tree_to_execute] was not specified correctly" );
460- }
461454 // --------------------------------------
462455 if ( !root_blackboard )
463456 {
0 commit comments