Exemplo n.º 1
0
def isosurf(context):
    global tmframe
    scn = bpy.context.scene

    stime = time.clock()
    global a

    SurfList = []
    i = 0
    for object in bpy.context.scene.objects:
        if 'IsoSurfer' in object:
            obsurf = object
            mesurf = object.data
            res = object.IsoSurf_res
            preview = object.IsoSurf_preview
            SurfList.append([(obsurf, mesurf, res, preview)])
            for item in object.IsoSurf:
                if item.active == True:
                    if item.obj != '':
                        if item.psys != '':
                            SurfList[i].append((item.obj, item.psys, item.sizem))
            i += 1

    for surfobj in SurfList:
        print("Start calculation of isosurface...frame:", bpy.context.scene.frame_current)
        # print(bpy.context)
        # print(bpy.context.screen)
        # print(bpy.context.window)
        # print(bpy.context.area)
        # print(surfobj)
        obsurf, mesurf, res, preview = surfobj[0]
        # print(obsurf,mesurf)
        # print(surfobj[1][0])
        ploc = []
        psize = []
        pprop = []
        stime = time.clock()
        xmin = 10000000000
        xmax = -10000000000
        ymin = 10000000000
        ymax = -10000000000
        zmin = 10000000000
        zmax = -10000000000
        for obj, psys, sizem in surfobj[1:]:
            # print(obj,psys,res,sizem)
            psys = bpy.data.objects[obj].particle_systems[psys]
            # print(psys)
            if 'IsoLocalUV' not in psys.settings:
                print("  WARNING: no IsoLocalUV prop found")
                psys.settings['IsoLocalUV'] = [0, 0, 0]
            psysize = len(psys.particles)
            if len(psys.settings['IsoLocalUV']) != (psysize * 3):
                print("  WARNING: not same numbers of props found")
                psys.settings['IsoLocalUV'] = [0] * psysize * 3
            for par in range(psysize):
                if psys.particles[par].alive_state == 'ALIVE':
                    size = psys.particles[par].size * sizem
                    ploc.append(psys.particles[par].location)
                    uvx = psys.settings['IsoLocalUV'][par * 3]  # psys.particles[par].location.x -
                    uvy = psys.settings['IsoLocalUV'][par * 3 + 1]  # psys.particles[par].location.y -
                    uvz = psys.settings['IsoLocalUV'][par * 3 + 2]  # psys.particles[par].location.z -
                    pprop.append(psys.particles[par].velocity.to_tuple() + (uvx, uvy, uvz, psys.particles[par].velocity.magnitude))
                    psize.append(size)

        if len(psize) > 0:

            isolevel = 0.0
            print('  pack particles:', time.clock() - stime, 'sec')

            a, b = mciso.isosurface(res, isolevel, ploc, psize, pprop)
            print('  mciso:', time.clock() - stime, 'sec')
            stime = time.clock()

            # if "myMesh" in bpy.data.meshes:
            #mesurf = bpy.data.meshes['myMesh']
            # else:
            #mesurf = bpy.data.meshes.new('myMesh')

            # if "myObject" in bpy.data.objects:
            #obsurf = bpy.data.objects['myObject']
            # else:
            #obsurf = bpy.data.objects.new('myObject', mesurf)
            # bpy.context.scene.objects.link(obsurf)

            #obsurf.show_name = True

            bm = bmesh.new()
            #print("   new bmesh")
            bm.from_mesh(mesurf)
            #print("   from_mesh bmesh")
            bm.clear()
            #print("   clear bmesh")
            sh = bm.verts.layers.shape.new("IsoSurf_mb")
            #print("   shape bmesh")
            p1 = bm.loops.layers.uv.new("IsoSurf_prop1")
            #print("   uv1 bmesh")
            p2 = bm.loops.layers.uv.new("IsoSurf_prop2")
            #print("   uv2 bmesh")
            lprop = 3 * 7
            for i in range(int(len(a) / 9)):

                vertex1 = bm.verts.new((a[i * 9], a[i * 9 + 1], a[i * 9 + 2]))
                vertex2 = bm.verts.new((a[i * 9 + 3], a[i * 9 + 4], a[i * 9 + 5]))
                vertex3 = bm.verts.new((a[i * 9 + 6], a[i * 9 + 7], a[i * 9 + 8]))

                vertex1[sh] = (a[i * 9] + b[i * lprop], a[i * 9 + 1] + b[i * lprop + 1], a[i * 9 + 2] + b[i * lprop + 2])
                vertex2[sh] = (a[i * 9 + 3] + b[i * lprop + 7], a[i * 9 + 4] + b[i * lprop + 8], a[i * 9 + 5] + b[i * lprop + 9])
                vertex3[sh] = (a[i * 9 + 6] + b[i * lprop + 14], a[i * 9 + 7] + b[i * lprop + 15], a[i * 9 + 8] + b[i * lprop + 16])

                # TEST---
                #vertex1[sh] = (a[i*9]-b[i*lprop+3],a[i*9+1]-b[i*lprop+4],a[i*9+2]-b[i*lprop+5])
                #vertex2[sh] = (a[i*9+3]-b[i*lprop+9], a[i*9+4]-b[i*lprop+10],a[i*9+5]-b[i*lprop+11])
                #vertex3[sh] = (a[i*9+6]-b[i*lprop+15],a[i*9+7]-b[i*lprop+16],a[i*9+8]-b[i*lprop+17])

                #vertex1[sh] = (b[i*lprop+3],b[i*lprop+4],b[i*lprop+5])
                #vertex2[sh] = (b[i*lprop+9], b[i*lprop+10],b[i*lprop+11])
                #vertex3[sh] = (b[i*lprop+15],b[i*lprop+16],b[i*lprop+17])
                #---TEST
                bm.faces.new((vertex1, vertex2, vertex3)).smooth = True

            # print(preview)
            # bm.verts.ensure_lookup_table()
            #print("   verts lookuptable bmesh")
            bm.faces.ensure_lookup_table()
            #print("   face lookuptable bmesh")
            for i in range(int(len(a) / 9)):
                bm.faces[i].loops[0][p1].uv = (b[i * lprop + 3], b[i * lprop + 4])
                bm.faces[i].loops[0][p2].uv = (b[i * lprop + 5], b[i * lprop + 6])
                bm.faces[i].loops[1][p1].uv = (b[i * lprop + 10], b[i * lprop + 11])
                bm.faces[i].loops[1][p2].uv = (b[i * lprop + 12], b[i * lprop + 13])
                bm.faces[i].loops[2][p1].uv = (b[i * lprop + 17], b[i * lprop + 18])
                bm.faces[i].loops[2][p2].uv = (b[i * lprop + 19], b[i * lprop + 20])
                #bm.faces[i].loops[0][p1].uv = (a[i*9]-b[i*lprop+3],a[i*9+1]-b[i*lprop+4])
                #bm.faces[i].loops[0][p2].uv = (a[i*9+2]-b[i*lprop+5],0.0)
                #bm.faces[i].loops[1][p1].uv = (a[i*9+3]-b[i*lprop+9], a[i*9+4]-b[i*lprop+10])
                #bm.faces[i].loops[1][p2].uv = (a[i*9+5]-b[i*lprop+11], 0.0)
                #bm.faces[i].loops[2][p1].uv = (a[i*9+6]-b[i*lprop+15],a[i*9+7]-b[i*lprop+16])
                #bm.faces[i].loops[2][p2].uv = (a[i*9+8]-b[i*lprop+17],0.0)
            #print("   uv loop bmesh")
            if preview == False:
                # print(preview)
                bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=res / 100)
            #print("   remove doubles bmesh")
            bm.to_mesh(mesurf)
            #print("   to_mesh bmesh")
            bm.free()
            #print("   free bmesh")
            scn.update()
            #print("   scene update")
            mesurf.shape_keys.animation_data_clear()
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].slider_min = -1.0
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].value = 1 / scn.render.fps
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].keyframe_insert("value", frame=scn.frame_current + 1)
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].value = -1 / scn.render.fps
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].keyframe_insert("value", frame=scn.frame_current - 1)
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].value = 0.0
            mesurf.shape_keys.key_blocks['IsoSurf_mb'].keyframe_insert("value", frame=scn.frame_current)
            scn.update()

            print('  Bmesh:', time.clock() - stime, 'sec')
Exemplo n.º 2
0
def isosurf(context):
    global tmframe
    scn = bpy.context.scene
    

    
    stime = time.clock()
    global a
    
    SurfList = []
    i = 0
    for object in bpy.context.scene.objects:
        if 'IsoSurfer' in object:
            obsurf = object
            mesurf = object.data
            res = object.IsoSurf_res
            preview = object.IsoSurf_preview
            SurfList.append([(obsurf,mesurf,res,preview)])
            for item in object.IsoSurf:
                if item.active == True:
                    if item.obj != '':
                        if item.psys != '':
                            SurfList[i].append((item.obj,item.psys,item.sizem))
            i += 1
                            
    for surfobj in SurfList:
        print("Start calculation of isosurface...frame:",bpy.context.scene.frame_current)
        #print(surfobj)
        obsurf,mesurf,res,preview = surfobj[0]
        #print(obsurf,mesurf)
        #print(surfobj[1][0])
        ploc = []
        psize = []
        stime = time.clock()
        xmin = 10000000000
        xmax = -10000000000
        ymin = 10000000000
        ymax = -10000000000
        zmin = 10000000000
        zmax = -10000000000
        for obj,psys,sizem in surfobj[1:]:
            #print(obj,psys,res,sizem)
            psys = bpy.data.objects[obj].particle_systems[psys].particles
            #print(psys)
            psysize = len(psys)
            for par in range(psysize):
                if psys[par].alive_state == 'ALIVE':
                    size = psys[par].size * sizem
                    ploc.append(psys[par].location)
                    psize.append(size)
                    
        if len(psize) > 0:
        
            isolevel=0.0
            print('  pack particles:',time.clock() - stime,'sec')
            
            a = mciso.isosurface(res,isolevel,ploc,psize)

            print('  mciso:',time.clock() - stime,'sec')
            stime = time.clock()
            
            #if "myMesh" in bpy.data.meshes:
                #mesurf = bpy.data.meshes['myMesh']
            #else:
                #mesurf = bpy.data.meshes.new('myMesh')

            #if "myObject" in bpy.data.objects:
                #obsurf = bpy.data.objects['myObject']
            #else:
                #obsurf = bpy.data.objects.new('myObject', mesurf)
                #bpy.context.scene.objects.link(obsurf)


            #obsurf.show_name = True

            bm = bmesh.new()

            bm.from_mesh(mesurf)
            bm.clear()
            for i in range(int(len(a)/9)):

                vertex1 = bm.verts.new( (a[i*9], a[i*9+1], a[i*9+2]) )
                vertex2 = bm.verts.new( (a[i*9+3], a[i*9+4], a[i*9+5]) )
                vertex3 = bm.verts.new( (a[i*9+6], a[i*9+7], a[i*9+8]) )

                bm.faces.new( (vertex1, vertex2, vertex3) ).smooth = True
            #print(preview)
            if preview == False:
                #print(preview)
                bmesh.ops.remove_doubles(bm,verts=bm.verts,dist=res/100)

            bm.to_mesh(mesurf)
            scn.update()
            bm.free()
            print('  Bmesh:',time.clock() - stime,'sec')