def name(self,name): deselect_all() location_old = [0,0,0] location_old[0] = bpy.data.objects['CenterOf:'+self.__name].location[0] location_old[1] = bpy.data.objects['CenterOf:'+self.__name].location[1] location_old[2] = bpy.data.objects['CenterOf:'+self.__name].location[2] pointing_old = [0,0,0] pointing_old[0] = self.pointing[0] pointing_old[1] = self.pointing[1] pointing_old[2] = self.pointing[2] bpy.context.scene.objects.active = bpy.data.objects[self.__name] bpy.ops.object.mode_set(mode='EDIT') # toggle to edit mode bpy.ops.font.delete() # delete what is there bpy.ops.font.text_insert(text=name) # insert new text bpy.ops.object.mode_set(mode='OBJECT') # toggle to edit object mode # now, recenter over center bpy.data.objects['CenterOf:'+self.__name].location = (0,0,0) bpy.data.objects['Empty'+self.__name].location = (0,0,5) txtsize = bpy.data.objects[self.__name].dimensions # now, recenter this thing bpy.data.objects[self.__name].location = (-0.5*txtsize[0], -0.5*txtsize[1],-0.5*txtsize[2]) bpy.data.objects['CenterOf:'+self.__name].location = location_old bpy.data.objects['Empty'+self.__name].location = pointing_old for mat in bpy.data.materials: #if (mat.name.find(self.__name) != -1): if mat.name == self.__name: mat.name = name # change materials name bpy.data.objects['Empty'+self.__name].name = 'Empty' + name bpy.data.objects['CenterOf:'+self.__name].name = 'CenterOf:' + name bpy.data.objects[self.__name].name = name # object name self.__name = name
def _import_ytsurface(vertices, colors, alpha, emisses, colorindex, meshname='Steve', meshlocation=(0,0,0), plot_index=0): import numpy as np deselect_all() nftype = [("f1","int"),("f2","int"),("f3","int"),("f4","int")] newfaces = np.empty(int(len(vertices)/3.), dtype=nftype) # store sets of face colors cc = 0 for i in range(0,int(len(vertices)/3.)): newfaces[i] = (cc, cc+1, cc+2, cc) # repeat last for triangles cc = cc+3 me = bpy.data.meshes.new(meshname+"Mesh") ob = bpy.data.objects.new(meshname,me) ob.location = meshlocation # position object at 3d-cursor bpy.context.scene.objects.link(ob) # Link object to scene # Fill the mesh with verts, edges, faces me.from_pydata(vertices.tolist(),[],newfaces.tolist()) me.update(calc_edges=True) # Update mesh with new data # materials woop woop! obj = bpy.data.objects[meshname] bpy.context.scene.objects.active=obj for i in range(0,len(colors[0])): mat = makeMaterial(meshname+str(i)+'_'+str(plot_index), (colors[0][i],colors[1][i],colors[2][i]), (1,1,1), alpha, emisses[i]) setMaterial(obj,mat) # now, do individual mat indecies for i in range(0,len(newfaces)): me.polygons[i].material_index = colorindex[i]
def __init__(self, name="NoNameSphere", color = (1,1,1), scale=(1,1,1), segments = 32, shadeless = True): deselect_all() bpy.ops.mesh.primitive_uv_sphere_add(segments=segments, size=1.0) sph = bpy.data.objects['Sphere'] self.__name = 'Sphere' self.name = name self.color = color self.shadeless = shadeless self.scale = scale self.location = (0,0,0)
def __init__(self, name = "NoName", color=(1.,1,1), scale=(1.,1,1), shadeless=True): newtxt = name xyzloc = (0,0,0) self.__name = 'Text' emptyname = 'Empty' + self.__name # what to call empty for facing emptycenter = 'CenterOf:' + self.__name # where the actual tracking will be # add a new empty in the center of this text bpy.ops.object.empty_add(type='SPHERE') # add object to track to esphc = bpy.data.objects['Empty'] esphc.name = emptycenter esphc.location = [xyzloc[0], xyzloc[1], xyzloc[2]] # give it an offset bpy.ops.object.empty_add(type='SPHERE') # add object to track to esph = bpy.data.objects['Empty'] esph.name = emptyname esph.location = [xyzloc[0], xyzloc[1], xyzloc[2]+5] # give it an offset # now, fix this this empty to the middle of the text deselect_all() bpy.ops.object.text_add(location=xyzloc) # make text bpy.ops.object.mode_set(mode='EDIT') # toggle to edit mode bpy.ops.font.delete() # delete what is there bpy.ops.font.text_insert(text=newtxt) # insert new text bpy.ops.object.mode_set(mode='OBJECT') # toggle to edit object mode self.name = name emptyname = 'Empty' + self.name # what to call empty for facing emptycenter = 'CenterOf:' + self.name # where the actual tracking will be bpy.data.objects[self.name].scale = scale # scale new txt object txtsize = bpy.data.objects[self.name].dimensions # now, recenter this thing bpy.data.objects[self.name].location = (xyzloc[0] - txtsize[0]*0.5, xyzloc[0] - txtsize[1]*0.5, xyzloc[0] - txtsize[2]*0.5) tobj = bpy.data.objects[self.name] bpy.context.scene.objects.active = tobj # this is a key step to "highlight" esphc tobj.select = True bpy.ops.object.constraint_add(type='CHILD_OF') bpy.data.objects[self.name].constraints["Child Of"].target = esphc # now, link new text Center to an empty that we can move around esphc.rotation_mode = 'QUATERNION' # mo betta then 'XYZ' deselect_all() bpy.context.scene.objects.active = esphc # this is a key step to "highlight" esphc # auto track to the new empty esphc.select = True bpy.ops.object.constraint_add(type='TRACK_TO') bpy.data.objects[emptycenter].constraints["Track To"].target = esph # now, set correct coords esphc.constraints["Track To"].track_axis = 'TRACK_Z' esphc.constraints["Track To"].up_axis = 'UP_Y' esphc.hide = True # hide this from the viewport self.color = color self.location = (0,0,0) self.pointing = (0,0,5) self.shadeless = shadeless self.scale = scale
def __init__(self, name="NoNameArrow", color = (1,1,1), scale=(0.5,0.5,0.5), shadeless = True, tip_scale = 2.0, base_scale = 1.0): deselect_all() self.__name = 'Cylinder' # create arrow # now, make ability to point emptyname = 'Empty' + self.__name # what to call empty xyzloc = (0,0,0) bpy.ops.object.empty_add(type='SPHERE') # add object to track to esph = bpy.data.objects['Empty'] esph.name = emptyname esph.location = [xyzloc[0], xyzloc[1], xyzloc[2]+5] # give it an offset bpy.ops.mesh.primitive_cone_add() bpy.ops.mesh.primitive_cylinder_add() cone = bpy.data.objects['Cone'] arrow = bpy.data.objects['Cylinder'] cone.location = (0,0,base_scale*scale[2]+scale[2]*tip_scale) arrow.location = (0,0,0) arrow.scale = (scale[0]*base_scale, scale[1]*base_scale, scale[2]*base_scale) cone.scale = (tip_scale*scale[0], tip_scale*scale[1], tip_scale*scale[2]) join_surfaces(['Cylinder','Cone']) arrow.rotation_mode = 'QUATERNION' # mo betta then 'XYZ' self.name = name # auto track to the new empty deselect_all() bpy.context.scene.objects.active = bpy.data.objects[self.name] # this is a key step to "highlight" the arrow # auto track to the new empty bpy.data.objects[self.name].select = True bpy.ops.object.constraint_add(type='TRACK_TO') bpy.data.objects[self.name].constraints["Track To"].target = esph # now, set correct coords ddir = 'TRACK_Z' updir = 'UP_X' bpy.data.objects[self.name].constraints["Track To"].track_axis = ddir bpy.data.objects[self.name].constraints["Track To"].up_axis = updir # now, set color self.color = color self.shadeless = shadeless self.scale = scale self.pointing = (0,0,5) self.location = (0,0,0)
def import_ytsph(filename, halo_sizes, color_field, color_map, color_log, n_ref): from yt import load import numpy as np import bmesh from scienceutils import deselect_all from scienceutils import makeMaterial, setMaterial ds = load(filename) # strip "/" for naming xnn2 = 0 xnn = 0 # you only want the file name, not all the directory stuff while xnn != -1: xnn2 = xnn xnn = filename.find('/',xnn+1) fname_out = filename[xnn2+1:] # get coords and color data dd = ds.all_data() #xcoord = dd['Gas','Coordinates'][:,0].v #ycoord = dd['Gas','Coordinates'][:,1].v #zcoord = dd['Gas','Coordinates'][:,2].v xcoord = (dd['Gas','Coordinates'][:,0].in_units('unitary')).v ycoord = (dd['Gas','Coordinates'][:,1].in_units('unitary')).v zcoord = (dd['Gas','Coordinates'][:,2].in_units('unitary')).v cs = dd[color_field] print('cs') print(len(cs)) # map colorcode to 256 material colors if color_log: cs = np.log10(cs) mi, ma = cs.min(), cs.max() cs = (cs - mi) / (ma - mi) from yt.visualization._colormap_data import color_map_luts # import colors # the rgb colors colors = color_map_luts[color_map] x = np.mgrid[0.0:1.0:colors[0].shape[0]*1j] # how the values map to the colors color_index = (np.interp(cs,x,x)*(colors[0].shape[0]-1)).astype("uint8") color_index_list = color_index.tolist() name = [] hused = [] scale = [(1.0, 1.0, 1.0)] # create scale if necessary if len(scale[:][:]) < color_index.max(): # generate larger scale sc = (scale[0][0], scale[0][1], scale[0][2]) scale = [] for i in range(color_index.min(), color_index.max()): scale.append(sc) # also, allow for multiple halo sizes if len(halo_sizes) < color_index.max(): hs = halo_sizes[0] halo_sizes = [] for i in range(color_index.min(), color_index.max()): halo_sizes.append(hs) # create sph data into meshes and color them for ind in range(color_index.min(), color_index.max()): cl = [i for i,j in enumerate(color_index_list) if j == ind] print(ind) if len(cl) > 0: #print("particle name") #print(name) fname = fname_out + '_' + str(ind) name.append('particle_'+fname) hused.append(halo_sizes[ind]) me = bpy.data.meshes.new('particleMesh_'+fname) ob = bpy.data.objects.new('particle_'+fname,me) ob.location = (0,0,0) bpy.context.scene.objects.link(ob) # Link object to scene coords = [(0,0,0)] me.from_pydata(coords,[],[]) ob.location = (0,0,0) ob = bpy.data.objects['particle_'+fname] # select right object deselect_all() ob.select = True bpy.context.scene.objects.active=ob mat = makeMaterial('particle_'+fname, (colors[0][ind],colors[1][ind],colors[2][ind]), (1,1,1), 1.0, 1.0, mat_type = 'HALO', halo_size=halo_sizes[ind]) setMaterial(ob,mat) # add in verts bpy.ops.object.mode_set(mode='EDIT') # toggle to edit mode bm = bmesh.from_edit_mesh(ob.data) # now, find all verts with this color index (ind) # move original vertex to actual location if hasattr(bm.verts, "ensure_lookup_table"): # to make it work with 2.73 bm.verts.ensure_lookup_table() bm.verts[0].co = (xcoord[cl[0]]*scale[ind][0],ycoord[cl[0]]*scale[ind][1],zcoord[cl[0]]*scale[ind][2]) print('coords') print(xcoord[cl[0]], ycoord[cl[0]], zcoord[cl[0]]) print(len(cl)) print(len(xcoord)) for i in range(1,len(xcoord[cl])): bm.verts.new((xcoord[cl[i]]*scale[ind][0],ycoord[cl[i]]*scale[ind][1],zcoord[cl[i]]*scale[ind][2])) bmesh.update_edit_mesh(ob.data) bpy.ops.object.mode_set(mode='OBJECT') #print(ind) #print(bpy.data.objects['particle_' + fname].name) print(bpy.data.objects['particle_' + fname].location) #print("NAME OUT HERE") #print(name) return name, ds, hused
def new_particle_set(self, dfile, particle_num=1, colors=None, halo_sizes=None, scale=(1, 1, 1)): # initial file, number of particles, colors array import bmesh deselect_all() location = (0, 0, 0) # strip "/" and ".txt"s for names xnn2 = 0 xnn = 0 # you only want the file name, not all the directory stuff while xnn != -1: xnn2 = xnn xnn = dfile.find('/', xnn + 1) fname = dfile[xnn2 + 1:] # take out .txt stufftoo xnn = fname.find('.txt') fname = fname[0:xnn] pname = [] # create different particle types for p in range(0, particle_num): #print(p) num = "%02d" % (p) pname.append(fname + 'particle' + num) me = bpy.data.meshes.new(fname + 'particleMesh' + num) ob = bpy.data.objects.new(fname + 'particle' + num, me) ob.location = (0, 0, 0) bpy.context.scene.objects.link(ob) # Link object to scene coords = [(0, 0, 0)] me.from_pydata(coords, [], []) ob.location = (0, 0, 0) if colors is not None: color = colors[:][p] halo_size = halo_sizes[p] else: print('no color') color = (1, 1, 1) halo_size = 0.1 mat = makeMaterial(fname + 'particle' + num, color, (1, 1, 1), 1.0, 1.0, mat_type='HALO', halo_size=halo_size) setMaterial(ob, mat) # sets everything to material 0 by default ob = bpy.data.objects[fname + 'particle' + num] # select right object ob.select = True bpy.context.scene.objects.active = ob # now, add in different data types p2 = -1 # for toggling to object mode when using a different particle f = open(dfile, 'r') # open file for reading for i, line in enumerate(f): pint = int(p) #p = data["p"][i] # get type - o - particle linep = line.strip() # get rid of end line data = linep.split(",") # split into data based on ","'s p = float(data[4]) if p != p2: bpy.ops.object.mode_set(mode='OBJECT') # toggle to edit mode num = "%02d" % (p) deselect_all() ob = bpy.data.objects[fname + 'particle' + num] # select right object ob.select = True bpy.context.scene.objects.active = ob bpy.ops.object.mode_set(mode='EDIT') # toggle to edit mode bm = bmesh.from_edit_mesh(ob.data) if p != p2: # if first instance - move origional if hasattr(bm.verts, "ensure_lookup_table"): # to make it work with 2.73 bm.verts.ensure_lookup_table() bm.verts[0].co = (float(data[1]) * scale[pint][0], float(data[2]) * scale[pint][1], float(data[3]) * scale[pint][2]) p2 = p else: bm.verts.new((float(data[1]) * scale[pint][0], float(data[2]) * scale[pint][1], float(data[3]) * scale[pint][2])) f.close() # close up the file # update all meshes for p in range(0, particle_num): deselect_all() bpy.ops.object.mode_set(mode='OBJECT') # toggle to edit mode num = "%02d" % (p) ob = bpy.data.objects[fname + 'particle' + num] # select right object ob.select = True bpy.context.scene.objects.active = ob bpy.ops.object.mode_set(mode='EDIT') # toggle to edit mode bmesh.update_edit_mesh(ob.data) bpy.ops.object.mode_set(mode='OBJECT') # toggle to edit mode return pname
for i in range(1,ndens): ind = np.where( (cs < 1.0*i/(ndens-1.)) & (cs >= 1.0*(i-1)/(ndens-1.)) )[0] if len(ind) > 0: #print(ind) # ok, plot only things with this range of "densities" particle_name = particle_name_base + str(pn).zfill(3) # create sph data into meshes and color them me = bpy.data.meshes.new(particle_name) ob = bpy.data.objects.new(particle_name,me) ob.location = (0,0,0) bpy.context.scene.objects.link(ob) # Link object to scene coords = [(0,0,0)] me.from_pydata(coords,[],[]) ob.location = (0,0,0) ob = bpy.data.objects[particle_name] # select right object deselect_all() ob.select = True bpy.context.scene.objects.active=ob # I think we *don't* want to set a material #mat = makeMaterial(particle_name, (1.0, 1,1), # (1,1,1), 1.0, 1.0, mat_type = 'HALO', halo_size=0.0008) #setMaterial(ob,mat) # add in verts bpy.ops.object.mode_set(mode='EDIT') # toggle to edit mode bm = bmesh.from_edit_mesh(ob.data) # now, find all verts with this color index (ind) # move original vertex to actual location if hasattr(bm.verts, "ensure_lookup_table"): # to make it work with 2.73 bm.verts.ensure_lookup_table() # first coord bm.verts[0].co = (xcoord[ind[0]], ycoord[ind[0]],zcoord[ind[0]])