Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ExtendScript: duplicate() and general object management within a shape layer

Community Beginner ,
May 12, 2025 May 12, 2025

I hope I'm wrong, but after 5h of investigation, C-GPT and me gave up on this.

 

As far as it looks, it is not possible to organize shape layer content via script? I've tried duplicate() and another recreational approach (duplicate would be best as my content has expressions) but it seems impossible. for-loops keep breaking after the first successful duplication, throwing an alert that the object from the duplicate line was invalid.

 

GPT says that the "invalid object" is a common bug when operating inside shape layer content and that duplicating, removing or sorting of shape content is just not possible.

 

I do hope that there's a way though, as I've always preferred a single-layer workflow for shapes whenever possible so I can quickly import them as animation presets + they don't clutter up comps. This works fine in most cases, but if I want to dynamically generate content - tables, diagrams etc. - into these templates, with various amounts of the same thing, I need ExtendScript. And I would really need to rework a lot of templates with complex expression logic to change that workflow.

 

 
 

 

 

TOPICS
Error or problem , Expressions , Scripting
203
Translate
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

Community Expert , May 12, 2025 May 12, 2025

It's hard to say exactly what your issue is without seeing what you've tried so far and any error messages generated, but sometimes when you duplicate something, it will invalidate a reference you have stored in a variable. So, for example, if you create a simple rectangle  shape layer and want to make 5 copies of the shape (just the rectangle shape, not the layer), something like this will fail:

var myLayer = app.project.activeItem.layer("Shape Layer 1");
var myContents = myLayer.property("Cont
...
Translate
Community Beginner ,
May 12, 2025 May 12, 2025

Other advantages of single-layer workflow (beside preset management and clean comps): 

- relative internal coordinates: No "fromWorld / toWorld" or parenting required to make something usable, x = 50 is just simply 50px away from [0, 0] from where I usually want to start a bunch of expressions.

- simple masks: masking only one main object instead of applying masks, mask effects or precomps to 50 individual copies of the same thing.

 

The more I think about it, the less I could live without it lol...

Translate
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
Community Expert ,
May 12, 2025 May 12, 2025

It's hard to say exactly what your issue is without seeing what you've tried so far and any error messages generated, but sometimes when you duplicate something, it will invalidate a reference you have stored in a variable. So, for example, if you create a simple rectangle  shape layer and want to make 5 copies of the shape (just the rectangle shape, not the layer), something like this will fail:

var myLayer = app.project.activeItem.layer("Shape Layer 1");
var myContents = myLayer.property("Contents");
var myRect = myContents.property("Rectangle 1");
for (var i = 1; i <= 5; i++){
	myRect.duplicate();
}

but re-establishing the reference to the rectangle each time will work:

var myLayer = app.project.activeItem.layer("Shape Layer 1");
var myContents = myLayer.property("Contents");
var myRect;
for (var i = 1; i <= 5; i++){
	myRect = myContents.property("Rectangle 1");
	myRect.duplicate();
}

I hope that helps.

Translate
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
Community Beginner ,
May 12, 2025 May 12, 2025
LATEST

Damn, Dan himself saving my day (it was pretty bad until here :D) and entire workflow - thanks a lot!


One of the errors was that the "parent property is not an INDEXED_GROUP" (GPT has been hinting at indexed groups too and got lost trying to debug every single property type in the tree). So this seems to work only if the duplication takes place in the root of the Contents? Sad, but possible to work around. I dissolved my manual content group, in this case "Rows" of a table, and it works just fine!

 

Also, I haven't worked with property() before, only content(), and am going to look into that more.

Translate
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