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

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



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,

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