diff --git a/Sources/armory/logicnode/SpawnObjectByNameNode.hx b/Sources/armory/logicnode/SpawnObjectByNameNode.hx new file mode 100644 index 00000000..5ecb1bc7 --- /dev/null +++ b/Sources/armory/logicnode/SpawnObjectByNameNode.hx @@ -0,0 +1,72 @@ +package armory.logicnode; + +import iron.data.SceneFormat.TSceneFormat; +import iron.data.Data; +import iron.object.Object; +import iron.math.Mat4; +import armory.trait.physics.RigidBody; + +class SpawnObjectByNameNode extends LogicNode { + + var object: Object; + var matrices: Array = []; + + /** Scene from which to take the object **/ + public var property0: Null; + + public function new(tree: LogicTree) { + super(tree); + } + + override function run(from: Int) { + var objectName = inputs[1].get(); + if (objectName == null) return; + + #if arm_json + property0 += ".json"; + #elseif arm_compress + property0 += ".lz4"; + #end + + var m: Mat4 = inputs[2].get(); + matrices.push(m != null ? m.clone() : null); + var spawnChildren: Bool = inputs.length > 3 ? inputs[3].get() : true; // TODO + + Data.getSceneRaw(property0, (rawScene: TSceneFormat) -> { + + //Check if object with given name present in the specified scene + var objPresent: Bool = false; + + for (o in rawScene.objects) { + if (o.name == objectName) { + objPresent = true; + break; + } + } + if (! objPresent) return; + + //Spawn object if present + iron.Scene.active.spawnObject(objectName, null, function(o: Object) { + object = o; + var matrix = matrices.pop(); // Async spawn in a loop, order is non-stable + if (matrix != null) { + object.transform.setMatrix(matrix); + #if arm_physics + var rigidBody = object.getTrait(RigidBody); + if (rigidBody != null) { + object.transform.buildMatrix(); + rigidBody.syncTransform(); + } + #end + } + object.visible = true; + runOutput(0); + }, spawnChildren, rawScene); + + }); + } + + override function get(from: Int): Dynamic { + return object; + } +} diff --git a/blender/arm/logicnode/object/LN_spawn_object_by_name.py b/blender/arm/logicnode/object/LN_spawn_object_by_name.py new file mode 100644 index 00000000..92faa105 --- /dev/null +++ b/blender/arm/logicnode/object/LN_spawn_object_by_name.py @@ -0,0 +1,24 @@ +from arm.logicnode.arm_nodes import * + +class SpawnObjectByNameNode(ArmLogicTreeNode): + """Spawns an object bearing the given name, even if not present in the active scene""" + bl_idname = 'LNSpawnObjectByNameNode' + bl_label = 'Spawn Object By Name' + arm_version = 1 + + property0: PointerProperty( + type=bpy.types.Scene, name='Scene', + description='The scene from which to take the object') + + def init(self, context): + super(SpawnObjectByNameNode, self).init(context) + self.add_input('ArmNodeSocketAction', 'In') + self.add_input('NodeSocketString', 'Name') + self.add_input('NodeSocketShader', 'Transform') + self.add_input('NodeSocketBool', 'Children', default_value=True) + + self.add_output('ArmNodeSocketAction', 'Out') + self.add_output('ArmNodeSocketObject', 'Object') + + def draw_buttons(self, context, layout): + layout.prop_search(self, 'property0', bpy.data, "scenes")