+void Scene::add_object(Object *obj)
+{
+ objects.push_back(obj);
+}
+
+bool Scene::remove_object(Object *obj)
+{
+ std::vector<Object*>::iterator it = std::find(objects.begin(), objects.end(), obj);
+ if(it != objects.end()) {
+ objects.erase(it);
+ return true;
+ }
+ return false;
+}
+
+bool Scene::have_object(Object *obj) const
+{
+ return std::find(objects.begin(), objects.end(), obj) != objects.end();
+}
+
+void Scene::add_mesh(Mesh *m)
+{
+ meshes.push_back(m);
+}
+
+bool Scene::remove_mesh(Mesh *m)
+{
+ std::vector<Mesh*>::iterator it = std::find(meshes.begin(), meshes.end(), m);
+ if(it != meshes.end()) {
+ meshes.erase(it);
+ return true;
+ }
+ return false;
+}
+
+bool Scene::have_mesh(Mesh *m) const
+{
+ return std::find(meshes.begin(), meshes.end(), m) != meshes.end();
+}
+
+void Scene::add_node(SceneNode *n)
+{
+ // we always want to have a dedicated root node
+ if(!nodes) {
+ nodes = new SceneNode;
+ nodes->scene = this;
+ nodes->set_name("root");
+ }
+
+ nodes->add_child(n);
+}
+
+bool Scene::remove_node(SceneNode *n)
+{
+ SceneNode *par;
+ if(!n || !(par = n->get_parent())) {
+ return false;
+ }
+
+ int nsub = n->get_num_children();
+ for(int i=0; i<nsub; i++) {
+ SceneNode *c = n->get_child(i);
+ n->remove_child(c);
+ par->add_child(c);
+ }
+
+ return par->remove_child(n);
+}
+
+bool Scene::have_node(SceneNode *n) const
+{
+ return n->scene == this;
+}
+
+static void find_nodes_rec(std::list<SceneNode*> *res, SceneNode *tree, const std::regex &re)
+{
+ if(std::regex_match(tree->get_name(), re)) {
+ res->push_back(tree);
+ }
+
+ int num = tree->get_num_children();
+ for(int i=0; i<num; i++) {
+ find_nodes_rec(res, tree->get_child(i), re);
+ }
+}
+
+Scene *Scene::extract_nodes(const char *qstr)
+{
+ std::regex re{qstr};
+
+ std::list<SceneNode*> nodelist;
+ find_nodes_rec(&nodelist, nodes, re);
+ if(nodelist.empty()) {
+ return 0;
+ }
+
+ Scene *res = new Scene(texset);
+
+ for(SceneNode *n : nodelist) {
+
+ int nobj = n->get_num_objects();
+ for(int i=0; i<nobj; i++) {
+ Object *obj = n->get_object(i);
+ if(obj->get_type() == OBJ_MESH) {
+ // XXX this assumes that meshes aren't shared between objects.
+ // maybe we'll have to refcount them at some point, and copy if nref>1
+ ObjMesh *om = (ObjMesh*)obj;
+ remove_mesh(om->mesh);
+ res->add_mesh(om->mesh);
+ }
+
+ remove_object(obj);
+ res->add_object(obj);
+ }
+
+ remove_node(n);
+ res->add_node(n);
+ }
+ return res;
+}
+
+void Scene::apply_xform()
+{
+ nodes->apply_xform();
+}
+