Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

Storing a reference to a parameter

General discussion about anything TouchDesigner

Storing a reference to a parameter

Postby bnRockwell » Fri Jun 15, 2018 5:46 pm

Hello forum! I was wondering if there's an easy way to create pointers to parameters. So for example, I define an array of parameter references stored at the top of a chop execute, but fetch, modify, and update them within the valuechange function. So basically I want to pass by reference instead of by value.

Thanks!
B
bnRockwell
 
Posts: 26
Joined: Tue May 29, 2018 11:52 am

Re: Storing a reference to a parameter

Postby raganmd » Fri Jun 15, 2018 6:23 pm

Hey B,

Have you looked into using python storage?

You can place all sorts of goodies into storage and retrieve them based on a the key they're stored as. Here's a quick overview:

https://youtu.be/B_awPnzhAMQ?t=29m38s

While we're here you might look at this example:
base_storage.tox
(1.29 KiB) Downloaded 29 times


Here we create a variable outside of our function that points to our dictionary in storage, then in the execution of the function we clear the table, update the values, then change the seed parameter for the noise CHOP. This means that our table values are one seed behind the vals in the noise CHOP.

Is this the kind of thing you're looking for?
raganmd
 
Posts: 1296
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA

Re: Storing a reference to a parameter

Postby bnRockwell » Tue Jun 19, 2018 6:15 pm

Thank you Matt! This definitely helps clear some things up; I still need to play around with these concepts to fully understand how it works.

With respect to using 'local' as a base that stores everything - do you move the default 'local' base to be inside of your overall project container?
bnRockwell
 
Posts: 26
Joined: Tue May 29, 2018 11:52 am

Re: Storing a reference to a parameter

Postby raganmd » Tue Jun 19, 2018 6:58 pm

I don't normally use local variables these days - though they're still a functioning feature of Touch.

Here's an example of how to set this up.
base_variables_example.tox
(758 Bytes) Downloaded 31 times


Local variables will inherit - from the parent object. You can see here in base_variable_example that 'test' evaluates to the results in local. The same is true for base1.

base1, however, has it's own set of variables that are retrieved instead of the parent's.

So the idea would be that you'd create a set of local variables at the levels of your project where you need them. Storage is similar, though more pythonic in nature, and a little easier to wrangle IMO, though both work well.

Hope that helps!
raganmd
 
Posts: 1296
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA

Re: Storing a reference to a parameter

Postby bnRockwell » Wed Jun 27, 2018 4:31 pm

Thanks again Matt! Follow up question for anyone:

Let's say I have a chop execute, and I have several functions defined within it that alter a given parameter. Instead of constantly referencing the parameter in its entirety, I decide to store the parameter.

So, what's the best practice for storing the parameter? IE do I just run a script once to store the relevant param? And then when I want to retrieve the reference from storage, where in my chop execute should I do that? I notice that if I try fetching something from storage and assigning it to a variable at the beginning of my script, I run into problems where the assignment isn't occurring.
bnRockwell
 
Posts: 26
Joined: Tue May 29, 2018 11:52 am

Re: Storing a reference to a parameter

Postby raganmd » Thu Jul 19, 2018 1:19 pm

I think I understand where you're going with this. A note about storing a parameter - this is great for reading vals, but not ideal for setting vals. Instead I'll do something like store the operator. In an execute DAT you might assign your variable outside of your methods (assuming that your reference here isn't changing every execution), then do the assignment in the method. That would look like this in a very simple form:

Code: Select all
noise = parent().fetch('noise_TOP')

def onValueChange(channel, sampleIndex, val, prev):
   
   noise.par.seed = val
   
   return


Here's a tox as well:
base_store_and_set.tox
(878 Bytes) Downloaded 23 times


Does that make sense?
raganmd
 
Posts: 1296
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA

Re: Storing a reference to a parameter

Postby bnRockwell » Fri Jul 20, 2018 2:50 pm

Thanks Matt, yes it does. I was just hoping that there'd be a nicer way of setting values without have to go "op.par.val" all over the place, but if that's the best practice, I'll stick with it. Thanks again.
bnRockwell
 
Posts: 26
Joined: Tue May 29, 2018 11:52 am

Re: Storing a reference to a parameter

Postby raganmd » Fri Jul 20, 2018 4:50 pm

I looked at this one more time, and I think you can do something like this:

Code: Select all
noise = parent().fetch('noise_TOP')
seed_par = parent().fetch('seed_par')

def onValueChange(channel, sampleIndex, val, prev):
   
   seed_par.val = val
      
   return


That .val call should let you set the value of the parameter. Here's an example tox:
base_store_and_set_val.tox
(1.13 KiB) Downloaded 21 times
raganmd
 
Posts: 1296
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA

Re: Storing a reference to a parameter

Postby bnRockwell » Tue Jul 24, 2018 9:48 am

Thanks Matt. As a follow up question; when you have a chop execute (or any execute), and there's a definition outside of a function, when does that get evaluated? It seems like in some cases, definitions outside of functions can fail to be evaluated if they aren't there when first 'activated'.
bnRockwell
 
Posts: 26
Joined: Tue May 29, 2018 11:52 am

Re: Storing a reference to a parameter

Postby raganmd » Wed Jul 25, 2018 1:01 pm

I'm pretty sure what's outside of the function is only called when you edit and then click away from the execute code.

It's worth pointing out that this will not work as expected:

Code: Select all
msg = parent().fetch('my_val')

def onOffToOn(channel, sampleIndex, val, prev):
   
   print(msg)
   
   return


This will only ever print the value fetched from storage during the last edit of the execute code. If your value in storage will change, you'll need to do something like this:

Code: Select all
def onOffToOn(channel, sampleIndex, val, prev):
   msg = parent().fetch('my_val')
   print(msg)
   
   return
raganmd
 
Posts: 1296
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA


Return to General TouchDesigner Discussion

Who is online

Users browsing this forum: Google [Bot] and 2 guests