示例#1
0
def uv_corners(mesh, image=None):
    '''returns the four corner points of the UVTex layout
    for the sculpt map image'''
    debug(40, "primitive.uv_corners(%s, %s)" % (mesh.name, image.name))
    max_vu = XYZ(-99999.0, -99999.0, 0.0)
    min_vu = XYZ(99999.0, 99999.0, 0.0)
    max_uv = XYZ(-99999.0, -99999.0, 0.0)
    min_uv = XYZ(99999.0, 99999.0, 0.0)
    current_uv = mesh.activeUVLayer
    mesh.activeUVLayer = 'sculptie'
    if image:
        faces = [f for f in mesh.faces if f.image == image]
    else:
        faces = mesh.faces
    mesh.activeUVLayer = 'UVTex'
    for f in faces:
        for i in range(len(f.verts)):
            v = XYZ(round(f.uv[i][0], 6), round(f.uv[i][1], 6), 0.0)
            max_uv = max(max_uv, v)
            min_uv = min(min_uv, v)
            v = XYZ(round(f.uv[i][1], 6), round(f.uv[i][0], 6), 0.0)
            max_vu = max(max_vu, v)
            min_vu = min(min_vu, v)
    min_vu = XYZ(min_vu.y, min_vu.x, 0.0)
    max_vu = XYZ(max_vu.y, max_vu.x, 0.0)
    if min_vu == min_uv:
        min_uv.y = max_uv.y
    if max_vu == max_uv:
        max_vu.y = min_vu.y
    mesh.activeUVLayer = current_uv
    return min_vu, min_uv, max_vu, max_uv
示例#2
0
def uv_corners(mesh, image=None):
    '''returns the four corner points of the UVTex layout
    for the sculpt map image'''
    debug(40, "primitive.uv_corners(%s, %s)" % (mesh.name, image.name))
    max_vu = XYZ(-99999.0, -99999.0, 0.0)
    min_vu = XYZ(99999.0, 99999.0, 0.0)
    max_uv = XYZ(-99999.0, -99999.0, 0.0)
    min_uv = XYZ(99999.0, 99999.0, 0.0)
    current_uv = mesh.activeUVLayer
    mesh.activeUVLayer = 'sculptie'
    if image:
        faces = [f for f in mesh.faces if f.image == image]
    else:
        faces = mesh.faces
    mesh.activeUVLayer = 'UVTex'
    for f in faces:
        for i in range(len(f.verts)):
            v = XYZ(round(f.uv[i][0], 6), round(f.uv[i][1], 6), 0.0)
            max_uv = max(max_uv, v)
            min_uv = min(min_uv, v)
            v = XYZ(round(f.uv[i][1], 6), round(f.uv[i][0], 6), 0.0)
            max_vu = max(max_vu, v)
            min_vu = min(min_vu, v)
    min_vu = XYZ(min_vu.y, min_vu.x, 0.0)
    max_vu = XYZ(max_vu.y, max_vu.x, 0.0)
    if min_vu == min_uv:
        min_uv.y = max_uv.y
    if max_vu == max_uv:
        max_vu.y = min_vu.y
    mesh.activeUVLayer = current_uv
    return min_vu, min_uv, max_vu, max_uv
示例#3
0
def main():
    root = None
    start_time = Blender.sys.time()
    gui.debug(1, "started", SCRIPT)
    call_baker = False
    try:
        root = gui.ModalRoot()
        app = GuiApp(root)
        app.redraw()
        root.mainloop()
        call_baker = app.doBake
        root.destroy()
        if gui.platform == "darwin":
            os.system(
                '''/usr/bin/osascript -e 'tell app "System Events" to activate process "Blender"' '''
            )

    except:
        if root:
            root.grab_release()
            root.quit()
            root.destroy()
        raise

    if call_baker:
        app = BakeApp()
        app.bake()

    gui.debug(1, "ended in %.4f sec." % \
            (Blender.sys.time() - start_time), SCRIPT)
示例#4
0
def main():
    root = None
    start_time = Blender.sys.time()
    gui.debug(1, "started", SCRIPT)
    call_baker = False
    try:
        root = gui.ModalRoot()
        app = GuiApp(root)
        app.redraw()
        root.mainloop()
        call_baker = app.doBake
        root.destroy()
        if gui.platform == "darwin":
            os.system('''/usr/bin/osascript -e 'tell app "System Events" to activate process "Blender"' ''')

    except:
        if root:
            root.grab_release()
            root.quit()
            root.destroy()
        raise

    if call_baker:
       app = BakeApp()
       app.bake()

    gui.debug(1, "ended in %.4f sec." % \
            (Blender.sys.time() - start_time), SCRIPT)
示例#5
0
def uv_params(mesh, image=None):
    '''returns the offset, scale and rotation of the UVTex layout
    for the sculpt map image'''
    debug(40, "primitive.uv_params(%s, %s)" % (mesh.name, image.name))
    if 'UVTex' not in mesh.getUVLayerNames():
        return XYZ(0.0, 0.0, 0.0), XYZ(1.0, 1.0, 0.0), 0.0
    bl, tl, br, tr = uv_corners(mesh, image)
    hv = tl - bl
    wv = br - bl
    a = atan2(hv.x, hv.y)
    s = XYZ(Blender.Mathutils.Vector(wv.x, wv.y).length,
            Blender.Mathutils.Vector(hv.x, hv.y).length, 0.0)
    return bl + (tr - bl) / 2.0 - XYZ(0.5, 0.5, 0.0), s, a
示例#6
0
def main():
    root = None
    start_time = Blender.sys.time()
    gui.debug(1, "started", SCRIPT)
    root = gui.ModalRoot()
    root.title(LABEL)
    app = GuiApp(root)
    app.redraw()
    root.mainloop()
    root.destroy()
    if gui.platform == "darwin":
        os.system("""/usr/bin/osascript -e 'tell app "System Events" to activate process "Blender"' """)
    gui.debug(1, "ended in %.4f sec." % (Blender.sys.time() - start_time), SCRIPT)
示例#7
0
def uv_params(mesh, image=None):
    '''returns the offset, scale and rotation of the UVTex layout
    for the sculpt map image'''
    debug(40, "primitive.uv_params(%s, %s)" % (mesh.name, image.name))
    if 'UVTex' not in mesh.getUVLayerNames():
        return XYZ(0.0, 0.0, 0.0), XYZ(1.0, 1.0, 0.0), 0.0
    bl, tl, br, tr = uv_corners(mesh, image)
    hv = tl - bl
    wv = br - bl
    a = atan2(hv.x, hv.y)
    s = XYZ(
        Blender.Mathutils.Vector(wv.x, wv.y).length,
        Blender.Mathutils.Vector(hv.x, hv.y).length, 0.0)
    return bl + (tr - bl) / 2.0 - XYZ(0.5, 0.5, 0.0), s, a
示例#8
0
def main():
    root = None
    start_time = Blender.sys.time()
    gui.debug(1, "started", SCRIPT)
    root = gui.ModalRoot()
    root.title(LABEL)
    app = GuiApp(root)
    app.redraw()
    root.mainloop()
    root.destroy()
    if gui.platform == 'darwin':
        os.system(
            '''/usr/bin/osascript -e 'tell app "System Events" to activate process "Blender"' '''
        )
    gui.debug(1, "ended in %.4f sec." % (Blender.sys.time() - start_time),
              SCRIPT)
示例#9
0
 def set_shape(self, name, filename=None):
     gui.debug(40, 'set_shape("%s", "%s")' % (name, filename), SCRIPT)
     self.shape_name.set(name)
     self.shape_file = filename
     if filename:
         # reading the image file leaves it in the blend
         # can uncomment this when able to remove the temporary image
         # i = Blender.Image.Load(filename)
         # sculpt_type = sculpty.map_type(i)
         # if sculpt_type == "TORUS":
         self.radius_input.config(state=NORMAL)
         # else:
         # 	self.radius_input.config(state=DISABLED)
         # self.x_faces_input.config(to=i.size[0] / 2)
         # self.y_faces_input.config(to=i.size[1] / 2)
     else:
         if name[:5] == "Torus":
             self.radius_input.config(state=NORMAL)
         else:
             self.radius_input.config(state=DISABLED)
     self.x_faces_input.config(to=256)
     self.y_faces_input.config(to=256)
     self.update_info()
     self.redraw()
示例#10
0
 def set_shape(self, name, filename=None):
     gui.debug(40, "set_shape(\"%s\", \"%s\")" % (name, filename), SCRIPT)
     self.shape_name.set(name)
     self.shape_file = filename
     if filename:
         # reading the image file leaves it in the blend
         # can uncomment this when able to remove the temporary image
         #i = Blender.Image.Load(filename)
         #sculpt_type = sculpty.map_type(i)
         #if sculpt_type == "TORUS":
         self.radius_input.config(state=NORMAL)
         #else:
         #	self.radius_input.config(state=DISABLED)
         #self.x_faces_input.config(to=i.size[0] / 2)
         #self.y_faces_input.config(to=i.size[1] / 2)
     else:
         if name[:5] == "Torus":
             self.radius_input.config(state=NORMAL)
         else:
             self.radius_input.config(state=DISABLED)
     self.x_faces_input.config(to=256)
     self.y_faces_input.config(to=256)
     self.update_info()
     self.redraw()
示例#11
0
    def add(self):
        Blender.Window.WaitCursor(1)
        editmode = Blender.Window.EditMode()
        if editmode:
            Blender.Window.EditMode(0)
        name = self.shape_name.get()
        basename = name.split(os.sep)[-1]
        if self.shape_file:
            baseimage = Blender.Image.Load(self.shape_file)
            sculpt_type = sculpty.map_type(baseimage)
        else:
            sculpt_type = name.upper()
            baseimage = None
        gui.debug(11, "Add sculptie (%s) of type %s" % (name, sculpt_type), SCRIPT)
        scene = Blender.Scene.GetCurrent()
        for ob in scene.objects:
            ob.sel = False
        try:
            mesh = sculpty.new_mesh(
                basename,
                sculpt_type,
                self.x_faces.get(),
                self.y_faces.get(),
                self.levels.get(),
                self.clean_lods.get(),
                min(0.5, max(self.radius.get(), 0.05)),
            )
            s, t, w, h, clean_s, clean_t = sculpty.map_size(self.x_faces.get(), self.y_faces.get(), self.levels.get())
            image = create_new_image(basename, w, h, 32)
            # print "add_mesh_sculpt_mesh: Packed image", image.getName()

            sculpty.bake_lod(image)
            ob = scene.objects.new(mesh, basename)
            mesh.flipNormals()
            ob.sel = True
            ob.setLocation(Blender.Window.GetCursorPos())
            sculpty.set_map(mesh, image)
            if baseimage:
                sculpty.update_from_map(mesh, baseimage)
            if self.levels.get():
                if self.sub_type.get():
                    mods = ob.modifiers
                    mod = mods.append(Blender.Modifier.Types.SUBSURF)
                    mod[Blender.Modifier.Settings.LEVELS] = self.levels.get()
                    mod[Blender.Modifier.Settings.RENDLEVELS] = self.levels.get()
                    mod[Blender.Modifier.Settings.UV] = False
                    if not self.subdivision.get():
                        mod[Blender.Modifier.Settings.TYPES] = 1
                else:
                    mesh.multires = True
                    mesh.addMultiresLevel(self.levels.get(), ("simple", "catmull-clark")[self.subdivision.get()])
                    mesh.sel = True
            if self.subdivision.get():
                for f in mesh.faces:
                    f.smooth = True
            # adjust scale for subdivision
            minimum, maximum = sculpty.get_bounding_box(ob)
            x = 1.0 / (maximum.x - minimum.x)
            y = 1.0 / (maximum.y - minimum.y)
            try:
                z = 1.0 / (maximum.z - minimum.z)
            except:
                z = 0.0
            if sculpt_type == "TORUS Z":
                z = min(0.5, max(self.radius.get(), 0.05)) * z
            elif sculpt_type == "TORUS X":
                x = min(0.5, max(self.radius.get(), 0.05)) * x
            elif sculpt_type == "HEMI":
                z = 0.5 * z
            tran = Blender.Mathutils.Matrix([x, 0.0, 0.0], [0.0, y, 0.0], [0.0, 0.0, z]).resize4x4()
            mesh.transform(tran)
            # align to view
            try:
                quat = None
                if Blender.Get("add_view_align"):
                    quat = Blender.Mathutils.Quaternion(Blender.Window.GetViewQuat())
                    if quat:
                        mat = quat.toMatrix()
                        mat.invert()
                        mat.resize4x4()
                        ob.setMatrix(mat)
            except:
                pass
            if self.save_settings.get() or self.save_defaults.get():
                settings = {
                    "x_faces": self.x_faces.get(),
                    "y_faces": self.y_faces.get(),
                    "levels": self.levels.get(),
                    "subdivision": self.subdivision.get(),
                    "sub_type": self.sub_type.get(),
                    "clean_lods": self.clean_lods.get(),
                    "radius": self.radius.get(),
                    "shape_name": self.shape_name.get(),
                    "shape_file": self.shape_file,
                    "quads": self.quads.get(),
                    "save": self.save_settings.get(),
                }
                Blender.Registry.SetKey(REGISTRY, settings, self.save_defaults.get())
        except RuntimeError:
            raise
        Blender.Window.EditMode(editmode)
        Blender.Window.WaitCursor(0)
        self.master.quit()
示例#12
0
 def add(self):
     Blender.Window.WaitCursor(1)
     editmode = Blender.Window.EditMode()
     if editmode:
         Blender.Window.EditMode(0)
     name = self.shape_name
     basename = name.split(os.sep)[-1]
     if self.shape_file:
         baseimage = Blender.Image.Load(self.shape_file)
         sculpt_type = sculpty.map_type(baseimage)
     else:
         sculpt_type = name.upper()
         baseimage = None
     gui.debug(11,
             "Add sculptie (%s) of type %s" % (name, sculpt_type),
             "add_mesh_sculpt_mesh")
     scene = Blender.Scene.GetCurrent()
     for ob in scene.objects:
         ob.sel = False
     try:
         mesh = sculpty.new_mesh(basename, sculpt_type,
                 self.x_faces, self.y_faces,
                 self.levels, self.clean_lods,
                 min(0.5, max(self.radius, 0.05)))
         s, t, w, h, clean_s, clean_t = sculpty.map_size(self.x_faces,
                 self.y_faces, self.levels)
         image = create_new_image(basename, w, h, 32)
         sculpty.bake_lod(image)
         ob = scene.objects.new(mesh, basename)
         mesh.flipNormals()
         ob.sel = True
         ob.setLocation(Blender.Window.GetCursorPos())
         sculpty.set_map(mesh, image)
         if baseimage:
             sculpty.update_from_map(mesh, baseimage)
         if self.levels:
             if self.sub_type:
                 mods = ob.modifiers
                 mod = mods.append(Blender.Modifier.Types.SUBSURF)
                 mod[Blender.Modifier.Settings.LEVELS] = self.levels
                 mod[Blender.Modifier.Settings.RENDLEVELS] = self.levels
                 mod[Blender.Modifier.Settings.UV] = False
                 if not self.subdivision:
                     mod[Blender.Modifier.Settings.TYPES] = 1
             else:
                 mesh.multires = True
                 mesh.addMultiresLevel(self.levels,
                     ('simple', 'catmull-clark')[self.subdivision])
                 mesh.sel = True
         if self.subdivision:
             for f in mesh.faces:
                 f.smooth = True
         # adjust scale for subdivision
         minimum, maximum = sculpty.get_bounding_box(ob)
         x = 1.0 / (maximum.x - minimum.x)
         y = 1.0 / (maximum.y - minimum.y)
         try:
             z = 1.0 / (maximum.z - minimum.z)
         except:
             z = 0.0
         if sculpt_type == "TORUS Z":
             z = min(0.5, max(self.radius, 0.05)) * z
             print "Create a Torus Z with radius ", z
         elif sculpt_type == "TORUS X":
             x = min(0.5, max(self.radius, 0.05)) * x
             print "Create a Torus X with radius ", x
         elif sculpt_type == "HEMI":
             z = 0.5 * z
         tran = Blender.Mathutils.Matrix([x, 0.0, 0.0], [0.0, y, 0.0],
                 [0.0, 0.0, z]).resize4x4()
         mesh.transform(tran)
         # align to view
         try:
             quat = None
             if Blender.Get('add_view_align'):
                 quat = Blender.Mathutils.Quaternion(
                         Blender.Window.GetViewQuat())
                 if quat:
                     mat = quat.toMatrix()
                     mat.invert()
                     mat.resize4x4()
                     ob.setMatrix(mat)
         except:
             pass
     except RuntimeError:
         raise
     Blender.Window.EditMode(editmode)
     Blender.Window.WaitCursor(0)
     return ob
示例#13
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)))