#!BPY """ Name: 'Refractor' Blender: '2.42' Group: 'Materials' Tooltip: 'IOR Presets' """ __author__ = "forTe" __url__ = ("blender", "blenderartists") __version__ = "1.0" __bpydoc__ = """\ This script has stored IOR values and can set them to a material. """ # -------------------------------------------------------------------------- # Index of Refraction Reference Script # -------------------------------------------------------------------------- # ***** BEGIN GPL LICENSE BLOCK ***** # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ***** END GPL LICENCE BLOCK ***** # -------------------------------------------------------------------------- # #This Script was published on July 23, 2006 import Blender from Blender import* import webbrowser #GUI routines def event(evt,val): global id, matname, material, idval if evt == Draw.ESCKEY or evt == Draw.QKEY: Draw.Exit() return if evt == Draw.RIGHTMOUSE: men = Draw.PupMenu("IOR Script%t|Apply IOR%x1|Quit%x2|More IOR Resources (Opens in Browser)%x3|") if men == 1: IOR_change(material,matname) Window.RedrawAll() if men == 2: Draw.Exit() return if men == 3: men_1=Draw.PupMenu("IOR Resources%t|3d-designer.dk%x1|is.kiruna.se%x2|") if men_1 == 1: webbrowser.open("http://www.3d-designer.dk/resource/ior.html") if men_1 == 2: webbrowser.open("http://www.is.kiruna.se/~cjo/d2i/REFRACTION.INDEX3.html") #The tasks below can certainly be optimized. I Have not had time to do so yet, though. def button_event(evt): global id, matname, material,idval if evt == 1: if idval.val == 1: id = acetone elif idval.val == 2: id = alcohol elif idval.val == 3: id = bromine elif idval.val == 4: id = cinnabar elif idval.val == 5: id = CO2_liquid elif idval.val == 6: id = crown_glass elif idval.val == 7: id = cryolite elif idval.val == 8: id = cubic_zirc elif idval.val == 9: id = diamond elif idval.val == 10: id = ethanol elif idval.val == 11: id = fus_quartz elif idval.val == 12: id = glass_thin elif idval.val == 13: id = glass_medium elif idval.val == 14: id = glass_thick elif idval.val == 15: id = glycerine elif idval.val == 16: id = moissanite elif idval.val == 17: id = polycarb elif idval.val == 18: id = rock_salt elif idval.val == 19: id = ruby_saph elif idval.val == 20: id = salt elif idval.val == 21: id = topaz elif idval.val == 22: id = vacuum elif idval.val == 23: id = water_liquid elif idval.val == 24: id = water_ice elif evt == 100: time1 = sys.time() IOR_change(material, id) Window.RedrawAll() time2 = sys.time() timetotal = time2 - time1 print "Changed in: %f seconds" %timetotal elif evt == 101: material = Material.Get(matname.val) def gui(): #Define Globals global id, matname, material, idval BGL.glClearColor(0.8, 0.8, 0.8, 1.0) BGL.glClear(BGL.GL_COLOR_BUFFER_BIT) BGL.glColor3f(0.0, 0.0, 1.0) BGL.glRasterPos2i(20, 450) Draw.Text("Refractor v1.0", "large") BGL.glColor3f(1.0,1.0,1.0) BGL.glRasterPos2i(20,435) Draw.Text("Created by forTe", "tiny") BGL.glColor3f(0.0, 0.0, 0.0) BGL.glRasterPos2i(20, 410) Draw.Text("Material To Set Refraction Value On:", "small") matname = Draw.String("Mat: ", 101, 20, 380, 200, 20, "", 350, "Enter the name of the material receiving change in IOR") BGL.glRasterPos2i(20, 360) Draw.Text("ID Number from Table Below", "small") idval = Draw.Number("", 1, 20, 330, 50, 20, 1, 1, 24, "Enter The Number of the ID to set") #def draw_columns(xi, yi, maxcolnum, size, ysize, yinc, xinc, numoffx, names): draw_columns(35, 295, 2, "small", 12, 15, 75, 15, namelist) Draw.PushButton("Change IOR", 100, 90, 100, 75, 20, "Click to Change IOR") # ------------------------------------------------------------------------- # IOR Ids # # IOR Cannot be larger than 3.000 # ------------------------------------------------------------------------- vacuum = 1.000 water_ice = 1.310 water_liquid= 1.333 cryolite = 1.338 ethanol = 1.360 glycerine = 1.4729 rock_salt = 1.516 glass_thin = 1.500 glass_medium= 1.700 glass_thick = 1.900 crown_glass = 1.520 salt = 1.544 polycarb = 1.590 bromine = 1.661 cubic_zirc = 2.170 diamond = 2.419 moissanite = 2.670 cinnabar = 3.020 alcohol = 1.329 acetone = 1.360 fus_quartz = 1.460 CO2_liquid = 1.200 ruby_saph = 1.770 topaz = 1.610 namelist = ['Acetone', 'Alcohol', 'Bromine', 'Cinnabar', 'Carbon Dioxide - Liquid', 'Crown Glass', 'Cryolite', 'Cubic Zirconium', 'Diamond', 'Ethanol', 'Fused Quartz', 'Glass - Thin', 'Glass - Medium', 'Glass - Thick', 'Glycerine', 'Moissanite', 'Polycarbonate', 'Rock Salt', 'Ruby & Sapphire', 'Salt', 'Topaz', 'Vacuum', 'Water - Liquid', 'Water - Ice'] """ Function: IOR_change(mat,id) Use: Changes the IOR value Inputs: mat: material to be changed id: name of substance Variables: orig:IOR previous to change id_a: Used for comparisons id_b: Used for comparisons new: IOR after change Theory: Raytransparency must be enabled for IOR to work at all, so it is turned on first. The script then compares the existing IOR to the new selected IOR and determines whether to make a change. If a change is needed, then the script changes the IOR. To ensure a proper change, a confirmation message is sent. Problems: -Blender returns an unclipped value for the IOR. As such, a true comparison is not possible. A range for the comparison had to be set, hence id_a and id_b. -Could Probably be optimized """ def IOR_change(mat,id): #Ensures RayTransp is enabled mat.mode |= Material.Modes.RAYTRANSP #Gets original IOR orig = mat.getIOR() #Clips IOR to 3.000 and ensures its not less than 1 if id > 3.0000: id = 3.000 if id < 1.0000: id = 1.000 #Sees if IOR values are equal id_a = id + .0001 id_b = id - .0001 if orig <= id_a and orig >= id_b: Draw.PupMenu("The two IOR values are equal. No Action Taken.") return #Changes the IOR value else: #Changes IOR mat.setIOR(id) #Check for change new = mat.getIOR() if new <= id_a and new >= id_b: Draw.PupMenu("Change Successful.") return else: Draw.PupMenu("An Error Occurred and the IOR was changed improperly.") return """ Function: draw_columns(xi, yi, maxcolnum, size, ysize, yinc, xinc, numoffx, names) Use: Draws the two columns of names and numbers automatically. Inputs: xi: X Coordinate of the first name yi: Y Coordinate of the first name maxcolnum: Sets maximum column number size: Sets Text Size ysize: Sets the number of names to print in a column yinc: Sets the differenece between yi and the position to draw xinc: Sets the Offset from the original column to draw the second column numoffx: Sets the distance to draw the numbers (in the -x direction) from the names names: List of IOR Names Variables: c: Used for Column Iteration cut: Used to tell where to begin the second column position, num: Tell when to stop drawing one column enum, enum_2: Used to create enumerated lists for the numbers Theory: Draws Two columns based off of position. It automates a task which can be very tedious. Problems: -Not very robust code. It breaks very easily, so any reuse is not reccomended. Is very micromanaged. -Needs a lot of optimization and other work to become more stable and robust. REUSE OF THIS CODE IN OTHER SCRIPTS: It is not recommended that this function be implemented in other code until it is more stable and optimized. As this is being release under the GPL license, reuse cannot legally be prevented. However, if you do optimize this code, please release it and send me the site of the script so I can optimize this code below. """ def draw_columns(xi, yi, maxcolnum, size, ysize, yinc, xinc, numoffx, names): c = 1 cut = 0 position = num = 0 enum = range(1, len(names)+1) while c <= maxcolnum: if c == 1: for n in names: #Draws The Name BGL.glRasterPos2i(xi, yi - (names.index(n) * yinc)) Draw.Text(n, size) #Draws The Number BGL.glRasterPos2i(xi - numoffx, yi - (names.index(n) * yinc)) Draw.Text(str(enum[num])+'.', size) num += 1 if num == ysize: cut = names.index(n) c += 1 break if c == 2: BGL.glRasterPos2i(xi + (c * xinc), yi) for ni in names[cut + 1:]: enum_2 = range(((len(names))/2)+1, len(names)+1) #Draws The Name BGL.glRasterPos2i(xi + (c * xinc), yi - (position * yinc)) Draw.Text(ni, size) #Draws The Number BGL.glRasterPos2i((xi + (c * xinc)) - numoffx, yi - (position * yinc)) Draw.Text(str(enum_2[position])+'.', size) position += 1 if position == ysize: cut = names.index(ni) c += 1 break return Draw.Register(gui, event, button_event)