# Copy Object Details by Value, Not Reference

**URL:** <https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501>\
**Category:** Ignition\
**Tags:** ignition80, scripting, perspective\
**Created:** [October 29, 2021, 2:31pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501 "2021-10-29T14:31:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [October 29, 2021, 2:31pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/1 "2021-10-29T14:31:13Z")

</div>

Good Morning. I’m working Perspective on the 8.0.16 Gateway. I have a script that is copying the `props.elements` of an SVG to use elsewhere in the project. When I update my copy of the data, it is also changing the original object’s data, since I’m actually given a reference to the original object, not a copy of the object. How can I go about copying the data without simply receiving a reference to the original object?

```auto
#Get the element data from the original object
element_adjusted = self.getSibling('test_2').props.elements[0]

#Update the information for the new object (also updates the original object)
element_adjusted.transform = 'translate(50,0) scale (1.0)'

#Write the adjusted data to the new object
elements_list.append(element_adjusted) 
self.getSibling('aggregate_layout').props.elements = elements_list

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 29, 2021, 2:38pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/2 "2021-10-29T14:38:39Z")

</div>

Not much time for to dig in here, but would this work?  
`element_adjusted = self.getSibling('test_2').props.elements[0].copy()`

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 29, 2021, 2:41pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/3 "2021-10-29T14:41:57Z")

</div>

FYI many objects in igntion have a `.copy()` function that can be used, always worth checking that out first.

Generally speaking, see what functions and properties are available on a given object,

```auto
x = some.ignition.object.thing
for option in dir(x):
    print option

```

will give you a list of properties and callable functions on said object - if `copy` is in that list, you can use `.copy()`.

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [October 29, 2021, 3:25pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/4 "2021-10-29T15:25:46Z")

</div>

Thanks for the quick responses guys. There is no `copy` function for this object, but there is a ` __copy__ ` - not sure what that means.

Trying `copy()` —

```auto
component_copy = self.getSibling('test_2').copy()

elements_list = []
elements_list.append(component_copy.props.elements[0]) 
self.getSibling('aggregate_layout').props.elements = elements_list 

```

Created this error —

```auto
Error running action 'component.onActionPerformed' on 
pages/system/overview@C$0:1:0:2/root/coord_overview/Button: 
Traceback (most recent call last): File "<function:runAction>", line 7, in
 runAction AttributeError: 'com.inductiveautomation.perspective.gateway.script' 
object has no attribute 'copy'

```

Trying ` __copy__ ` —

```auto
component_copy = self.getSibling('test_2'). __copy__

elements_list = []
elements_list.append(component_copy.props.elements[0]) 
self.getSibling('aggregate_layout').props.elements = elements_list 

```

Created this error —

```auto
Error running action 'component.onActionPerformed' on 
pages/system/overview@C$0:1:0:2/root/coord_overview/Button: 
Traceback (most recent call last): File "<function:runAction>", line 10, 
in runAction AttributeError: 'builtin_function_or_method' object has no attribute 'props'

```

It’s interesting this one seemed to work (didn’t error out), but returned a ‘builtin\_function\_or\_method’ object.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 29, 2021, 8:41pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/5 "2021-10-29T20:41:03Z")

</div>

Ok, let’s try this:

```python
import copy

component_copy = copy.copy(self.getSibling('test_2')) # shallow copy
# -or-
component_copy = copy.deepcopy(self.getSibling('test_2')) # deep copy
```

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [November 1, 2021, 12:27pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/6 "2021-11-01T12:27:05Z")

</div>

`copy.copy(self.getSibling('test_2'))` returns this error:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/3/730a8816fb7e391e842b2516469315a01b7bf8e4.png)

`copy.deep_copy(self.getSibling('test_2'))` returns this error:

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

Ever used monkey patching as recommended in the error message? Don’t want to take the time to learn how to use it to find out that it’s not what I need.

Worst case, I’m looking at simply reading through the elements’ JSON and recreating a copy line-by-line. Definitely not the quickest way.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 1, 2021, 1:51pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/7 "2021-11-01T13:51:57Z")

</div>

What if you put the property you were trying to copy, instead of the entire component?  
`element_adjusted = copy(self.getSibling('test_2').props.elements[0])`

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [November 1, 2021, 2:16pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/8 "2021-11-01T14:16:28Z")

</div>

I had that same thought earlier, but had no dice. Calling either

```auto
copy(self.getSibling('test_2').props.elements[0])

```

or

```auto
copy(self.getSibling('test_2').props.elements)

```

both give error

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/a/0af91e1867eae824b1da82320607592eec62aba3.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 1, 2021, 2:46pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/9 "2021-11-01T14:46:59Z")

</div>

What component are you working with? I’ll take a closer look when time allows.

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [November 1, 2021, 5:14pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/10 "2021-11-01T17:14:30Z")

</div>

I’m using the Drawing component, created by dragging an SVG image into the Designer and selecting to Embed it. Screenshot of the object properties and the JSON of the elements are attached.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/4/e421a8c71ce42653b55ee0bd709a57267e6ae3f6.png)

```auto
[
  {
    "type": "group",
    "elements": [
      {
        "type": "path",
        "fill": {
          "paint": "#E03DBD"
        },
        "style": {},
        "d": "M 0,0 50,0 50,20 0,20",
        "name": "path_01"
      }
    ],
    "name": "layer1",
    "transform": "translate(0,0) scale(1.0)"
  }
]

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 1, 2021, 5:40pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/11 "2021-11-01T17:40:41Z")

</div>

You can do a recursive descent function, converting to builtin types each step of the way, like this:

> [@Function to convert json string or python dict into ObjectWrapper](https://forum.inductiveautomation.com/t/function-to-convert-json-string-or-python-dict-into-objectwrapper/32861/16):
>
> Forcing each to the expected type while traversing makes a deep copy: def sanitize\_tree(element): if hasattr(element, '\_\_iter\_\_'): if hasattr(element, 'keys'): return dict((k, sanitize\_tree(element[k])) for k in element.keys()) else: return list(sanitize\_tree(x) for x in element) return element EDIT: Got rid of the unnecessary zip() and used only one keys() iterator instead of two.

---

<div class="post-metadata">

**Author:** ![m.boog](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@m.boog](https://forum.inductiveautomation.com/u/m.boog)\
**Post date:** [November 1, 2021, 8:05pm UTC](https://forum.inductiveautomation.com/t/copy-object-details-by-value-not-reference/52501/12 "2021-11-01T20:05:51Z")

</div>

The sanitize\_tree function snippet did the trick for me. Thanks for the help, gentlemen.
