<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">On 1/20/2021 5:25 PM, Etienne Trimaille
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAMtDFLLHHzG25yKaXAmUqM6SSWgZW-0d6vizJbMo_Mz-=LChtg@mail.gmail.com">
      <div>Can you try that?</div>
      <div><br>
      </div>
      <div>
        <pre class="gmail-code gmail-highlight" lang="python"><span id="gmail-LC20" class="gmail-line" lang="python"><span class="gmail-k">class</span> <font face="monospace">Renamer</font><span class="gmail-p">(</span><span class="gmail-n">QgsProcessingLayerPostProcessorInterface</span><span class="gmail-p">):</span></span>
<span id="gmail-LC21" class="gmail-line" lang="python">    <span class="gmail-n">instance</span> <span class="gmail-o">=</span> <span class="gmail-bp">None</span></span>
<span id="gmail-LC22" class="gmail-line" lang="python"></span>
<span id="gmail-LC23" class="gmail-line" lang="python">    <span class="gmail-k">def</span> <span class="gmail-nf">postProcessLayer</span><span class="gmail-p">(</span><span class="gmail-bp">self</span><span class="gmail-p">,</span> <span class="gmail-n">layer</span><span class="gmail-p">,</span> <span class="gmail-n">context</span><span class="gmail-p">,</span> <span class="gmail-n">feedback</span><span class="gmail-p">):</span></span>
<font face="monospace">        layer.setName('DiffLayer')</font>
<span id="gmail-LC28" class="gmail-line" lang="python"></span>
<span id="gmail-LC29" class="gmail-line" lang="python">    <span class="gmail-o">@</span><span class="gmail-nb">staticmethod</span></span>
<span id="gmail-LC30" class="gmail-line" lang="python">    <span class="gmail-k">def</span> <span class="gmail-nf">create</span><span class="gmail-p">()</span> <span class="gmail-o">-></span> <span class="gmail-s">'<font face="monospace">Renamer</font>'</span><span class="gmail-p">:</span></span>
<span id="gmail-LC31" class="gmail-line" lang="python">        <font face="monospace">Renamer</font><span class="gmail-p">.</span><span class="gmail-n">instance</span> <span class="gmail-o">=</span> <font face="monospace">Renamer</font><span class="gmail-p">()</span></span>
<span id="gmail-LC32" class="gmail-line" lang="python">        <span class="gmail-k">return</span> <font face="monospace">Renamer</font><span class="gmail-p">.</span><span class="gmail-n">instance


</span></span></pre>
        <pre class="gmail-code gmail-highlight" lang="python"><span style="font-family:arial,sans-serif">In your processAlgorithm method() :</span><span id="gmail-LC32" class="gmail-line" lang="python"><span class="gmail-n">


<span id="gmail-LC142" class="gmail-line" lang="python">                <span class="gmail-n">context</span><span class="gmail-p">.</span><span class="gmail-n">layerToLoadOnCompletionDetails</span><span class="gmail-p">(</span></span>
<span id="gmail-LC143" class="gmail-line" lang="python">                    <span class="gmail-n">data</span><span class="gmail-p">[</span><span class="gmail-n">layer</span><span class="gmail-p">].</span><span class="gmail-nb">id</span><span class="gmail-p">()</span></span>
<span id="gmail-LC144" class="gmail-line" lang="python">                <span class="gmail-p">).</span><span class="gmail-n">setPostProcessor</span><span class="gmail-p">(</span></span>
<span id="gmail-LC145" class="gmail-line" lang="python">                    <font face="monospace">Renamer</font><span class="gmail-p"></span><span class="gmail-p">.</span><span class="gmail-n">create</span><span class="gmail-p">()</span></span>
<span id="gmail-LC146" class="gmail-line" lang="python">                <span class="gmail-p">)</span></span>
</span></span>

</pre>
      </div>
    </blockquote>
    That works. Thanks. <br>
    I made a small change because I didn't know  what data[layer] was
    referring to. Instead I used<br>
    <blockquote>     <font face="monospace">  
context.layerToLoadOnCompletionDetails(self.dest_id).setPostProcessor(Renamer.create())</font><br>
    </blockquote>
    where self.dest_id is the second value returned from the
    ParameterAsSink call.<br>
    <br>
    Your solution leads me to conjecture there is either a temporal
    scoping issue with just using Renamer() in the call to
    setPostProcessor(), or else it has something to do with how Python
    passes parameters by object reference. Either way, by the time the
    postProcessorLayer is called, there's nothing there. This leads to a
    slightly different solution that is more flexible if there are
    multiple output layers:<br>
    <blockquote><font face="monospace">class Renamer
        (QgsProcessingLayerPostProcessorInterface):<br>
            def __init__(self, layer_name):<br>
                self.name = layer_name<br>
                super().__init__()<br>
                <br>
            def postProcessLayer(self, layer, context, feedback):<br>
                layer.setName(self.name)<br>
                <br>
        renamer = Renamer("DiffBuf")<br>
        <br>
        <later in the processAlgorithm method><br>
      </font><font face="monospace">context.layerToLoadOnCompletionDetails(self.dest_id).setPostProcessor(renamer)</font><br>
    </blockquote>
  </body>
</html>