Example #1
0
    def bake(self):
        #print "bake_sculpt_mesh_util(): Baker started"
        startTime = Blender.sys.time()  #for timing purposes

        # We must bake in Object Mode (contraint imposed by blender)
        editmode = Blender.Window.EditMode()
        #print "Using editmode ... ", editmode
        Blender.Window.WaitCursor(1)
        if editmode:
            Blender.Window.EditMode(0)

        # prepare for bake, set centers and create bounding box
        bb = sculpty.BoundingBox(local=True)
        bb.rgb.min = sculpty.XYZ(
                self.range_min_r,
                self.range_min_g,
                self.range_min_b)
        bb.rgb.max = sculpty.XYZ(
                self.range_max_r,
                self.range_max_g,
                self.range_max_b)
        bb.rgb.update()

        
        scene = Blender.Scene.GetCurrent()
        if not self.keep_center:
            sculpty.set_selected_object_centers(scene)

        selection = []
        selection.extend(scene.objects.selected)
        for ob in selection:
            if sculpty.check(ob):
                bb.add(ob)            
        if self.keep_scale:
            bb = bb.normalised()
        if self.keep_center:
            bb = bb.centered()
        # Good to go, do the bake
        success = False
        image = None
        a = self.alpha
        if a == 3:
            alpha_image = Blender.Image.Load(self.alpha_filename)

        print "do_optimize is set to:", self.optimize_resolution
        image_dict = {}
        log = ""
        number_of_bake_candidates = len(selection)
        success = False
        for ob in selection:
            Blender.Window.WaitCursor(1)
            can_bake, reassigned_sculptmap, image, lg = check_for_bake(ob, image_dict, self.auto_correct)
            is_active = sculpty.active(ob)
            log = log + lg
            print "Object", ob.getName(), "can bake:",can_bake, "is active:", is_active
            if can_bake and is_active:

                if sculpty.bake_object(ob, bb, self.clear,
                            self.keep_seams, self.keep_center, self.optimize_resolution):
                    if need_rebake(ob, self.auto_correct):
                        if self.auto_correct:
                            sculpty.bake_object(ob, bb, self.clear,
                                self.keep_seams, self.keep_center, self.optimize_resolution)
                            log = log + "Info: Rebaked ob["+ob.getName()+"]. (reason: flipped vertex normals) |"
                            #print "rebake done"
                        else:
                            log = log + "Warn: ob["+ob.getName()+"] has flipped vertex normals. Please check for inside/out|"

                    success = success or True
                
                for image in sculpty.map_images(ob.getData(False, True)):
                    n = Blender.sys.splitext(image.name)
                    print "Baking [", n[0], "]"
                    if n[0] in ["Untitled", "Sphere_map", "Torus_map",
                            "Cylinder_map", "Plane_map", "Hemi_map",
                            "Sphere", "Torus", "Cylinder", "Plane", "Hemi"]:
                        image.name = ob.name
                    if self.range_scale:
                        if 'primstar' not in image.properties:
                            image.properties['primstar'] = {}
                        image.properties['primstar']['scale_x'] /= bb.rgb.scale.x
                        image.properties['primstar']['scale_y'] /= bb.rgb.scale.y
                        image.properties['primstar']['scale_z'] /= bb.rgb.scale.z
                    if self.fill:
                        sculpty.fill_holes(image)
                    if self.finalise:
                        sculpty.finalise(image)
                        if a == 2:
                            sculpty.bake_preview(image)
                        elif a == 1:
                            sculpty.clear_alpha(image)
                        elif a == 3:
                            sculpty.set_alpha(image, alpha_image)
                    if image.packed:
                        image.pack()
                    image.glFree()
        if self.activeObject != None:
            self.activeObject.select(True)
        if editmode:
            #print "Return to edit mode now ..."
            Blender.Window.EditMode(1)
        else:
            #print "Return to object mode now ..."
            if success and number_of_bake_candidates == 1:
                #print "stop by in edit mode"
                Blender.Window.EditMode(1)
                Blender.Redraw()
                Blender.Window.EditMode(0)

        for ob in selection:
            ob.select(True)
            
        if log != "":
            text = "Bake Report %t|" + log +"|OK"
            #print text
            Blender.Draw.PupMenu(text)  
        #Blender.Window.WaitCursor(0)
        #print "release cursor 2"
        Blender.Redraw()

        if success:
            gui.debug(1, 'finished baking: in %.4f sec.' % \
                    ((Blender.sys.time() - startTime)))
        else:
            gui.debug(0, 'bake failed after %.4f sec.' % \
                    ((Blender.sys.time() - startTime)))
Example #2
0
def bake_sculptie():
		print "--------------------------------"
		startTime = Blender.sys.time()  #for timing purposes
		editmode  = Blender.Window.EditMode()
		if editmode: Blender.Window.EditMode(0)
		Blender.Window.WaitCursor(1)
		# prepare for bake, set centers and create bounding box
		bb = sculpty.BoundingBox()
		bb.rgb.min = sculpty.XYZ( GLOBALS['minR'].val, GLOBALS['minG'].val, GLOBALS['minB'].val )
		bb.rgb.max = sculpty.XYZ( GLOBALS['maxR'].val, GLOBALS['maxG'].val, GLOBALS['maxB'].val )
		bb.rgb.update()

		scene = Blender.Scene.GetCurrent()
		for ob in scene.objects.selected:
			if sculpty.check( ob ):
				ob.properties["ps_bake_final"]     = GLOBALS['doFinal'].val
				ob.properties["ps_bake_fill"]      = GLOBALS['doFill'].val
				ob.properties["ps_bake_scale"]     = GLOBALS['keepScale'].val
				ob.properties["ps_bake_center"]    = GLOBALS['keepCenter'].val
				ob.properties["ps_bake_clear"]     = GLOBALS['doClear'].val
				ob.properties["ps_bake_protect"]   = GLOBALS['doProtect'].val
				ob.properties["ps_bake_preview"]   = GLOBALS['protect_type_silhouette'].val == True
				ob.properties["ps_bake_min_r"]     = GLOBALS['minR'].val
				ob.properties["ps_bake_min_g"]     = GLOBALS['minG'].val
				ob.properties["ps_bake_min_b"]     = GLOBALS['minB'].val
				ob.properties["ps_bake_max_r"]     = GLOBALS['maxR'].val
				ob.properties["ps_bake_max_g"]     = GLOBALS['maxG'].val
				ob.properties["ps_bake_max_b"]     = GLOBALS['maxB'].val
				ob.properties["ps_bake_scale_rgb"] = GLOBALS['doScaleRGB'].val
				if not ob.properties["ps_bake_center"]:
					#center new
					sculpty.set_center( ob )
				bb.add( ob )
		if ob.properties["ps_bake_scale"]:
			bb = bb.normalised()
		if ob.properties["ps_bake_center"]:
			bb = bb.centered()
		# Good to go, do the bake
		success = False
		for ob in scene.objects.selected:
			if sculpty.active( ob ):
				if sculpty.bake_object( ob, bb, ob.properties["ps_bake_clear"] ):
					success = True
				for image in sculpty.map_images( ob.getData( False, True) ):
					n = Blender.sys.splitext( image.name )
					if n[0] in ["Untitled", "Sphere_map", "Torus_map", "Cylinder_map", "Plane_map", "Hemi_map", "Sphere", "Torus","Cylinder","Plane","Hemi" ]:
						image.name = ob.name
					if ob.properties["ps_bake_scale_rgb"]:
						image.properties['ps_scale_x'] /= bb.rgb.scale.x
						image.properties['ps_scale_y'] /= bb.rgb.scale.y
						image.properties['ps_scale_z'] /= bb.rgb.scale.z
					if ob.properties["ps_bake_fill"]:
						sculpty.fill_holes( image )
					if ob.properties["ps_bake_final"]:
						sculpty.finalise( image )
						if ob.properties["ps_bake_protect"]:
							if ob.properties["ps_bake_preview"]:
								sculpty.bake_preview( image )
							else:
								sculpty.clear_alpha( image )

		print "--------------------------------"
		print 'finished baking: in %.4f sec.' % ((Blender.sys.time()- startTime))
		Blender.Redraw()
		if editmode: Blender.Window.EditMode(1)
		Blender.Window.WaitCursor(0)
		if not success:
			Blender.Draw.PupBlock( "Sculptie Bake Error", ["No objects selected"] )