I create a new print layout using PyQgis and then I open it in the layout designer. Everything seems ok till I try to refresh the page or update the map extent, then map disappears.
project = QgsProject.instance()
lmg = QgsProject.instance().layoutManager()
l = QgsPrintLayout(project)
l.initializeDefaults()
l.setUnits(QgsUnitTypes.LayoutMillimeters)
lmg.clear()
lmg.addLayout(l)
page = l.pageCollection().pages()[0]
lm = 20 # left margin
tm = 32 # upper margin
w, h = 900, 600 #width and height
page.setPageSize(QgsLayoutSize(1189, 841))
theMap = QgsLayoutItemMap(l)
theMap.updateBoundingRect()
theMap.setRect(QRectF(lm,tm, w, h))
theMap.setPos(lm,tm)
theMap.setFrameEnabled(True)
theMap.updateBoundingRect()
theMap.setLayers(project.mapThemeCollection().masterVisibleLayers())
theMap.setExtent(QgsRectangle(363600.0, 376800.0, 364500.0 ,377400.0))
theMap.attemptSetSceneRect(QRectF(lm,tm, w, h))
l.addItem(theMap)
l.updateBounds()
iface.openLayoutDesigner(l)