Destroy() error in Custom Parameters with size > 1

I’m building a component with a menu.
Every item in the menu creates its own custom parameters when selected.
So that the parameters don’t accumulate, I wrote this:

	custom_pars = parent().customPars
	for par in custom_pars:
		if par.name != 'Menu':
			base.par[par.name].destroy()

Which deletes everything (expect the menu) before adding the new parameters.
It works like a charm!
Except… when I try to change from a menu item that has a custom parameter with multiples values (e.g. Translate X Y Z), then TouchDesigner only deletes the first custom parameter (which is the one with multiples values) and gives me this error:

td.tdError: Invalid Par object.

If I select any other item from the menu, the error immediately gets solved (since the custom parameter with multiples values was already deleted when the error emerged).

I suspect it has something to do with appendFloat creating parameters with name + number when size is > 1 (e.g. Clouds size=1 creates Clouds, Trees size=2 creates Trees1 and Trees2) and somehow base.par[par.name].destroy() does not work anymore.

Hey @postd,

the reason is indeed that what often is refered to as “parameters” are in fact parameter groups. A single float parameter is also a parameter group. A parameter “tx” might be part of a parameter group that also holds parameters “ty” and “tz”.
Now when you destroy a single parameter of a parameter group - the whole group is being destroyed with is - hence trying to destroy the next parameter, TouchDesigner might not be able to find it anymore as it has already been removed.
So the best way to detroy parameters is actually to do it via their groups. A pargroup has the same destroy() method as a parameter (ParGroup Class - Derivative). What you would want to do is get all custom parameter groups (OP Class - Derivative):

customPars = parent().customParGroups
for pG in customPars:
	pGName = pG.name
	if pGName != 'Menu':
		pG.destroy()
...

Theoretically and for futureproofing, it might make sense to also test if the parameter or parameter group still exists:

if op('someOp').parGroup['someParGroupName'] is not None:
	...

Hope this helps
Markus

Thank you, Markus.
That was very enlightening.
It is fully working now!