How to automatically make the title of your print equal to the name of the printed layer? Tried by using the next piece of code, but it doesn't work (QGIS 3.X):
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def func(feature, parent):
root = QgsProject.instance().layerTreeRoot()
layer = qgis.utils.iface.activeLayer()
lyr = root.findLayer(QgsProject.instance().mapLayersByName(layer.name())[0].id())
group = lyr.parent().name()
if group != '':
return lyr.parent().name()
else:
return 'Group not found'