Skip to main content
m1b
Community Expert
Community Expert
October 19, 2024
Answered

Script for Bin Packing (automatically fitting items onto artboards)

  • October 19, 2024
  • 2 replies
  • 1989 views

Hi everyone, I'd like to share a script.

 

Some time ago I wrote a script for bin-packing in Illustrator (posted here on the forum) and recently another user asked me if I could make a version for Indesign, which I have now done and I've published to github (the repo has scripts for both Indesign and Illustrator).

 

To read more about the scripts, including instructions, you can visit the github repo or to jump right in you can directly download the scripts.

 

What is bin packing?

Bin packing is arranging things so that they fit into "bins". In Illustrator, the bins are artboards, and in Indesign the bins are pages.

 

This is the sort of thing you can do in Illustrator (packing 35 items into 2 artboards):

 

And here's an Indesign example, packing 146 items onto 4 pages:

(This animation is using the option "Divide Bins at Guides", which keeps the items clear of the guides by a set margin.)

 

I hope someone will find the scripts useful. Feel free to leave feedback here or via the github repo.

- Mark

Correct answer Sergey Osokin

Hi @Sergey Osokin, yes, and libaries like svgnest that are amazing, but too complex for me to port to ExtendScript.

 

But for now, I've improved my script's capabilities by (optionally) first rotating each item so that it fits within the smallest rectangle possible. This can make a big difference when packing arbitrary shapes.

 

I've updated the code on github.

- Mark

 

P.S. Here's an example of the new functionality:


I have a problem with the display of the degree sign in the UI. I changed it to Unicode "Allow 90\u00b0 rotation".

New test:

 

2 replies

jduncan
Community Expert
Community Expert
October 21, 2024

This is nice! Thanks for sharing @m1b!

Sergey Osokin
Inspiring
October 20, 2024

Thanks Mark for sharing this. I think I have come across these JS libraries. The result is acceptable, although even on rectangles sometimes the result can be corrected manually if the user can see that the empty areas can be filled in.

 

m1b
Community Expert
m1bCommunity ExpertAuthor
Community Expert
October 20, 2024

Hi @Sergey Osokin thanks for checking it out. Yes it is a basic algorithm that only works with rectangles—I did look at the more sophisticated algorithms but they were a major time investment to understand!

 

When I look at your "test 1.jpg" it gives me an idea to add an improvement—watch this space!

 

And your "test.jpg" shows a strange bug which I haven't noticed. I will look into that. Thanks again!

- Mark

Sergey Osokin
Inspiring
October 20, 2024

It seems to me that ExtendScript would be too slow if the goal is to maximize compact packaging. I know that printing companies buy specialized software for this, which is not cheap.