How to change color of 3d-cube inside 3D-View?

I am new to Slicer. I got a question:
How to change color of 3D-cube inside 3D-view through python-interactor?
Thanks a lot!
Aijun

I don’t think you can do that. But you can hide it and create your own:

# Choose your color
RGB_COLOR = 0.2, 0.4, 0.8

# Hide the cube
v = slicer.util.getNode('View1')
v.SetBoxVisible(False)

# Create yours
cube = vtk.vtkCubeSource()
cube.SetXLength(200)
cube.SetYLength(200)
cube.SetZLength(200)
cube.Update()
cubePolyData = cube.GetOutput()

# Create a model with the cube and add it to scene
modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
modelNode.SetAndObservePolyData(cubePolyData)
modelNode.CreateDefaultDisplayNodes()
modelNode.SetName('My cube')

# Edit display properties
displayNode = modelNode.GetDisplayNode()
displayNode.SetLighting(False)
displayNode.SetColor(RGB_COLOR)
WIREFRAME = 1
displayNode.SetRepresentation(WIREFRAME)
displayNode.SetBackfaceCulling(False)
1 Like

Thanks you so much, Fernando!
I do appreciate your help very well!

It worked well after I did a little bit change:
#modelNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLModelNode’)
–> modelNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLModelNode())
I am not sure of why AddNewNodeByClass was not there.

Thank you again! Great job!

regards,
Aijun

1 Like

You can mark my answer so that it helps next person who gets in the thread for help.

You probably have an old version of Slicer. I think the commit that introduced that function is from May.

Thanks a lot.
I guess you are right, I am using an old one.

By the way, I am facing the new issue of the center. I got an model there, is there way to get the center of the model object so that I may get my cube-center matched?

Thanks in advance!
Aijun

Try modelNode.GetPolyData().GetCenter().

hi Fernando,
Thanks a lot, But I am still stuck with how to get the modelNode.GetPolyData().GetCenter() working (I guess just because I am new). I am still not sure of the classes structures/config for the slicer view.

Best,
Aijun

I’m not sure I understand the problem. Can you please rephrase? What are you doing? What do you expect? What do you get?

Hi Fernando,
sorry for a long time to get back here to check with your expertise on the slicer.
Here is a snapshot:
image
you see here, I get your idea of making a new cube scene but I have to no idea how to get my object and the cube sharing the same center.

Thanks a lot,
Aijun

I just saw your answer. Were you able to solve the problem?

A post was split to a new topic: How to change the size of the cube in 3D views