Merge pull request #2177 from QuantumCoderQC/get-object-node-fix

Get object by name node fix
This commit is contained in:
Lubos Lenco 2021-05-02 11:16:12 +02:00 committed by GitHub
commit 4db37ea1c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 40 deletions

View file

@ -14,36 +14,7 @@ class GetObjectNode extends LogicNode {
override function get(from: Int): Dynamic {
var objectName: String = inputs[0].get();
if (property0 == null || property0 == iron.Scene.active.raw.name) {
return iron.Scene.active.getChild(objectName);
}
#if arm_json
property0 += ".json";
#elseif arm_compress
property0 += ".lz4";
#end
var outObj: Null<Object> = null;
// Create the object in the active scene if it is from an inactive scene
iron.data.Data.getSceneRaw(property0, (rawScene: TSceneFormat) -> {
var objData: Null<TObj> = null;
for (o in rawScene.objects) {
if (o.name == objectName) {
objData = o;
break;
}
}
if (objData == null) return;
iron.Scene.active.createObject(objData, rawScene, null, null, (newObj: Object) -> {
outObj = newObj;
});
});
return outObj;
return iron.Scene.active.getChild(objectName);
}
}

View file

@ -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<Mat4> = [];
/** Scene from which to take the object **/
public var property0: Null<String>;
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;
}
}

View file

@ -4,20 +4,14 @@ from arm.logicnode.arm_nodes import *
class GetObjectNode(ArmLogicTreeNode):
"""Searches for a object that uses the given name and returns it."""
"""Searches for a object that uses the given name in the current active scene and returns it."""
bl_idname = 'LNGetObjectNode'
bl_label = 'Get 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(GetObjectNode, self).init(context)
self.add_input('NodeSocketString', 'Name')
self.add_output('ArmNodeSocketObject', 'Object')
def draw_buttons(self, context, layout):
layout.prop_search(self, 'property0', bpy.data, "scenes")

View file

@ -1,7 +1,8 @@
from arm.logicnode.arm_nodes import *
class SpawnObjectNode(ArmLogicTreeNode):
"""Spawns the given object. The spawned object has the same name of its instance, but they are threated as different objects."""
"""Spawns the given object if present in the current active scene. The spawned object has the same name of its instance, but they are treated as different objects."""
bl_idname = 'LNSpawnObjectNode'
bl_label = 'Spawn Object'
arm_version = 1

View file

@ -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")