Why rotating and translating a duplicated layers in a loop tend towards the center of the image?

Explorer ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

The code below should duplicate, rotate and translate layers at random positions on the screen each one in a loop with given iterations. Duplicating of layers is executed well, but rotation and translation is giving me very strange results, and that instead of rotating as well as translating randomly it tends to do that towards the center of the image:

doc = app.activeDocument;

function random(min, max) {
	const num = Math.floor(Math.random() * (max - min + 1)) + min;
	return num;
};

function duplicateCurrentLayer(iter){
	for(i=0;i<iter;i++){
		doc.activeLayer.duplicate();
		doc.activeLayer.translate(random(-256,256),random(-256,256));
		doc.activeLayer.rotate(random(20,50),AnchorPosition.MIDDLECENTER);
	}
}

doc.suspendHistory("duplicate","duplicateCurrentLayer(30)");

Please find the image belo as an example to see what I mean:

 

Untitled.jpg

 

The first two duplicates are fine translated and rotated, but the others tend towards the center of the image.

 

Thanks.

 

TOPICS
Actions and scripting, How to, Problem or error, Windows

Views

1.1K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Jun 15, 2021 Jun 15, 2021
You always move and rotate the same original layer. This spoils the image. Also, according to the theory of probability (I cannot prove it), it will often return to its original position. Units of measure are not specified for you. The behavior of the script also depends on this. Check out this code. doc = app.activeDocument; function random(min, max) { const num = Math.floor(Math.random() * (max - min + 1)) + min; return num; }; function duplicateCurrentLayer(iter){ var l...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

You always move and rotate the same original layer. This spoils the image.

Also, according to the theory of probability (I cannot prove it), it will often return to its original position.

 

Units of measure are not specified for you. The behavior of the script also depends on this.

 

Check out this code.

 

doc = app.activeDocument;

function random(min, max) {
    const num = Math.floor(Math.random() * (max - min + 1)) + min;
    return num;
};

function duplicateCurrentLayer(iter){
    var layer0 = doc.activeLayer;    
    for(i=0;i<iter;i++){
        doc.activeLayer = layer0;
        var layer1 = doc.activeLayer.duplicate();
        doc.activeLayer = layer1;
        doc.activeLayer.translate(random(-256,256),random(-256,256));
        doc.activeLayer.rotate(random(20,50),AnchorPosition.MIDDLECENTER);
    }
}

doc.suspendHistory("duplicate","duplicateCurrentLayer(30)");

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

I am not sure, but this looks like a bug. When the object exits out of the boundaries of the image, it returns it to the center and every object out of the boundaries of the image are also centered and rotated in a sequence.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

Yes indeed. If you duplicate a layer in PS2020 that is completely outside the canvas, then the new layer will be in the center of the document.

 

This is pure BUG. If you want, please report it on the photoshop feedback site.

 

It's not in CS6. 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

This means that CS6 is the latest version. Come on... 😕

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

This works very well r-bin and thank you again.

 

I have a question if you have time to explain it about the passing the value of one variable into another that I have met many similarities like in your example above:

var layer0 = doc.activeLayer;

then:

doc.activeLayer = layer0;

Does it mean that both are exchanging the same information and the second one holds the reference in a certain memory location until that reference is changed similar to the pointers in C++,  i.e. passing by reference?

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

I'm trying to understand what you are asking. I am not a programmer by training, I am an amateur. Javascript is not the same as C ++. There are no pointers here. These are objects with their properties and methods. For example, when you assign layer0.opacity = 50, you not only change the value of the opacity variable in the object, but also a number of actions occur, as a result of which some functions are performed that lead to a real change in the opacity of the layer.

 

doc.activeLayer = layer0;

is executed at the beginning of each iteration of the loop. This must be done, to restore the activity of the original layer. What's incomprehensible?

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

LATEST

Thank you r-bin, this is very helpful indeed.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines