In this tutorial, we'll see how to fill a container with objects using Blender's Rigid Body simulation.
Video Transcript
Hello everyone!
In this tutorial, we'll see how to fill a container with objects using Blender's Rigid Body simulation.
I'm making this tutorial with Blender 3.3 and, to show you a practical example, I'll be using one of my assets called "Pharma capsule pills and bottle"; anyway, it's not necessary to have it: you can learn even without that particular model.
The important thing here is to understand the technique, which can be useful for placing many objects in a random but believable way, not only inside containers but also on surfaces. The principles are the same and you can apply them to pills, candies, gold coins, or anything else!
The 3D model that I'm using is actually made up of multiple objects that are related to each other.
We don't need the lid, so we can hide or remove it, as I'm doing.

We have three objects: the container, the label, and an object that represents all the pills, which have been fused into a single object. We can examine them more closely by temporarily hiding the container and the label.

To create the simulation, we need to isolate a pill and give it a Rigid Body physical component, and then duplicate the pill prototype and start the simulation of the fall.
With the Pills object selected, I switch to Edit mode, then select a pill by placing the mouse cursor over a vertex of a pill and pressing L (that is: "Linked"), and select all the elements connected to the one near the mouse cursor.

Then, I invert the selection with CTRL+I and delete all the other pills with X and Vertices.

Back to Object mode, I notice that the Origin of the "Pills" object is not positioned at the center of the only surviving pill. I fix it immediately with a right-click and "Set Origin" - "Origin to Geometry".

I also make sure that the scale transformations are applied. It's easy to check because the Scale XYZ values of the object in the Transform panel are all set to 1.
If they weren't, we would need to apply them with CTRL+A and "Apply Scale".
Now I make the container visible again because I want to check that the pill is positioned so that it can pass through the container's opening. I'll try to drop the pills inside it from above.
In fact, in this position, the pill cannot fall inside the container, so I manually move it so that, in the top view, it is inside the opening.

The dotted line that connects the pill to the container indicates that the pill is a child of the container. This could be a problem with the pill's copies and the physical simulation, so I unlink the two objects while keeping the position and other characteristics of the pill. To do this, I select the pill, press ALT+P, and choose "Clear and keep transformation" from the "Clear Parent" menu.

The pill is now a separate object.
Before creating the copies, I need to give the pill a Rigid Body simulation component so that it will already be ready in the copies I'll make later.

To add the Rigid Body, I simply click on "Rigid Body" in the Physics Properties panel, as I just showed.
The newly created Rigid Body will be of the Active type by default, which is fine because the object needs to react to gravity.
To observe the physics simulation at this point, we set the animation frame to 1 in the Timeline and press Play. The pill falls and passes through the container because it is not yet part of the physical simulation of the scene.
To make the container catch the pill, we need to give it a Rigid Body component like we did for the pill, but this time we need to immediately change its type to Passive because it shouldn't fall into the void but only interact with the other objects.
To start the physics simulation with the changes we just made, we need to go back to the first frame of the animation in the Timeline and click the Play button again.
We immediately notice that the pill falls and... "interacts" with the container, but in a way that we didn't expect: it gets stuck on the opening, suspended in mid-air, as if the opening was closed.

This is because the shape used by Blender to calculate the collisions of the container in the Rigid Body panel is the default one, which is "Convex Hull": a closed box used to simplify the calculations of the physics simulation engine.

To solve this problem, I change the value of the Shape parameter from "Convex Hull" to "Mesh": Blender will now approximate the shape of the collisions to the shape of the container.
As you can imagine, this mode is quite resource-intensive and should be used sparingly. In this case, the scene is not very complex, so it's okay, but in other situations, you may need to evaluate whether to use the other options available in the Shape menu of the Rigid Body.
Regarding the pill, you can choose, for example, the Capsule shape, which approximates it very well... in any case, here I'm leaving the default option, which is Convex Hull.
Let's go back to the first frame of the Timeline, click Play, and take a look at the result: this time the pill falls inside the container and bounces a couple of times on the bottom before stopping.

Note that the simulation begins to slow down when the pill touches the bottom of the container. This is because Blender is calculating the collisions.
When we add the other pills, the simulation will proceed much more slowly because, obviously, there will be more calculations to perform.
Let's go back to the first frame of the simulation.
We want to get a series of pills stacked on top of each other to drop inside the container, letting Blender handle the task of getting all the pills to a final position after calculating the collisions with the container walls and between pills.
Getting the various copies of the pill is actually quite simple: we just need to add an Array modifier to the pill, setting the relative offset to 0 for the X and Y axes and instead setting it to 2 for the relative Z axis.
I have no idea how many pills can fit inside the container (I didn't count them at the beginning of the video), so I'll try with 40 pills, entering this number in the Count parameter of the Array modifier.

We can't apply the simulation now because this is still a modifier that hasn't been applied yet, so all the pills are still instances, not individual objects.

So I apply the modifier by clicking on Apply in its panel.
However, the pills are still part of a single object: they are geometries that we need to make independent.
To do this, we simply need to go into Edit Mode, select all the geometries, press P, and choose "By loose parts" in the Separate menu that will appear on the screen.

This way, as the tooltip suggests, we will get one object for each "island", i.e. for each separate geometry.
Let's go back to Object mode and note that all the new pills are, in fact, separate objects, as we can see in the Outliner, too.
These objects all have the Rigid Body component set up as we did for the first pill, which served as a "prototype" for all the others...

...however, we can also see that the Origin of each copy is still where the Origin of the first pill was!
This problem can be easily solved: we select one of the copies (to make it the active object), then select all the other copies (I'm doing this with the B key and the mouse), right-click on the selection, and choose "Set Origin - Origin to Geometry" from the "Object Context" menu that appears on the screen.

Now all the pills are separate objects, with the Origins set correctly, and they all have the Rigid Body component correctly set up.
We can then go to the first frame of the Timeline and start the simulation by clicking the Play button.
In this case, the simulation will take several minutes.

Indeed, maybe 40 pills were too few for this container!
Anyway, we can take a look at the arrangement of the pills by interrupting the playback of the animation at various points.
Let's go to the last frame of the animation and temporarily hide the container and the label: the pills are in a plausible resting position (without empty spaces or intersections), so the frames of the animation are sufficient to achieve a good result.

The animation is interesting, but the tutorial is not yet complete: if we wanted to export the result of this physical simulation, with the pills arranged inside the container as they are at the last frame of the animation, we would need to somehow make the physical simulation permanent.
The operation of removing the Rigid Body component from the Physics tab of the pills should NOT be done, nor should the pills be fused together or with the container. These operations would cause us to lose the physics simulation and we would have to start over!
Instead, we should position ourselves on the frame that we want to make final (for example, the last one), then select one pill (so we have at least one active object in the selection), and then select all the pills. To make this selection, we can use the B key and the mouse.

With all the pills selected (one of which is active), we should search for "Apply Transformation" in the Blender search box (which I can invoke with the spacebar in my case) and choose the option under the Rigid Body section, as we want to apply the Rigid Body transformations.

We're not done yet: we need to open the search box again and start typing "Remove Rigid Body", as we now want to remove the Rigid Body component from all the selected pills.
We should select the Object - Rigid Body - Remove operation.
Now the simulation is permanent: moving between the various frames of the Timeline, we can see that the pills no longer move and no longer have the Rigid Body physical component.

Now, this object can be exported to be used in other scenarios.
The pills are individual meshes, each with the Origin at the center of its geometry, so it's up to you to decide whether to leave them as they are or group them, parent them, or even merge them together; choose according to your needs.
That's it for this tutorial! See you soon!