I have a scene composed of multiple objects. One of the objects is a lamp (that looks like a house). This lamp has differnet parts to it, but is a single Blender object. The parts are shown below with different colors: roof, windows, walls and floor.

I’m starting to texture paint only this object, but I’m having trouble painting its different parts independently from each other, as when I paint the windows, I can accidentaly go over the walls for example.
The only way I’ve found to paint each aprt independently is by separting the mesh by material, so now I have 4 individual objects. That’s how I managed to show each aprt of the lamp with different colors in the image above.
But I’m wondering, was separating the lamp in different objects the only way? I would have lioked to keep it as a single object, and have some klind of mask that only allowed to hand paint a texture in the active material. Is this possible?
I found something that looked promising but didn’t work for my case as this masks out a single material, whereas I have 4 materials in this object, so I needed to mask out 3 of them:

By the way, I’m using a single UV Map with a single texture:
