from mcib3d.geom import Objects3DPopulation
from mcib3d.image3d import ImageInt
from ij import IJ,WindowManager
from mcib_plugins.tools import RoiManager3D_2
import time

#@ UpdateService updateService

update_site_name = "3D ImageJ Suite"

update_site = updateService.getUpdateSite(update_site_name)

if update_site is not None:
	if not update_site.isActive():
		IJ.log(update_site_name 	+ " update site needs to be activated to use this plugin.\nGo to >Help >Update... and then use the Manage update sites button.")
		
	else:
	
		# get current image, an image with labelled objects
		current_image=WindowManager.getCurrentImage()
		
		if current_image is not None:
			rm = RoiManager3D_2()
			rm.create3DManager()
			
			
			# wrap ImagePlus into 3D suite image format
			img=ImageInt.wrap(current_image)
			
			# create a population of 3D objects
			population=Objects3DPopulation(img)
			object_count=population.getNbObjects()
			time.sleep(1)
			
			# loop over the objects
			for i in range(0, object_count):
				obj=population.getObject(i)
				#print(obj.getVolumeUnit())
				rm.addObject3D(obj)
		else:
			IJ.log("No available image found")
else:
	IJ.log(update_site_name 	+ " update site not existing")

WindowManager.getWindow("Log").dispose()