# This script checks if an object exists and if it does, edits its mesh data. # Useful for animating multi-channel data. # This script automaticallys every time the frame is changed. However, must # first manually run with ALT+P, and again if any changes are made. import numpy import bpy import math import os def animatemeshes(scene): filebase = 'M33AndMilkyWay_5' # Get the current frame frame = bpy.context.scene.frame_current filename = filebase+'_'+str(frame).zfill(3)+'.txt' # Check if the file exists checkfile = str(os.path.isfile(filename)) # Check if the object exists if 'Surface' in bpy.data.objects: checkob = 'True' obj = bpy.data.objects.get('Surface') elif 'Surface' not in bpy.data.objects: checkob = 'False' if (checkfile == 'True') and (checkob == 'True'): # Read in the data from the correct file data = numpy.genfromtxt(filename) sizex = int(numpy.max(data[:,0]))+1 sizey = int(numpy.max(data[:,1]))+1 # Get the minimum flux minf = abs(numpy.nanmin(data[:,3])) print(minf) # Prevent extreme values causing thing to go bad. Square root has # lower contrast at higher values so severely negative values can # cause the noise to be appear highly suppressed. if minf >= 0.05: minf = 0.05 # Correct the flux values all at once (faster and makes them easier to # work with later) data[:,3] = 200.0*numpy.sqrt(data[:,3]+minf) # Get the median level of the surface so we can keep all the planes at the # same level #medflux = 400.0*math.sqrt(numpy.median(data[:,3])+minf+1.0) medflux = numpy.nanmedian(data[:,3]) # Get the mesh data mesh = obj.data for i in range(0,data.shape[0]): xp = data[i,0] yp = data[i,1] flux = data[i,3] mesh.vertices[i].co.x = xp mesh.vertices[i].co.y = yp mesh.vertices[i].co.z = flux - medflux # Run the script every time the frame has changed bpy.app.handlers.frame_change_pre.append(animatemeshes)