Hello,
I have some visual tests to do.
how can I get a packing of spheres inside a closed shape.
goal is to know how many spheres would fit for further build.
Ive looked around but didnt find a solution in the forums.

Example
10in x 10in x10in cube.
filled with 1in diameter spheres.
would it be 1260 spheres ?
Can grasshopper do this visually so i can change spheres diameters and box sizes ?
eventually to get sphere counts in different non box shapes or meshes

Do you even need to produce the spheres for this? If you know the volume of your shape - something which can be done easily enough with GH, and you know the system of units you’re working in, then you can calculate how many unit cells it contains. Multiply this by the number of spheres per unit cell and you derive the total number spheres.

CCP has four spheres per unit cell, You specified that the sphere has a diameter =1, so r = 1/2. If the radius is 1/2 inch, then, according to the formula above, unit cell edge = sqrt(8)*(1/2) = sqrt(2). The volume of the cell is then sqrt(8). The number of cells of volume sqrt(8) in a 10 x 10 x 10 cube is 1000/sqrt(8).

numbers of spheres in volume = number of unit cells * number of spheres/unit cell = (1000/sqrt(8))4
which is 1000 sqrt(2), about 1414.

OR, more simply, if you already know the packing efficiency for the arrangement, which in this case is 74.05%, then the volume occupied by the spheres is 1000 * .7405 = 740.5 . The volume of a single sphere is 4/3* pi*(1/2)^3 = 0.5236. so the number of spheres is 740.5 /.5236 = 1414 (rounded)

Do you want to know the exact number of spheres which can fit in some volume or just an approximation?

Do you want the absolute maximum number possible, or the maximum number while keeping them all in the cubic-close packed structure?

The densest possible arrangement within a given boundary isn’t necessarily just some subset of the densest arrangement in infinite space.

Consider the problem for just a single layer of spheres (ie circle packing within a boundary) - this is the densest possible packing for one ratio of circle to square size.

-which, while it is part of a hexagonal packing, doesn’t achieve the same density, because you have half circle sized gaps near the edges

You could certainly use a physics based method, and I can share some more examples with Kangaroo if that is the approach you want to take. Generally this can be a way to find a fairly dense sphere packing, but not usually a perfectly regular arrangement, and not necessarily the absolute optimum one (and I believe there isn’t any known (non brute-force) way of finding a guaranteed optimum for this sort of problem).

If your boundary volume is reasonably smooth, and large relative to the size of the spheres, the volume fraction based approximation of the number of spheres should be fairly close though.

Thanks for the good feedback.
I understand the math better now (thanks Ethan!)
I still want to make a 3D GH sphere pack calculator.
and doesnt have to be super exact, just a good approximation.
i found few topics but not a solution way yet:

For a rough approximation you can use the volume fraction approach Ethan gives above. The actual number of spheres would generally be less than predicted by this though, depending on their size relative to the volume, its shape, and how they are physically put in there

are these ball-bearings pouring into a hopper, oranges being carefully placed by hand…?

For a physics approach you can try this: FillVolumeWithSpheres.gh (22.4 KB)
It uses a script which will ask for a reference assembly location, and you need to set the location of your KangarooSolver dll, which is generally
C:\Program Files\Rhino 6\Plug-ins\Grasshopper\Components
There’s a slider for the number of spheres, which you can gradually increase, and their colour is an indication of the pressure, or how much they are overlapping. So if you slowly slide up the number until they are no longer all green, you can see how many fit.
You could also optionally add a Grab component to let you give them a stir if needed to see if they can rearrange to a denser arrangement.
It would also be possible with a bit more work to script the adding of spheres, so it would automatically add them until it was full.

If, as Daniel stated, my previous method overstates the number of spheres, here’s a solution which will slightly understate it. Since the CCP packing has four spheres per unit cell, if you divide the cell into eight subcells, each one will contain 1/2 sphere. The ideas is to create a lattice of one eighth cells (or their centers more accurately) and then check to see how many are contained in your shape. Take the total number contained, and multiply by 1/2. Before, I got 1414 and this method gives 1372.

I hope this isn’t just about winning a jelly bean counting contest.

Tnx,
I think i can figure out thr Kangeroo2 version (i already haveit but maybe the wrong version)
but what is the multiplication ? ? ? im missing and betweeen what is it connected ?