# Why clone a line node without display properties?

**URL:** <https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922>\
**Category:** Development\
**Tags:** python\
**Created:** [September 30, 2021, 4:10am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922 "2021-09-30T04:10:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 30, 2021, 4:10am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/1 "2021-09-30T04:10:35Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/9/69d7ea198515b33164a452caee809669176e90b1.png)

Why clone a line node without display properties?  
克隆一条直线为啥不带显示属性呢?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [September 30, 2021, 8:51am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/2 "2021-09-30T08:51:20Z")

</div>

What do you do, what do you expect, and what happens instead? What Slicer version?

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 30, 2021, 9:36pm UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/3 "2021-09-30T21:36:00Z")

</div>

slicer 4.13  
我画了一条直线如图, 并且设置了显示属性加粗和颜色…

> I drew a straight line as shown in the figure, and set the display attributes bold and color…

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/1/9198492ddc2e91035f65a6a7ce48d3d361a0da6d.png)

and then clone it… get this result

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/b/bb0b3618687299afce7937abc13b50ac8ea4641a.png)

Why clone a line node without display properties?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 30, 2021, 9:47pm UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/4 "2021-09-30T21:47:36Z")

</div>

I guess the cloned node is using default markups properties, which make sense, but probably copying display properties would be a more desirable behavior. You can submit a bug report to [https://issues.slicer.org](https://issues.slicer.org) and we’ll try to get to it soon.

If you copy the node programmatically then after cloning you can simply copy the content of the original node’s display node to the cloned node’s display node.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 12, 2022, 11:34pm UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/5 "2022-05-12T23:34:31Z")

</div>

> [@lassoan](#):
>
> If you copy the node programmatically then after cloning you can simply copy the content of the original node’s display node to the cloned node’s display node.

@lassoan  
Howto _copy the content of the original node’s display node to the cloned node’s display node_ **except those that have been modified display properties** , such as the line diameter and color of the lineNode display properties, etc. programmatically (py)?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 13, 2022, 12:50am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/6 "2022-05-13T00:50:30Z")

</div>

You can copy all content of a node into another, using the `CopyContent` method.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 13, 2022, 2:05am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/7 "2022-05-13T02:05:08Z")

</div>

```py
def cloneNode(modNode):
  """Create a copy from input nodes and return the new instance"""
  modClass = modNode.GetClassName()
  modCopy = slicer.mrmlScene.AddNewNodeByClass(modClass)
  modCopy.CopyContent(modNode, False)
  modCopy.SetName(f"{modNode.GetName()}_copy")
  modCopy.GetDisplayNode().CopyContent(modNode.GetDisplayNode(), False)
  modCopy.GetDisplayNode().SetVisibility3D(True)
  modCopy.GetDisplayNode().SetVisibility2D(True)
  return modCopy

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/b/5bcc174f93ba8d1f760fdd97144b96056b29ce05.png)

@lassoan Thanks

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 13, 2022, 5:06pm UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/8 "2022-05-13T17:06:43Z")

</div>

The `cloneNode` function that you created takes a MRML node object as input. It fails because you passed a string as input.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 21, 2022, 12:56am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/9 "2022-05-21T00:56:10Z")

</div>

```auto
def cloneNode(modName="None",modNode=None):
  """Create a copy from input nodes and return the new instance"""
  if modName!="None":
    modNode=slicer.util.getNode(modName)
  else:
    modName=modNode.GetName()
  modClass = modNode.GetClassName()
  modCopy = slicer.mrmlScene.AddNewNodeByClass(modClass)
  modCopy.CopyContent(modNode,False)
  modCopy.SetName(f"{modNode.GetName()}_copy")
  modCopyDn=slicer.mrmlScene.CreateNodeByClass(modNode.GetDisplayNode().GetClassName())
  modCopyDn.CopyContent(modNode.GetDisplayNode(),False)
  modCopyDn.SetVisibility3D(True)
  modCopyDn.SetVisibility2D(True)
  return modCopy

```

这样似乎可以了, 只是不知道为啥颜色属性没有copy过来…

> This seems to work, but I don’t know why the color attribute is not copied…

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 21, 2022, 2:04am UTC](https://discourse.slicer.org/t/why-clone-a-line-node-without-display-properties/19922/10 "2022-05-21T02:04:16Z")

</div>

`CopyContent` copies color:

> <https://github.com/Slicer/Slicer/blob/43e8f5b2407b4c83509406b1acf266e25412eb49/Libs/MRML/Core/vtkMRMLDisplayNode.cxx#L272-L273>

If you can provide an example (source code and data) that behaves differently than you expect then we can have a look.

Your code snippet above looks good, except I would recommend only use `slicer.util.getNode` for testing and debugging, not for any production code. Instead, when you create or read a node, store the returned node variable or node ID. A node name does not uniquely identify a node, because many nodes can have the same name.

If you want to clone any node that shows up in the subject hierarchy tree (for example, in Data module) then you can use [this code snippet](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#clone-a-node).
