Adding Silkscreen via script

Hello All,
I want to add silkscreen text to some components programmatically while hiding the default reference silkscreen text.
I am not sure how to do this. Can anyone tell me how to extend/change my existing code:

board = pcbnew.LoadBoard(bfn)
for a in range(6):
    s=board.FindFootprintByReference("SW"+str(a+1))
    s.SetProperty('Silk',"MyCustomSilk"+str(a+1))
    s.Reference().SetVisible(False) # Hide the "SWx" reference
    ...

I am not sure how to make the MyCustomSilk[x] show up as silkscreen.

Thanks.

Application: KiCad PCB Editor

Version: 6.0.7-f9a2dced07~116~ubuntu20.04.1, release build

Libraries:
	wxWidgets 3.0.4
	libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3

Platform: Linux 5.4.0-124-generic x86_64, 64 bit, Little endian, wxGTK, xubuntu, x11

Build Info:
	Date: Jul 26 2022 19:32:41
	wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8) GTK+ 3.24
	Boost: 1.71.0
	OCC: 7.5.2
	Curl: 7.84.0
	ngspice: 36
	Compiler: GCC 9.4.0 with C++ ABI 1013

Build settings:
	KICAD_USE_OCC=ON
	KICAD_SPICE=ON

You need to create an FP_TEXT object (or modify a clone of one) and add it to the footprint.

So something like this:

my_text = s.Reference().Clone()
my_text.SetText("MyCustomSilk")
s.Add(my_text)

Tweak my_text size, angle, style as needed. See FP_TEXT and EDA_TEXT class reference KiCad Pcbnew Python Scripting: pcbnew.FP_TEXT Class Reference

s.Reference().Clone()
<pcbnew.EDA_ITEM; proxy of <Swig Object of type 'EDA_ITEM *' at 0x7f6b4fd8ef30> >
m=s.Reference().Clone()
m.SetText("MyCustomSilk")
Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pcbnew.py", line 1659, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, EDA_ITEM, name)
  File "/usr/lib/python3/dist-packages/pcbnew.py", line 80, in _swig_getattr
    raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
AttributeError: 'EDA_ITEM' object has no attribute 'SetText'

Looks like Clone doesn’t cast to the correct type. Then try to create a copy with constructor like
my_text = pcbnew.FP_TEXT(s.Reference())

>>> my_text = pcbnew.FP_TEXT(s.Reference())
Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pcbnew.py", line 17952, in __init__
    this = _pcbnew.new_FP_TEXT(*args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_FP_TEXT'.
  Possible C/C++ prototypes are:
    FP_TEXT::FP_TEXT(FOOTPRINT *,FP_TEXT::TEXT_TYPE)
    FP_TEXT::FP_TEXT(FOOTPRINT *)

No copy constructor then. Don’t you just love how “consistent” the API is. Creating a new one it is
my_text = pcbnew.FP_TEXT(s).
You may have to set things like layer and visibility manually.