eugene.litvinov Posted November 23, 2010 Posted November 23, 2010 It could be great to see some topics in Unigine help about working with nodes: adding nodes directly from script, handle nodes from world file, etc. In phythics examples I get on this: Object mesh = add_editor(class_append(spheres[hash].clone())); Body body = class_remove((density > 0.0f) ? new BodyRigid(mesh) : new BodyDummy(mesh)); ShapeSphere shape = class_remove(new ShapeSphere(body,radius)); It's quite not understand, what doing functions add_editor/class_append/class_remove? For example, I have some object in world, I get it through int nodeIndex = engine.editor.findNode(name); Node node = engine.editor.getNode(nodeIndex); ObjectMeshDynamic meshDie = class_cast(node.getTypeName(), node); So I use this through script - check some logic for it, etc. Need I remove this object from editor or not? If I creating nodes like this, and not use any logic for it: ObjectMesh zeroPoint = new ObjectMesh("source/models/cube.mesh"); zeroPoint.setMaterial("mesh_base", "*"); zeroPoint.setProperty("surface_base", "*"); zeroPoint.setPosition(vec3(0.0f, 0.0f, 0.25f)); then I need to use function below? Node add_editor(Node node) { engine.editor.addNode(node); return node_remove(node); } ?
ulf.schroeter Posted November 23, 2010 Posted November 23, 2010 It's all about object ownership and memory management. See documentation acticle "Programming\Memory Management" ObjectMesh zeroPoint = new ObjectMesh("source/models/cube.mesh"); zeroPoint.setMaterial("mesh_base", "*"); zeroPoint.setProperty("surface_base", "*"); zeroPoint.setPosition(vec3(0.0f, 0.0f, 0.25f)); then I need to use function below? Node add_editor(Node node) { engine.editor.addNode(node); return node_remove(node); } ? Yes, because you created ObjectMesh within world script (world is owner) and then "transfer" ownership to editor (engine.editor.addNode()) and finally have to tell world about this ownership transfer via node_remove, so world will not try to delete ObjectMesh instance on garbage collection (editor will take care of this).
eugene.litvinov Posted November 24, 2010 Author Posted November 24, 2010 Yes, I read this article... My question is "how I distribute nodes between script and editor?" In what case I add nodes in editor? I have some task: 1. I load some hierarchical world - majority of objects is static. 2. Then I find some nodes that have some interaction and create pointers to it... but I don't remove it from editor, but I use node_cast to use typed object in my script. Or in that case I need to release this object from editor, cause I handle it via script? Additional question: 1. If I create object within script, this object instantly added to "display list"? If I wont to create object, but I don't want to add it in my scene, I need create this object and than also remove from script?
manguste Posted November 24, 2010 Posted November 24, 2010 Or in that case I need to release this object from editor, cause I handle it via script? When you handle an object, it absolutely does not matter whom it belongs to. The ownership matters when deleting it. Meaning, no, you don't have to pass the ownership of the editor object (loaded from the world file) to handle it from the script. Objects created dynamically in the script are not displayed in the Nodes hierarchy - so it's not possible to tweak them in the editor. In this case engine.editor.addNode(node_remove(my_node)); will help. (But remember, after that the script cannot delete it.) And vice versa, if you want to delete an editor object in the world script, you'll have to use node_append() and engine.editor.releaseNode() beforehand. If I wont to create object, but I don't want to add it in my scene, I need create this object and than also remove from script? It's not clear what do you mean by that. But sure, it's possible can create an object via script and then remove it via script as well.
eugene.litvinov Posted November 24, 2010 Author Posted November 24, 2010 I mean, if I only create object via script, this object instantly add to world: ObjectMesh zeroPoint = new ObjectMesh("source/models/cube.mesh"); zeroPoint.setMaterial("mesh_base", "*"); zeroPoint.setProperty("surface_base", "*"); zeroPoint.setPosition(vec3(0.0f, 0.0f, 0.25f)); So I don't need to use, for example, engine.editor.addNode(). I mean, that if I create object it always add to display list (visualization conveyor). I don't need add an created object to some scene node, or scene root manually. So this functional need only for managing created objects (for garbage collector)? It's not use for adding object to different trees of visualization? I thought that when I add node to editor I do object visible (add object to render) :). Another question about objects transformations in node's tree: I have some object hierarchy, for example: Room include Table, Table include Cup. And I have PlayerActor. If I want "take" Cup from Table to PlayerActor, I do next: player.addChild(cupObject); In this case I have problems with transforms (Cup places to different position). For move objects via different nodes I must recalculate world/local transformation by myself, or Unigine have functions like localToGlobal/globalToLocal for calculate right local/world transformations? Or another way? Thanks.
eugene.litvinov Posted November 25, 2010 Author Posted November 25, 2010 The answer is addWorldChild/removeWorldChild
Recommended Posts