Trying to get the Root from the Scene nodes and respective children

with some changes because above runs only first time.

var repeated = false;
function myPluginCommand(selection, documentRoot) {
	if (selection.hasArtboards) {
		(repeated) ? console.log("repeated") : 0;
		var rootnodes = selection.insertionParent.parent;
		var nodes = selection.items[0];
		console.log(rootnodes.children.length);
		rootnodes.children.forEach(element => {
			//This is ArtBoard
			let artboardname = element.name;
			selection.items = [element];
			let rect = new Rectangle();
			rect.height = 10;
			rect.width = 10;
			rect.fill = new Color("red");
			selection.insertionParent.addChild(rect);

			var children = element.children;
			children.forEach(element => {
				//This is an Artboard child
				console.log(element);
				selection.items = [element];
				let height = element.globalDrawBounds.height;
				let width = element.globalDrawBounds.width;

				let x = element.globalDrawBounds.x;
				let y = element.globalDrawBounds.y;
				console.log("height : " + height + "width : " + width);
				console.log("x : " + x + "y : " + y);
				let rect = new Rectangle();
				rect.height = height / 10;
				rect.width = width / 10;
				rect.fill = new Color("red");
				selection.insertionParent.addChild(rect);
				let xParent = rect.parent.globalDrawBounds.x;
				let yParent = rect.parent.globalDrawBounds.y;
				console.log("rect parent : " + rect.parent);
				rect.moveInParentCoordinates(x - xParent, y - yParent);
			})
		});
	} else if (!selection.hasArtboards) {
		repeated = true;
		console.log("Selected Otherthan Artboards");
		var element;
		var set = true;
		documentRoot.children.forEach(element => {
			if (set) {
				selection.items = [element];
				set = false;
			}
		});
		myPluginCommand(selection, documentRoot);
	}
}
1 Like