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

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.

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

1 Like

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

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

# Create a new empty part

part = mimics.Part()

# Add a sphere to the part

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.

# Calculate mask from the part

|

• |

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)

# Create a Part from the Sphere

# Convert the Part to a Mask

# Now you can use the ‘mask’ object as needed

|

• |

Notes:

· 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,

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