Search code examples
pythongraphicspyx

What is the type of the below argument in Pyx?


The official documentation describes that layers can be used to set the occlusion of rendered elements

canvas.layer(name, above=None, below=None)

This method creates or gets a layer with name name.

A layer is a canvas itself and can be used to combine drawing operations for ordering purposes, i.e., what is above and below each other. The layer name name is a dotted string, where dots are used to form a hierarchy of layer groups. When inserting a layer, it is put on top of its layer group except when another layer instance of this group is specified by means of the parameters above or below.

Well I tried the following:

c = canvas.canvas().layer("top")
t = canvas.canvas().layer("bot", below="top")
t = canvas.canvas().layer("bot", below=c)
t = canvas.canvas().layer("bot", below=0)

They all return with some error. For example the string version:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pyx/canvas.py", line 296, in layer
    group, layer = name.split(".", 1)
ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "EdgeRefining/refine_edges.py", line 174, in <module>
    t = canvas.canvas().layer("bot", below="top")
  File "/usr/lib/python3/dist-packages/pyx/canvas.py", line 312, in layer
    self.items.insert(self.items.index(self.layers[below]), self.layers[name])
KeyError: 'top'

Has anyone used this functionality?


Solution

  • Layers are canvas instances within a canvas. Here is an example:

    from pyx import *
    
    c = canvas.canvas()
    l1 = c.layer('l1')
    l2 = c.layer('l2')
    
    l1.fill(path.circle(0, 0, 2), [color.rgb.red])
    l2.fill(path.circle(3, 0, 2), [color.rgb.green])
    
    c.writePDFfile()
    

    Now you can add below='l1' when creating the layer l2 and the red circle will be placed above the green circle. The problem in your code was, that you created new canvas instances all the time, however, multiple layers are to be used within the same canvas.