# Template Canvas Property Change

**URL:** <https://forum.inductiveautomation.com/t/template-canvas-property-change/78135>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [August 21, 2023, 6:42am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135 "2023-08-21T06:42:49Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 24, 2023, 11:33am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/22 "2023-08-24T11:33:10Z")

</div>

> [@Dharani\_T](#):
>
> script should be written on each template

Great. I'm glad you were able to adapt my proof of concept for your use case, but for posterity, I want to reiterate what I said in my original post:

> [@justinedwards.jle](#):
>
> You will probably want to move the template scripts into a single function in your script library to eliminate redundancy.

**Edit:** I developed the second code example further to eliminate this redundancy.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 24, 2023, 12:17pm UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/23 "2023-08-24T12:17:04Z")

</div>

Under no circumstances should any significant code be placed at the outermost level of a property change script. This script will be executed for _ **every change** _ to _ **every property** _ on this component. You will be constructing the function in the `def` _from scratch_ on _ **every property change** _ whether you care about the particular change or not.

Guard _ **everything** _ in Vision property change events with an

```auto
if event.propertyName=="something":

```

check at the outermost indent level.

If you must, place the `def` inside the `if...`. But better (way, way better, and faster) is to move function definitions to a script in the project library, where its `def` will happen once and persist for all callers to use quickly. (Same with imports.)

Edit: Justin, you introduced this abomination in your advice above. You've offered similar advice in other topics, citing convenience to excuse the poor practice. Please stop.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 24, 2023, 1:05pm UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/24 "2023-08-24T13:05:26Z")

</div>

I can't let this pass:

> [@justinedwards.jle](#):
>
> I wrote it the way I did to be a simple, easy to follow example

It would have been an extra few seconds for you to show two code blocks, with one labeled "place this in a project library script", and the other for the event, altered to call `someScript.someFunction()`. Conveys the same information, without displaying _ **bad practices** _.

> [@justinedwards.jle](#):
>
> with the understanding that a little more development would be necessary

That is simply _ **not** _ a reasonable understanding for most supplicants here. Or lurkers. Do you really think our OP will go back and split up his **working** code into two pieces? Or even understand what parts need to be where?

Let me repeat: please stop.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 24, 2023, 1:12pm UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/25 "2023-08-24T13:12:10Z")

</div>

> [@pturmel](#):
>
> That is simply _ **not** _ a reasonable understanding for most supplicants here.

Point taken, and thank you for the stern rebuke. ~~When I get time, I'll go back and correct the answers above.~~ Feel free to PM me with any other answers you take issue with, and if I can, I'll fix them as well.

@pturmel, @Dharani_T : The solution has been corrected.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 25, 2023, 6:37am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/26 "2023-08-25T06:37:02Z")

</div>

Thanks for the corrected code !!.

As said earlier,  
i have 6 circles (Circle 1, Circle 2..) for 6 canvas  
How to make

```auto
circle = canvas.parent.getComponent('Circle')

```

as dynamic

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 25, 2023, 6:46am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/27 "2023-08-25T06:46:52Z")

</div>

I imagined that each canvas and circle would be separated into six container components. In this way, they there would be no need for the circles and canvases to have unique names.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 25, 2023, 6:49am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/28 "2023-08-25T06:49:53Z")

</div>

I have 6 canvas and 6 circles separate as

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/8/c8f0312e7b56842d1346122b589512ba932a953d.png)

but how to have separate each canvas and circle.  
Can u explain if u dont mind.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 25, 2023, 6:52am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/29 "2023-08-25T06:52:47Z")

</div>

I see. That layout is not what I had envisioned. Is template1 always associated with circle1, and 2 with 2, and so on?

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 25, 2023, 6:53am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/30 "2023-08-25T06:53:26Z")

</div>

yes you are right

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 25, 2023, 7:32am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/31 "2023-08-25T07:32:49Z")

</div>

In your setCircleColor function, you could probably use the name attribute of the canvas to create the needed suffix for your circle.

The canvas will always be correct, since it is the direct ancestor of the component that calls the function that gets it, so the circle which is called by name would be the only part of the scripting that would need to be dynamic. To do this, you would change this line:

```auto
circle = canvas.parent.getComponent('Circle')

```

to this:

```auto
circleSuffix = canvas.name.replace('Template Canvas', '')
circleName = 'Circle' + circleSuffix
circle = canvas.parent.getComponent(circleName)

```

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 25, 2023, 7:37am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/32 "2023-08-25T07:37:00Z")

</div>

Working as expected!! Thank you for the support as always.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 25, 2023, 7:52am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/33 "2023-08-25T07:52:06Z")

</div>

You're welcome. FWIW, this post was my 100th solution, so...

Thanks!

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 25, 2023, 8:01am UTC](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135/34 "2023-08-25T08:01:40Z")

</div>

> [@justinedwards.jle](#):
>
> FWIW, this post was my 100th solution, so...

Only 1000 more to catch up with Phil !

[Previous page](https://forum.inductiveautomation.com/t/template-canvas-property-change/78135.md?page=1)
