We propose a theoretical framework to calculate capillary stresses in complex mesoporous materials, such as moist sand, nanoporous hydrates, and drying colloidal films. Molecular simulations are mapped onto a phase-field model of the liquid-vapor mixture, whose inhomogeneous stress tensor is integrated over Voronoi polyhedra in order to calculate equal and opposite forces between each pair of neighboring grains. The method is illustrated by simulations of moisture-induced forces in small clusters and heterogeneous porous packings of spherical grains using lattice-gas Density Functional Theory. For a nano-granular model of cement hydrates, this approach reproduces the hysteretic water sorption/desorption isotherms and predicts drying shrinkage strain isotherm in good agreement with experiments. We show that capillary stress is an effective mechanism for internal stress relaxation in colloidal heterogeneous porous packings, which contributes to the extraordinary durability of cement paste.Capillary condensation is a ubiquitous process of vapor-liquid phase transition in porous media, such as sand piles, plaster, paints, silica gels, cementitious materials, which has an important yet poorly understood effect on mechanical behavior. The confined fluid can generate enormous local stresses, as observed in granular material aging[80], wet floor friction [81], nano-tribology [82], multi-phase immiscible flows[83, 84], cement drying shrinkage[85, 86] and in everyday life experiences such as hardening of a drying sponge or building a sand castle on the beach[87]. Capillary condensation and evaporation potentially bring undesirable fracture processes, as in drying cracking of colloidal films [88, 89] and paints [90], but capillary stress can also can be exploited in nanomaterials fabrication by capillary force lithography [91], capillary rise infiltration [92, 93], evaporation-driven assembly and self-organization [94-97] and composite imbibition [98] and even used to evaluate the atmosphere of planets[99].Despite the broad importance of capillary forces, they remain challenging to predict in complex porous materials over the full range of liquid saturation, either in equilibrium or during a dynamical process of drainage/imbibition. For granular or colloidal materials, existing models addressing partial saturations are oversimplified and only apply either to low humidity (so called pendular/funicular regimes) [100][101][102] or to idealized geometries (slit/cylindrical independent pores or single sphere against a wall) [103][104][105][106]. At higher saturations, models based on geometrical analysis of Young-Laplace equation for smaller clusters [107,108] are proposed but restricted to only equilibrium liquid distributions inside monodisperse packings. Molecular simula-tions are also difficult to apply, since the porous structure considered on the mesoscale (∼ 1 µm) are ∼10 orders of magnitude larger than the volume of a single molecule (∼ 1 nm).In this Article, we present a theoretical framework to compute capillary forces...