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

Script for Bin Packing (automatically fitting items onto pages)

  • October 19, 2024
  • 3 replies
  • 1269 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

This topic has been closed for replies.

3 replies

m1b
Community Expert
m1bCommunity ExpertAuthor
Community Expert
October 23, 2024

Update 2024-10-23: I have added an option "Allow any rotation" which will rotate each item as needed to best fit inside a rectangle. This can improve packing significantly, depending on the shapes.

Here's an example—the bottom items are packed into the artboards at top. Without the new option, they would never have packed as well. Also note that the "J" and "q" are text items, not outlined.

- Mark

 

Marc Autret
Legend
October 21, 2024

Great work Mark!

 

Many nice settings (‘Divide Bins at Guides’, ‘Maximize’ options) will make this tool essential.

 

FYI, a much more rudimentary approach without UI stuff and extra options:

https://github.com/indiscripts/IdExtenso/tree/master/tests#rectpacktestjsx

 

I only mention it for the purpose of ​​comparing the two underlying algorithms. IdExtenso's RectPack module is based on github.com/mapbox/potpack, while yours relies on Jake Gordon's Binary Tree Bin Packing Algorithm which is probably the root reference of all existing solutions. The different implementations might be interesting to analyze in terms of performance.

 

Best,

Marc

m1b
Community Expert
m1bCommunity ExpertAuthor
Community Expert
October 23, 2024

Hi @Marc Autret, thank you for your encouragement! I will be interested to compare the different approaches sometime—it fascinates me how such things work. As I'm sure you can tell I don't have a computer science background (or even programming background for that matter) and I struggle sometimes to understand, but I will press forward!

- Mark

brian_p_dts
Community Expert
Community Expert
October 19, 2024

Amazing Mark!

m1b
Community Expert
m1bCommunity ExpertAuthor
Community Expert
October 19, 2024

Thanks Brian! As usual I am standing on the shoulders of giants!

brian_p_dts
Community Expert
Community Expert
October 20, 2024

Aren't we all?