# Scripted Module and editor difference

**URL:** <https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893>\
**Category:** Support\
**Created:** [May 23, 2019, 9:35am UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893 "2019-05-23T09:35:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![prorai](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@prorai](https://discourse.slicer.org/u/prorai)\
**Post date:** [May 23, 2019, 9:35am UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/1 "2019-05-23T09:35:13Z")

</div>

Odd behavior when i run my Scriptable Module and when i run same code in Python interactor.  
i’m using threshold first to mask the segment and then applying seed grow for making segment, when i run it in python interactor it looks its working as expected , but same code in scripted module is not working as expected and i’m not seeing any difference with and without masking code

---

<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:** [May 23, 2019, 11:55am UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/2 "2019-05-23T11:55:46Z")

</div>

The most likely cause is that the GUI gets updated each time you hit return in the python interactor, but it’s blocked when executing a script. So you might need to call `slicer.app.processEvents()` in your script or otherwise make it event driven.

---

<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 23, 2019, 12:48pm UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/3 "2019-05-23T12:48:08Z")

</div>

Segment Editor only use asynchronous event processing for display, so if you see difference in processing results then there may be some other trivial mistake (e.g., you have multiple segment editor parameter nodes and you change the wrong one). You can typically find such errors very easily by using a [Python debugger](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/DebuggingTools).

---

<div class="post-metadata">

**Author:** ![prorai](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@prorai](https://discourse.slicer.org/u/prorai)\
**Post date:** [May 23, 2019, 2:30pm UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/4 "2019-05-23T14:30:04Z")

</div>

@pieper  
i tried to put `slicer.app.processEvents()` this before applying threshold and after `effect.self().onUseForPaint()`  
then the seedgrow Code from the example script referred by @lassoan  
and i’m sure that i’ve created Segment editor node once and using that for both the segments (masking and seedgrow )

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/a/5a066a5262d275b8b6ba7fed44b1cad88f954eab.png)  
this is the effect when i call my scripted module.

and  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/5/85ed568e7dc3927c99b9952a96b4e68c281b56ea.png)  
and this is when i use same code in python interactor  
threshold masking is used and seeds grown in that limited area.

---

<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 24, 2019, 5:40pm UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/5 "2019-05-24T17:40:18Z")

</div>

If you show the editor widget, do you see that “Editable intensity range” is enabled?

Put a breakpoint in AbstractScriptedSegmentEditorAutoCompleteEffect.py in `preview(self)` method. What value `intensityBasedMasking` is set to?

---

<div class="post-metadata">

**Author:** ![prorai](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@prorai](https://discourse.slicer.org/u/prorai)\
**Post date:** [May 29, 2019, 7:33am UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/6 "2019-05-29T07:33:21Z")

</div>

> [@lassoan](#):
>
> intensityBasedMasking

I have 2 scenarios ,

1. when i’m ruining my script in python interarctor , i can able to add breakpoints and i’m getting this value as True, and threshold masking works perfectly as expected .
2. when i call my script through batch file , i cant set the breakpoints and i cant see the “Editable intensity range” enable when slicer window pop up. , threshold masking doesn’t seems to be applied here.

so the thing is happening here as per my observation is, when i call the script module using batch file it goes in one shot and before the onUseForPaint() finishes the work, seedgrow code already gets executed and it doesn’t get that masking intensity.

i tried to put this code in thread , which doesn’t work , and i cant use the async/await call as i’m getting error for import asyncio. ☹

---

<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 29, 2019, 4:18pm UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/7 "2019-05-29T16:18:20Z")

</div>

> [@prorai](#):
>
> when i call my script through batch file

Do you attempt to run the script without a main window?

> [@prorai](#):
>
> when i call my script through batch file , i cant set the breakpoints

You should be able to set breakpoints the same way. To make sure your code runs after the debugger is attached, start you processing with a timer (e.g., after 5-10 seconds the application has started).

> [@prorai](#):
>
> i tried to put this code in thread

I don’t think multithreading is relevant here.

---

<div class="post-metadata">

**Author:** ![prorai](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@prorai](https://discourse.slicer.org/u/prorai)\
**Post date:** [May 30, 2019, 10:50am UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/8 "2019-05-30T10:50:14Z")

</div>

> [@lassoan](#):
>
> start you processing with a timer

its helped me to debug my code.  
and finally i found the cause , its the slicer version.  
i have Slicer 4.11.0-2018-11-13 nightly version which was set for batch file process, and i was testing in stable 4.10 editor which was working fine.  
so in this nightly build i believe that the masking plus seedgrow has some issue or may be solved in next nightly release.

i’m really thankful to @lassoan for all the support .

one last thing

> [@lassoan](#):
>
> Do you attempt to run the script without a main window?

i was using window mode .  
now everything is working in window mode but i just tried no-window mode and after crash this is the log i’m seeing

```auto
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const class QItemSelection &,const class QItemSelection &) : Invalid segment editor parameter set node
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onSegmentationNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const class QItemSelection &,const class QItemSelection &) : Invalid segment editor parameter set node
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onSegmentationNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
[WARNING][Qt] 30.05.2019 16:10:39 [] (unknown:0) - QObject::connect: Cannot connect (null)::layoutChanged(int) to qMRMLSegmentEditorWidget::onLayoutChanged(int)
[CRITICAL][Qt] 30.05.2019 16:10:39 [] (unknown:0) - void __cdecl qMRMLSegmentEditorWidget::updateWidgetFromMRML(void) : Invalid segment editor parameter set node

```

---

<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 30, 2019, 1:10pm UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/9 "2019-05-30T13:10:48Z")

</div>

> [@prorai](#):
>
> so in this nightly build i believe that the masking plus seedgrow has some issue or may be solved in next nightly release.

Masking in “Grow from Seeds” effect is available since latest stable (Slicer-4.10.2) and recent preview releases.

> [@prorai](#):
>
> i just tried no-window mode

I think Segment Editor currently requires a main application window (it may be possible to remove the dependency but we don’t plan to work on that right now).

---

<div class="post-metadata">

**Author:** ![prorai](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@prorai](https://discourse.slicer.org/u/prorai)\
**Post date:** [May 30, 2019, 1:55pm UTC](https://discourse.slicer.org/t/scripted-module-and-editor-difference/6893/10 "2019-05-30T13:55:35Z")

</div>

> [@lassoan](#):
>
> I think Segment Editor currently requires a main application window

strange! , cause same script when i was using for seedgrow only (without masking code) , it was running fine with --no-window mode.
