I have a .qml file that I am using on a project importing photos using the 'import photos' plugin. Every import batch needs to have the color manually changed after importing in order to distinguish it from the other imports. This is something I'll be doing hundreds of times. Is there a way to set the qml file to randomize the color when importing the data?
Yes there is a way, and it is only a bit hacky:
In a point file, open up the symbology and hit the "Data defined override" to the right of
Fill color (for reference see the image). In the context menu which opens up, choose
Here you can paste this code:
'#' || right(@layer_id, 6)
Then you can save the symbology as .qml and use it to import your photos.
The code takes advantage of how the layer ids get generated in QGIS. The layer name will be appended with somewhat random hex values, which we can use to change colors. Since every layer gets a unique layer ID, every layer will also most likely get a unique color.
You could open the python console and execute the code below, for QGIS 3.X, to randomly change all the layers' colors.
from random import randrange layers = [layer for layer in QgsProject.instance().mapLayers().values()] for layer in layers: layer.renderer().symbol().setColor(QColor.fromRgb(randrange(256), randrange(256), randrange(256))) #redraw the layer layer.triggerRepaint() #refresh the legend iface.layerTreeView().refreshLayerSymbology(layer.id())