custom parameters into python script

Trying to learn how to make a custom parameters page spit out to Python and end up with a string like:

“100 2 99”

whereas:

“100” is ID
“2” is the number of my parameter (hardness ‘Par2’)
“99” is data from Par2

with Parameter execute DAT attempts I get :

‘TypeError: ‘td.containerCOMP’ object is not subscriptable’

I don’t really understand what that means, nor if I should be using Parameter execute DAT.

I got further with Chop Exectute DAT, but seemed like maybe not the purest way to go. Any guidance mucho aprec.
food.6.toe (3.88 KB)

I think the issue is that you’re not referencing the parent object’s parameters correctly. Line 12 should instead read something like:

print('%s %s %s' % (parent().par.Par0, parent().par.Par1, parent().par.Par2))

thanks LO5!

“print(‘%s %s %s’ % (parent().par.Par0, parent().par.Par1, parent().par.Par2))”

that definitely works in the right direction.
But I still cant figure out how to end up with the current active parameter name as 2nd part of the string ,
so say moving ‘hardness’, ‘color’, or ‘size’ slider would generate a string of “ID,par#,data” ,

parent().par.Par0 gives me the ID easily as first variable, but am at a loss as to following that with ‘Par1’, ‘Par2’ , or ‘Par3’ then its corresponding slider data,

so adjusting ‘hardness’ would generate something like ‘9,2,112’ (ID9,Par2,data from par2)
food.9.toe (3.88 KB)

Ah I see, I didn’t fully understand the original question.

If you want to get the active parameter’s name and value, you’d use par.name and par.val, respectively. So your print function call might look like:

print('%s %s %s' % (parent().par.Par0, par.name, par.val))

However, this will give you the full parameter name, and your textport output would read something like:

9 Par1 100

If you want to get a string’s trailing digits, you can use some extra python code such as what’s described here-- [url]Check what number a string ends with in Python - Stack Overflow. In this case, you’d be importing the re python module and calling the search method. For example:

[code]import re

def onValueChange(par, prev):

parNum = re.search(r'\d+$', par.name)
print('%s %s %s' % (parent().par.Par0, int(parNum.group()), par.val))
return[/code]

This will output:

9 1 100

thanks so much!

I was able to skip the re module import with par.name[3:] because all my parameters have same ‘Par[i]’ format.

thanks!

Nice! Simple ftw. :smiley:

Side note, to get the trailing digits on any string use:

digits(str)→ int or None:

Returns the numeric value of the last consecutive group of digits in the string, or None if not found. The search begins after the last slash if any are present.

tdu.digits(‘arm123’) # returns 123
tdu.digits(‘arm123/leg456’) # returns 456
tdu.digits(‘arm123/leg’) # returns None
Note this method will work on any string, but when given a specific operator, its more efficient to use its local digits member:

n = op(‘arm123/leg456’)
d = n.digits # returns 456

More info: docs.derivative.ca/Tdu_Module

1 Like

Thanks Rob! I didn’t realize digits is also a method. Very helpful.