code: def _visible_layers_count(self):
          
          treeroot = QgsProject.instance().layerTreeRoot()
          return len([lyr for lyr in treeroot.findLayers() if lyr.isVisible()])
docs: Calculate the number of visible layers in the legend.
  
          .. versionadded: 3.1
  
          :returns: Count of layers that are actually visible.
          :rtype: int