<div dir="ltr">Hello everyone. I was trying to layout with pyQGIS, and I already have the map frame with the layers, but now I need to add a grid, I tried with the code written down, but the grid didn't work. I'm new pyQGIS and maybe there are errors in the code.<div><br></div><div><font face="monospace">project = QgsProject().instance()<br>layer_name = "Curvas de nivel"<br>layout_name = "MyLayout"<br>layout_page = ["Letter", 0]<br>layers = QgsProject.instance().mapLayersByName(layer_name)[0]<br><br>for layout in project.layoutManager().printLayouts():<br>    if <a href="http://layout.name">layout.name</a>() == layout_name:<br>       project.layoutManager().removeLayout(layout)<br><br>layout = QgsPrintLayout(project)<br>page = QgsLayoutItemPage(layout)<br>page.setPageSize(layout_page[0], layout_page[1])<br>layout.pageCollection().addPage(page)<br>map = QgsLayoutItemMap(layout)<br>map.attemptMove(QgsLayoutPoint(5,5,0))<br>map.attemptResize(QgsLayoutSize(200,200,0))<br>map.setExtent(layers.extent())<br><br>map_grid =  QgsLayoutItemMapGrid("Grid", map)<br>map_grid.setEnabled(True)<br>map_grid.setIntervalX(layers.extent().width() / 3)<br>map_grid.setIntervalY(layers.extent().width() / 3)<br>map_grids =  QgsLayoutItemMapGridStack(map).addGrid(map_grid)<br>layout.addLayoutItem(map)<br><br><br>layout.setName(layout_name)<br>project.layoutManager().addLayout(layout)</font><br></div></div>