Skip to content

Graphics instructions don't seem to be designed to have multiple parents, yet there is no check preventing this. #9220

@gottadiveintopython

Description

@gottadiveintopython

Software Versions

  • Python: CPython 3.13
  • OS: Linux Mint
  • Kivy: 91dc72d (master branch around Sep 2025)
  • Kivy installation method: development install ($ pip install -e ".[dev,full]")

Describe the bug

The Instruction class has an attribute that holds its parent.

cdef class Instruction(ObjectWithUid):
cdef int flags
cdef public str group
cdef InstructionGroup parent

To me, this implies that graphics instructions cannot be shared between multiple parents.
However, when you run the following code, no error or warning is raised:

import kivy.core.window  # Ensures OpenGL context exists. This doesn't seem to matter, though.
from kivy.graphics import InstructionGroup, Color

g1 = InstructionGroup()
g2 = InstructionGroup()
color = Color()

g1.add(color)
g2.add(color)

I have made this mistake a couple of times when working with stencil instructions:

with canvas:
    StencilPush()
    shared = RoundedRectangle(...)
    StencilUse()
    ...
    StencilUnUse()
    canvas.add(shared)
    StencilPop()

I haven't encountered any incorrect rendering results from this, though.
Still, I feel that some kind of check—or at least documentation—is neccesarry.

Additional context

InstructionGroup.add() calls Instruction.radd()

cpdef add(self, Instruction c):
'''Add a new :class:`Instruction` to our list.
'''
c.radd(self)
self.flag_data_update()
return

which calls Instruction.set_parent()

cdef void radd(self, InstructionGroup ig):
ig.children.append(self)
self.set_parent(ig)

which performs no check.

cdef void set_parent(self, Instruction parent):
self.parent = parent

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions