I’m new to Python in Touch and have hit a block in terms of understanding. My goal is to achieve the following:
Create a prototype “Flower”
Replicate the Flower several times to get n flowers
Arrange the flowers in space
… SO FAR SO GOOD … then:
Each cook call a script (“flowerBehavior” for example) in each replicated object from a remote master script where
remote call:
op("flower_1").updateFlower(255,255,64,921,0.8);
flowerBehavior:
def updateFlower(red, green, blue, animTime, bloomValue)
# set color values based on incoming values
# set animation time of an asset
# etc
Is this possible? I have read the tutorials and looked in the examples doc and must me missing something. Assuming I can get the op address scoping correct is it possible to remotely call methods in scripts inside other ops?
Have you tried using an Execute DAT set to cook on Frame Start, so it would cook every frame, and at that beginning of the frame you could reference your replicator table to see how many flowers you have, then use a for loop to iterate through the flowers and run the script inside each one.
Hi elburz - yes that’s more or less exactly what I want to do. I understand and have used the Execute DAT as you describe - my question is how to embed a “free-floating” script inside each replicator table entry. How do I call a defined function in an operator from a remote Execute DAT? I’m probably missing something very simple. Thanks!
Im using the numRows of table1 to run a for loop, so that even if more rows are added to table1, It’ll get the new items replicated as well. And then every frame, it goes through the for loop n amount of times where n = numRows, using the numeric iteration of the loop in the path name along with things that are static, such as ‘item’ and ‘/flower’, as well as passing arguments through to the script its running, which you catch inside each script using args[i]:
basically the above get changed to something like op(‘item0/flower’).run(args) if its the first time the for loop is running, op(‘item1/flower’).run(args) if its the 2nd, etc etc. Thats probably the way I’d do it.
If you really wanted to use functions, you could make a single module outside of all of the replicated items, then what you could do is run a for loop where you pass the item number you want to affect to the function, and then the function from outside, would run it’s various actions on that specific item. replicator example.toe (4.29 KB)
Regarding your last paragraph - are you suggesting that one script would maintain knowledge of how to manipulate every “thing?” This is another approach for sure. Ideally I would like to author portable containers that have their own logic “behaviors” built in. Looks like the op.run technique in your example is perhaps the best expression of that?
Yes you could approach it from that side as well, where you have 1 single function that sits outside all of the replicated items, and along with the regular info you’d like to send it, like rgb values etc, you’d also send the number or name of the item you want operated on, and it would create a path to the item before running its set of actions.
Really depends how you’re comfortable and how you’ll be transmuting it down the line I believe. This run way is pretty straight forward for perhaps this one.
For future ones, you might actually want to check out the new python extensions in builds 21000+. Then you could do what you mentioned where it would be op(‘example’).updateStats(r,g,b,etc). I’m just scratching the surface with them, and they’re a little bit buggy since it’s an experimental build, so if you’re doing this for a job, probably stick with the op.run method for now, otherwise would be good to experiment with the extensions.
Also another way to do this is to have it as a function in the DAT such as updateFlower() in the original example, and reference the DAT itself using the mod() function instead of op(). That will get the DAT as a module, and then you can call .updateFlower() on the returned object.
It’s much more readable than using the run() and args[] arrays in the script.