# Extension to execute python function

**URL:** <https://discourse.slicer.org/t/extension-to-execute-python-function/31478>\
**Category:** Feature requests\
**Created:** [August 31, 2023, 3:35pm UTC](https://discourse.slicer.org/t/extension-to-execute-python-function/31478 "2023-08-31T15:35:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [August 31, 2023, 3:35pm UTC](https://discourse.slicer.org/t/extension-to-execute-python-function/31478/1 "2023-08-31T15:35:15Z")

</div>

Is there a simple extension available to execute python code.

Would be a great way to customize 3DSlicer and allow execution of custom code with a button.

I find my self using code such as this and it would be great to have a button up top…

def threeDDIsplay():  
import slicer  
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass(‘vtkMRMLSegmentationNode’)  
segmentationNode.CreateClosedSurfaceRepresentation()  
layoutManager = slicer.app.layoutManager()  
threeDWidget = layoutManager.threeDWidget(0)  
threeDView = threeDWidget.threeDView()  
threeDView.resetFocalPoint()  
displayNode = segmentationNode.GetDisplayNode()  
displayNode.SetOpacity3D(0.3)  
print(‘3D’)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 31, 2023, 3:44pm UTC](https://discourse.slicer.org/t/extension-to-execute-python-function/31478/2 "2023-08-31T15:44:12Z")

</div>

This example may help:

> [@How to add button on 2d or 3d window?](https://discourse.slicer.org/t/how-to-add-button-on-2d-or-3d-window/31174/2):
>
> Good question - this code seems to work. You’d need to add some extra code if you want to have it show up in the lower right corner and track window resizes and things. \>\>\> sliceButton = qt.QPushButton("Slice") \>\>\> sliceButton.setParent(slicer.app.layoutManager().sliceWidget("Red")) \>\>\> sliceButton.geometry = qt.QRect(0,0,200,200) \>\>\> sliceButton.show() \>\>\>
