[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4497] contrib/py/scripts/addons/ oscurart_tools/oscurart_meshes.py: IO Groups rewritten!

Campbell Barton ideasman42 at gmail.com
Wed May 1 18:50:19 CEST 2013


On Tue, Apr 30, 2013 at 6:21 AM, Eugenio Pignataro <info at oscurart.com.ar> wrote:
> Revision: 4497
>           http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4497
> Author:   oscurart
> Date:     2013-04-29 20:21:43 +0000 (Mon, 29 Apr 2013)
> Log Message:
> -----------
> IO Groups rewritten!
>
> Modified Paths:
> --------------
>     contrib/py/scripts/addons/oscurart_tools/oscurart_meshes.py
>
> Modified: contrib/py/scripts/addons/oscurart_tools/oscurart_meshes.py
> ===================================================================
> --- contrib/py/scripts/addons/oscurart_tools/oscurart_meshes.py 2013-04-29 17:01:35 UTC (rev 4496)
> +++ contrib/py/scripts/addons/oscurart_tools/oscurart_meshes.py 2013-04-29 20:21:43 UTC (rev 4497)
> @@ -150,37 +150,11 @@
>      bl_options = {"REGISTER", "UNDO"}
>      def execute(self,context):
>
> -        OBSEL=bpy.context.active_object
> -        FILEPATH = bpy.data.filepath
> -
> -        with open(os.path.join(os.path.split(FILEPATH)[0],"%s.xml" % (OBSEL.name)), mode = "w") as FILE:
> -            VERTLIST = []
> -            LENVER = len(OBSEL.data.vertices)
> -            for VG in OBSEL.vertex_groups:
> -                BONELIST = []
> -                for VERTICE in range(0,LENVER):
> -                    try:
> -                        BONELIST.append((VERTICE,VG.weight(VERTICE),VG.name,))
> -                    except:
> -                        pass
> -                VERTLIST.append(BONELIST)
> -            NAMEGROUPLIST=[]
> -            for VG in OBSEL.vertex_groups:
> -                NAMEGROUPLIST.append(VG.name)
> -            VERTLIST.append(NAMEGROUPLIST)
> -            FILE.write(str(VERTLIST))
> +        with open(os.path.join(os.path.split(bpy.data.filepath)[0],"%s_vg" % (bpy.context.object.name)), "w") as FILE:
> +            WEIGHTLIST = [[group.group, vert.index, group.weight] for vert in bpy.context.object.data.vertices[:] for group in vert.groups[:]]
> +            WEIGHTLIST.append([group.name for group in bpy.context.object.vertex_groups])
> +            FILE.write(str(WEIGHTLIST))
>
> -        with open(os.path.join(os.path.split(FILEPATH)[0],"%s_DATA.xml" % (OBSEL.name)), mode = "w") as FILE:
> -            DATAVER = []
> -            for VERT in OBSEL.data.vertices[:]:
> -                LISTVGTEMP = []
> -                for VGTEMP, GROUP in enumerate(VERT.groups[:]):
> -                    LISTVGTEMP.append((GROUP.group,VGTEMP))
> -                LISTVGTEMP=sorted(LISTVGTEMP)
> -                for TEMP, GROUP in enumerate(VERT.groups[:]):
> -                    DATAVER.append((VERT.index,TEMP,VERT.groups[LISTVGTEMP[TEMP][1]].weight))
> -            FILE.write(str(DATAVER))
> -
>          return {'FINISHED'}
>
>  class OscImportVG (bpy.types.Operator):
> @@ -188,59 +162,15 @@
>      bl_label = "Import Groups"
>      bl_options = {"REGISTER", "UNDO"}
>      def execute(self,context):
> -
> -        OBSEL = bpy.context.active_object
> -        if os.sys.platform.count("win"):
> -            print("WINDOWS")
> -            BAR = "\\"
> -        else:
> -            print("LINUX")
> -            BAR = "/"
> -
> -        FILEPATH = bpy.data.filepath
> -        FILE = open(FILEPATH.rpartition(BAR)[0] + BAR + OBSEL.name + ".xml", mode="r")
> -        VERTLIST = FILE.readlines(0)
> -        VERTLIST = eval(VERTLIST[0])
> -        VERTLISTR = VERTLIST[:-1]
> -        GROUPLIST = VERTLIST[-1:]
> -        VGINDEX = 0
> -
> -
> -        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
> -
> -        for GROUP in GROUPLIST[0]:
> -            bpy.ops.object.vertex_group_add()
> -            OBSEL.vertex_groups[-1].name=GROUP
> -
> -
> -
> -        for VG in OBSEL.vertex_groups[:]:
> -            bpy.ops.object.vertex_group_set_active(group=VG.name)
> -            bpy.ops.object.mode_set(mode='EDIT')
> -            bpy.ops.mesh.select_all(action='DESELECT')
> -            bpy.ops.object.mode_set(mode='OBJECT')
> -            for VERTI in VERTLISTR[VG.index]:
> -                OBSEL.data.vertices[VERTI[0]].select=1
> -            bpy.context.tool_settings.vertex_group_weight=1
> -            bpy.ops.object.mode_set(mode='EDIT')
> -            bpy.ops.object.vertex_group_assign(new=False)
> -
> -        FILE.close()
> -
> -
> -        ## ----------- LEVANTO DATA ----
> -        # VARIABLES
> -        FILEPATH = bpy.data.filepath
> -        FILE = open(FILEPATH.rpartition(BAR)[0]+BAR+OBSEL.name+"_DATA.xml", mode="r")
> -        DATAPVER = FILE.readlines(0)
> -        DATAPVER = eval(DATAPVER[0])
> -
> -        bpy.ops.object.mode_set(mode='OBJECT')
> -        for VERT in DATAPVER:
> -            OBSEL.data.vertices[VERT[0]].groups[VERT[1]].weight = VERT[2]
> -        FILE.close()
> -        # PASO A MODO PINTURA DE PESO
> -        bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
> +
> +        with open(os.path.join(os.path.split(bpy.data.filepath)[0],"%s_vg" % (bpy.context.object.name)), "r") as FILE:
> +            WEIGHTLIST = eval(FILE.read())
> +            for group in WEIGHTLIST[-1]:
> +                bpy.context.object.vertex_groups.new(name=group)
> +            for ind ,(gr, index, weight) in enumerate(WEIGHTLIST[:-1]):
> +                print(ind, gr, index, weight)
> +                bpy.context.object.vertex_groups[gr].add(index=(index,index),weight=weight, type="REPLACE")
> +
>          return {'FINISHED'}
>

Note,
os.path.split(bpy.data.filepath)[0]
...can be replaced with os.path.dirname

WEIGHTLIST.append([group.name for group in bpy.context.object.vertex_groups])
... can be replaced with
WEIGHTLIST.append(bpy.context.object.vertex_groups.keys())

also suggest assigning bpy.context.object to a variable if accessed
multiple times, especially within a loop.


More information about the Bf-extensions-cvs mailing list