require 'sketchup.rb' def l2str(len) if len < 0.0 aux_str = "-" + (-len).inch.to_s.to_f.to_s else aux_str = len.inch.to_s.to_f.to_s end return aux_str end def export_individual_cameras # read SU variables model = Sketchup.active_model pages = model.pages # read model path and name model_path = File.dirname(model.path) model_filename = File.basename(model.path) model_name = model_filename.split(".")[0] # save the SU file if not already if model_name == NIL UI.messagebox("SU2Blender\n\nSave your model first") return nil end # export the SU model to .obj (or not) option = UI.messagebox("SU2Blender\n\n" + "Export your model to: [chose one option]\n " + " 1. " + model_name + ".obj, or\n" + " 2. " + model_name + ".3ds" , MB_YESNOCANCEL) if option == 2 UI.messagebox("SU2Blender\n\nExport aborted") return nil elsif option == 6 zzz = Sketchup.send_action(21149) puts zzz end # open file (python script) aux_file = File.new(model_name + "_cam.py", "w") # write python script head aux_file.puts("import Blender") aux_file.puts aux_file.puts("scene = Blender.Scene.getCurrent()") aux_file.puts("context = scene.getRenderingContext()") # create new page if none exist pages.add("NewPage_s2b") if pages.size == 0 # read view information view = model.active_view width = view.vpwidth height = view.vpheight v_ar = width.to_f/height.to_f # continue write script head aux_file.puts("context.imageSizeX(" + width.to_s + ")") aux_file.puts("context.imageSizeY(" + height.to_s + ")") aux_file.puts # for all pages for ind in (0..pages.size-1) # read camera for page camera = pages[ind].camera # read eye and target and fov of the camera eye = camera.eye vx = eye.x vy = eye.y vz = eye.z target = camera.target tx = target.x ty = target.y tz = target.z fov = camera.fov # calculate lens value c_ar = camera.aspect_ratio if (v_ar < 1.0) || ((c_ar != 0.0) && (c_ar != 1.0)) mult = 1.0 else mult = 1.0/v_ar end lens = (16.0*mult)/Math::tan(fov*Math::PI/360.0) # for each page create a new Blender camera (write in python script) aux_file.puts("cam" + (ind+1).to_s + " = Blender.Camera.New('persp', 'cam" + (ind+1).to_s + "')") aux_file.puts("cam" + (ind+1).to_s + ".setLens(" + lens.to_s + ")") aux_file.puts("objcam" + (ind+1).to_s + " = Blender.Object.New('Camera', '" + pages[ind].name + "')") aux_file.puts("objcam" + (ind+1).to_s + ".setLocation([" + l2str(vx) + ", " + l2str(vy) + ", " + l2str(vz) + "])") aux_file.puts("objcam" + (ind+1).to_s + ".link(cam" + (ind+1).to_s + ")") aux_file.puts("scene.link(objcam" + (ind+1).to_s + ")") aux_file.puts("empcam" + (ind+1).to_s + " = Blender.Object.New('Empty', '" + pages[ind].name + " (Empty)')") aux_file.puts("empcam" + (ind+1).to_s + ".setLocation([" + l2str(tx) + ", " + l2str(ty) + ", " + l2str(tz) + "])") aux_file.puts("scene.link(empcam" + (ind+1).to_s + ")") aux_file.puts("objcam" + (ind+1).to_s + ".makeTrack(empcam" + (ind+1).to_s + ")") aux_file.puts end # close file (python script) aux_file.close UI.messagebox("SU2Blender\n\nExport completed") end def export_animated_camera # read SU variables model = Sketchup.active_model pages = model.pages # read model path and name model_path = File.dirname(model.path) model_filename = File.basename(model.path) model_name = model_filename.split(".")[0] # save the SU file if not already if model_name == NIL UI.messagebox("SU2Blender\n\nSave your model first") return nil end # export the SU model to .obj (or not) option = UI.messagebox("SU2Blender\n\n" + "Export your model to: [chose one option]\n " + " 1. " + model_name + ".obj, or\n" + " 2. " + model_name + ".3ds" , MB_YESNOCANCEL) if option == 2 UI.messagebox("SU2Blender\n\nExport aborted") return nil elsif option == 6 zzz = Sketchup.send_action(21149) puts zzz end # open file (python script) aux_file = File.new(model_name + "_cam.py", "w") # write python script head aux_file.puts("import Blender") aux_file.puts aux_file.puts("scene = Blender.Scene.getCurrent()") aux_file.puts("context = scene.getRenderingContext()") # create new page if none exist pages.add("NewPage_ec2b") if pages.size == 0 # read view information view = model.active_view width = view.vpwidth height = view.vpheight v_ar = width.to_f/height.to_f # continue write script head aux_file.puts("context.imageSizeX(" + width.to_s + ")") aux_file.puts("context.imageSizeY(" + height.to_s + ")") aux_file.puts # create a only new Blender camera (write in python script) aux_file.puts("sucam = Blender.Camera.New('persp', 'sucam')") aux_file.puts("objsucam = Blender.Object.New('Camera', 'SU Anim Cam')") aux_file.puts("objsucam.link(sucam)") aux_file.puts("scene.link(objsucam)") aux_file.puts("empsucam = Blender.Object.New('Empty', 'SU Anim Cam (Empty)')") aux_file.puts("scene.link(empsucam)") aux_file.puts("objsucam.makeTrack(empsucam)") aux_file.puts # add a IPO curve to the lens value (write in python script) aux_file.puts("suipolens = Blender.Ipo.New('Camera', 'suipolens')") aux_file.puts("sucam.setIpo(suipolens)") aux_file.puts aux_file.puts("suipolens.addCurve('Lens')") aux_file.puts("suipol = suipolens.getCurve('Lens')") aux_file.puts("suipol.setInterpolation('Linear')") aux_file.puts # add a IPO curve to the location X, Y and Z values and rotation X, Y, and Z values (write in python script) aux_file.puts("suipoloc = Blender.Ipo.New('Object', 'suipoloc')") aux_file.puts("objsucam.setIpo(suipoloc)") aux_file.puts aux_file.puts("suiporot = Blender.Ipo.New('Object', 'suiporot')") aux_file.puts("empsucam.setIpo(suiporot)") aux_file.puts aux_file.puts("suipoloc.addCurve('LocX')") aux_file.puts("suipolx = suipoloc.getCurve('LocX')") aux_file.puts("suipolx.setInterpolation('Linear')") aux_file.puts("suipoloc.addCurve('LocY')") aux_file.puts("suipoly = suipoloc.getCurve('LocY')") aux_file.puts("suipoly.setInterpolation('Linear')") aux_file.puts("suipoloc.addCurve('LocZ')") aux_file.puts("suipolz = suipoloc.getCurve('LocZ')") aux_file.puts("suipolz.setInterpolation('Linear')") aux_file.puts aux_file.puts("suiporot.addCurve('LocX')") aux_file.puts("suiporx = suiporot.getCurve('LocX')") aux_file.puts("suiporx.setInterpolation('Linear')") aux_file.puts("suiporot.addCurve('LocY')") aux_file.puts("suipory = suiporot.getCurve('LocY')") aux_file.puts("suipory.setInterpolation('Linear')") aux_file.puts("suiporot.addCurve('LocZ')") aux_file.puts("suiporz = suiporot.getCurve('LocZ')") aux_file.puts("suiporz.setInterpolation('Linear')") aux_file.puts # for all pages for ind in (0..pages.size-1) # read camera for page camera = pages[ind].camera # read eye and target and fov of the camera eye = camera.eye vx = eye.x vy = eye.y vz = eye.z target = camera.target tx = target.x ty = target.y tz = target.z fov = camera.fov # calculate lens value c_ar = camera.aspect_ratio if (v_ar < 1.0) || ((c_ar != 0.0) && (c_ar != 1.0)) mult = 1.0 else mult = 1.0/v_ar end lens = (16.0*mult)/Math::tan(fov*Math::PI/360.0) # for each page add bezier points to the IPO curves, LocX, LocY, LocZ, RotX, RotY, RotZ and Lens (write in python script) aux_file.puts("suipol.addBezier((" + (ind+1).round.to_s + ", " + lens.to_s + "))") aux_file.puts("suipolx.addBezier((" + (ind+1).round.to_s + ", " + l2str(vx) + "))") aux_file.puts("suipoly.addBezier((" + (ind+1).round.to_s + ", " + l2str(vy) + "))") aux_file.puts("suipolz.addBezier((" + (ind+1).round.to_s + ", " + l2str(vz) + "))") aux_file.puts("suiporx.addBezier((" + (ind+1).round.to_s + ", " + l2str(tx) + "))") aux_file.puts("suipory.addBezier((" + (ind+1).round.to_s + ", " + l2str(ty) + "))") aux_file.puts("suiporz.addBezier((" + (ind+1).round.to_s + ", " + l2str(tz) + "))") aux_file.puts end # close file (python script) aux_file.close UI.messagebox("SU2Blender\n\nExport completed") end # create entries in menu if the script is not loaded yet if (not file_loaded?("[CASF]SU2Blender.rb")) CASF_menu = UI.menu("Plugins").add_submenu("[CASF] SU2Blender") CASF_menu.add_item("Export Individual Cameras") { export_individual_cameras } CASF_menu.add_item("Export Animated Camera") { export_animated_camera } end # load the script file_loaded("[CASF]SU2Blender.rb")