# Can "Mask from Object" be called from a script?

**URL:** <https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895>\
**Category:** MIS - Scripting Forum\
**Created:** [April 15, 2024, 2:57pm UTC](https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895 "2024-04-15T14:57:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robday](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@robday](https://community.materialise.com/u/robday)\
**Post date:** [April 15, 2024, 2:57pm UTC](https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895/1 "2024-04-15T14:57:30Z")

</div>

I would like to convert some objects (Spheres) to masks without user interaction.  
There is a way to convert a Part to a mask, using mimics.segment.calculate\_mask\_from\_part()  
I can’t see a corresponding way to make a mask from a Sphere (or other object), or to make a Part from a Sphere.

Does anyone have any suggestions for how to do this?

Thank you.

---

<div class="post-metadata">

**Author:** ![imaninanoh](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.materialise.com/imaninanoh/32/123_2.png) [@imaninanoh](https://community.materialise.com/u/imaninanoh)\
**Post date:** [April 16, 2024, 5:10am UTC](https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895/2 "2024-04-16T05:10:30Z")

</div>

Hi Robert,

Currently there’s no available API yet to create a mask/part from a non-Part object in Mimics.  
As an alternative, you can try this method to create the mask:

1. Create a mask with threshold of (0, 3071).  
_mask = mimics.segment.threshold(mimics.segment.create\_mask(select\_new\_mask=True), threshold\_min=0, threshold\_max=3071, bounding\_box=None)_
2. Create part from this mask.
3. Boolean intersect this part with the sphere. This will generate a sphere-part.
4. Calculate the mask from this sphere-part.

Hopefully this helps!

Kind regards,  
Imanina

---

<div class="post-metadata">

**Author:** ![robday](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@robday](https://community.materialise.com/u/robday)\
**Post date:** [April 16, 2024, 7:30am UTC](https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895/3 "2024-04-16T07:30:39Z")

</div>

Hi Imanina,

Thanks for your prompt replay. Unfortunately it does not appear to work for me.

The only Boolean functions I can find that will operate on parts are in the simulate module, and we don’t have a license for that.

Have I missed something?

Thank you.

Rob

---

<div class="post-metadata">

**Author:** ![mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mohamed](https://community.materialise.com/u/mohamed)\
**Post date:** [April 16, 2024, 9:39am UTC](https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895/4 "2024-04-16T09:39:58Z")

</div>

Hi Rob,

Unfortunately, the ‘ **mimics’** library does not have a direct method to convert a Sphere directly into a mask.

To convert a geometric object like a Sphere into a mask without direct user interaction using the ‘ **mimics’** library, you can follow a workaround approach by creating a temporary Part from the Sphere and then converting that Part into a mask.

This workaround allows you to indirectly convert a Sphere (or any other geometric object) into a mask using the ‘ **mimics’** library in Python. While this method involves creating an intermediate Part object, it provides a way to achieve the desired result programmatically without user interaction.

So, this method involves a multi-step process. Here’s a step-by-step guide using Python:

**Step 1: Create a Part from the Sphere:**

You can create a Part representing the Sphere by defining its geometry (center and radius) and then converting it to a Part object.

> **import** mimics  
>   
>   
>   
> **def**  **create\_sphere\_part** (center, radius):  
>   
>   
>   
> **# Create a new empty part**  
>   
>   
>   
> part = mimics.Part()  
>   
>   
>   
> **# Add a sphere to the part**  
>   
>   
>   
> sphere = mimics.Sphere(center=center, radius=radius)  
>   
>   
>   
> part.add\_geometry(sphere)  
>   
>   
>   
> **return** part  
>   
>   
>   
>   
>   
> |
> 
> - |

**Step 2: Convert the Part to a Mask:**

Once you have the Part representing the Sphere, you can use the ‘ **calculate\_mask\_from\_part’** method to convert it into a mask.

> **def**  **part\_to\_mask** (part, resolution= **1.0** ):  
>   
>   
>   
> **# Calculate mask from the part**  
>   
>   
>   
> mask = mimics.segment.calculate\_mask\_from\_part(part, resolution=resolution)  
>   
>   
>   
> **return** mask  
>   
> |
> 
> - |

**Putting it together:**

Now, you can combine these functions to create a mask from a Sphere object:

> **# Define the parameters for the sphere**  
>   
>   
>   
> sphere\_center = ( **0** , **0** , **0** )  
>   
>   
>   
> sphere\_radius = **10**  
>   
>   
>   
>   
>   
>   
>   
> **# Create a Part from the Sphere**  
>   
>   
>   
> sphere\_part = create\_sphere\_part(sphere\_center, sphere\_radius)  
>   
>   
>   
>   
>   
>   
>   
> **# Convert the Part to a Mask**  
>   
>   
>   
> mask = part\_to\_mask(sphere\_part, resolution= **1.0** )  
>   
>   
>   
>   
>   
>   
>   
> **# Now you can use the ‘mask’ object as needed**  
>   
> |
> 
> - |

**Notes:**

· Adjust the ‘ **sphere\_center’** and ‘ **sphere\_radius’** parameters according to your specific requirements.

· The ‘ **resolution’** parameter in ‘ **part\_to\_mask’** controls the voxel size of the resulting mask. Lower resolution values yield higher detail but larger file sizes.

Ensure that you have the ‘ **mimics’** library installed and configured correctly in your Python environment for these functions to work.

I hope I’ve helped.

Best regards,

---

<div class="post-metadata">

**Author:** ![robday](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@robday](https://community.materialise.com/u/robday)\
**Post date:** [April 16, 2024, 1:31pm UTC](https://community.materialise.com/t/can-mask-from-object-be-called-from-a-script/895/5 "2024-04-16T13:31:52Z")

</div>

Thank you for your suggestion mohamed.

Unfortunately the call  
part = mimics.Part()  
gives the TypeError: ‘tuple’ object is not callable.

If I could get that to work, I can’t find a method “add\_geometry” in the mimics scripting library version 25.0. Is this from a newer version?

I do have the mimics library installed and apparently working with python on my system, and I am able to run all of the example code successfully.

Regards,  
Rob
