Why clone a line node without display properties?

image

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

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

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

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

and then clone it… get this result

Why clone a line node without display properties?

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 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.

@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)?

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

1 Like
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

@lassoan Thanks

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

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…

CopyContent copies color:

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.