<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
span.EpostStil17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="NO-BOK" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-GB">Hi, <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">I’m trying the set label masks for heights on a contour line layer.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">It works like a charm manually set with Label mask and Masks. I saved the style in a qml-file.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">However, when I open/add this style-file in python script, the mask settings (masked symbol layer and mask source) are missing?!? (Labels are checked when saving the qml-file)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Therefore, I would like to add the mask with pyQGIS, but how do I get the name (id) of the “Masked symbol layers” and the “Mask sources”?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Must Masks be set in Layer properties to appear? Ref.
<a href="https://gis.stackexchange.com/questions/447362/pyqgis-to-set-masked-symbol-layer-and-mask-source">
https://gis.stackexchange.com/questions/447362/pyqgis-to-set-masked-symbol-layer-and-mask-source</a><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Some of the code I’ve tried so far:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK"># Add geopackage<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">fileName = "karplantehoyder.gpkg"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">layer = QgsVectorLayer(fileName, "hoyder", "ogr")<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK"># Load style<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">layer.loadNamedStyle('./qml/n5hoyder.qml')  # color, line width, label font/size set, but not mask<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">label = QgsPalLayerSettings()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">label.MaskEnabled = True<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK"># masklayer = QgsMaskMarkerSymbolLayer()  # How do i set the mask symbol layer and mask source<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK"># masklayer.setMasks("hoyder")            # This is not working, have tried id and layer - not working<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">QgsProject.instance().addMapLayer(layer, True)
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">layer.saveStyleToDatabase(layer.name(),'style n5hoyder',True,'')<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">mask_settings = QgsTextMaskSettings()<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">mask_settings.maskedSymbolLayers()      # How do I set the contour line layer to be the Masked symbol layer?  
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:10.0pt;font-family:"Courier New";mso-fareast-language:NO-BOK">mask_settings.setMaskedSymbolLayers()   # and how do I set the contour layer to be the label mask?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Best regards<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Anne Nilsen<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
</div>
</body>
</html>