[cairo] pycairo matrix copy limitation

lloyd konneker bootch at nc.rr.com
Fri Jan 28 07:27:52 PST 2011


You can't copy (shallow or deep) a pycairo Matrix object using standard
Python idioms.  There is a workaround.

It just bit me.  A web search reveals one prior mention, hidden on the
pygtk mailing list.

I think copying transform matrices is common.  My app uses retained mode
(versus immediate mode) rendering and hierarchical modeling, which means
keeping a copy of a transformation matrix throughout the model tree.
Mostly use context.get_matrix(), but I also need to copy a retained
matrix.  (Or maybe my design is poor?)

Can I submit a patch to the pycairo documentation to warn of this?
(Or a fix to pycairo?)

This illustrates:

#!/usr/bin/env python

import cairo
import copy

foo = cairo.Matrix()
foo.translate(2,3)
print foo
print copy.copy(foo)
print copy.deepcopy(foo)
print cairo.Matrix() * foo  # workaround

# output
# cairo.Matrix(1, 0, 0, 1, 2, 3)
# cairo.Matrix(1, 0, 0, 1, 0, 0)
# cairo.Matrix(1, 0, 0, 1, 0, 0)
# cairo.Matrix(1, 0, 0, 1, 2, 3)



More information about the cairo mailing list