Issues with animating stencil frame size

Here is my code simplified:

from panda3d.core import *
from direct.gui.DirectGui import *
from direct.interval.LerpInterval import *

import direct.directbase.DirectStart

constantOneStencil = StencilAttrib.make(1, StencilAttrib.SCFAlways,
                                        StencilAttrib.SOZero, StencilAttrib.SOReplace,
                                        StencilAttrib.SOReplace, 1, 0, 1)

stencilReader = StencilAttrib.make(1, StencilAttrib.SCFEqual,
                                   StencilAttrib.SOKeep, StencilAttrib.SOKeep,
                                   StencilAttrib.SOKeep, 1, 1, 0)

cm = CardMaker("cardmaker")
cm.setFrame(-.5, .5, -.5, .5)

viewingSquare = render.attachNewNode(cm.generate())
viewingSquare.reparentTo(base.camera)
viewingSquare.setPos(0, 5, 0)

viewingSquare.node().setAttrib(constantOneStencil)
viewingSquare.node().setAttrib(ColorWriteAttrib.make(0))
viewingSquare.setBin('background', 0)
viewingSquare.setDepthWrite(0)

def changestencil(t):
    cm.setFrame(-t,t,-t,t) # not working as expected
    print(t)

anim = LerpFunc(changestencil,fromData=0.5,toData=1,duration=1)

button = DirectButton(scale=6,command=anim.start)
button.node().setAttrib(stencilReader)

run()

The animation function is being called normally but the frameSize of the stencil isn’t changing. How can I animate the frameSize and what am I doing wrong?

cm.generate() is a one-time generation step, changing settings on it doesn’t automatically rescale the card.

You should either re-run cm.generate() and replace the existing node, or you should use setScale() to alter the size of the card.

That makes sense, how can I add that into my code? I tried running cm.generate() and that doesn’t seem to work

edit: I cant use setScale as in certain cases I’ll need to change the frameSize attributes independently rather than uniformly

I got it to work doing this:

def changestencil(t):
    cm.setFrame(-t,t,-t,t)
    viewingSquare = render.attachNewNode(cm.generate())
    viewingSquare.reparentTo(base.camera)
    viewingSquare.setPos(0, 5, 0)

    viewingSquare.node().setAttrib(constantOneStencil)
    viewingSquare.node().setAttrib(ColorWriteAttrib.make(0))
    viewingSquare.setBin('background', 0)
    viewingSquare.setDepthWrite(0)

I’m sure there’s a better way to do it though