//SIZZLEX .py Blender (Python) @[ // (c)2007 Andersson Technologies LLC // // Blender exporter for version 2.43 and hopefully later. // You will have to tweak the background settings manually---functionality // to do this from the script has largely been withdrawn from blender. //trackerSize = 0.001 // relative to world size //objSize = 0.01 // object in object-mode xscl = 1 yscl = 1 zscl = 1 bias = 1 if(0) // must be 1, blender requires frame# starting at 1 dlg = NewDialog("blender") dlg.Check("bias", "Start at Frame 1", bias) dlg.Show() bias = dlg.bias end axisMode = 0 // ALWAYS Z Up rotOrder = 1 maincam = Scene.activeObj.cam scnDis = 4 Header(maincam) for (ob in Obj) if (ob.cam == maincam) OutObj(ob) end end for (msh in Mesh) OutMesh(msh) end for (ob in Lite) OutLight(ob) end SetFrame(firstFrame) //"camobj.Select()\n" "scene.update(1)\n" "Blender.Redraw()\n" // Message("Set the Output Format AspX to " round(pixasp*100) " and AspY to 100") // Functions // Output scene-wide parameters, and shot background function Header(ob) shot = ob.shot pixasp = shot.aspect * shot.height / shot.width "# Blender Exporter: " Scene.sourceFile "\n" DateTime("# Exported %a, %b %d, %Y %I:%M:%S %p\n") // Framerate "import Blender\n" "from Blender import Scene, Object, Camera, Ipo, Texture, Image, Material\n" "scene = Blender.Scene.GetCurrent()\n" "context = scene.getRenderingContext()\n" "context.framesPerSec(" shot.rate ")\n" firstFrame = shot.start lastFrame = shot.stop "context.startFrame(" firstFrame + bias ")\n" "context.endFrame(" lastFrame + bias ")\n" "context.aspectRatioX(" round(shot.pixasp * 100) ")\n" "context.aspectRatioY(100)\n" if (0) if (abs(shot.aspect - 1.333) < 0.02) "context.aspectRatioX(4)\n" "context.aspectRatioY(3)\n" elseif (abs(shot.aspect - 1.778) < 0.02) "context.aspectRatioX(16)\n" "context.aspectRatioY(9)\n" elseif (abs(shot.aspect - 2.35) < 0.02) "context.aspectRatioX(47)\n" "context.aspectRatioY(20)\n" end end SetFrame(0) "synwld = Blender.Object.New('Empty', 'SynthEyesWorld')\n" "scene.link(synwld)\n" // Output image size and aspect "context.imageSizeX( " shot.width " )\n" "context.imageSizeY( " shot.height " )\n" "# " fix_slashes(shot.nm) "\n" end // Output the information for a camera or moving object function OutObj(ob) onm = ob.nm basecam = ob.cam wsize = ob.worldSize pathChange = DoesTransformChange(ob) fixedLens = IsLensFixed(basecam) // Set up the base object in MAX frame = firstFrame SetFrame(frame) if (!ob.isCamera) "obj = Blender.Object.New('Empty', '" onm "')\n" else "obj = scene.getCurrentCamera()\n" "obj.setName(\"" onm "\")\n" "cam = obj.getData()\n" "camipo = Blender.Ipo.New('Camera', '" onm "Ipo')\n" "cam.ipo = camipo\n" // Code to make the camera, but blender can't create the lens ipo // "cam = Camera.New('persp')\n" // "obj = Object.New('Camera', '" onm "')\n" // "obj.link(cam)\n" // "camobj = obj\n" // "scene.setCurrentCamera(camobj)\n" // not cam end if (ob.isCamera) BuildTexture(ob) end printf("obj.setLocation(%lg,%lg,%lg)\n", ob.px*xscl, ob.py*yscl, ob.pz*zscl) printf("obj.setEuler((%lg,%lg,%lg))\n", EX(ob.trans), EY(ob.trans), EZ(ob.trans)) "cam.setLens(" Lens(ob.fov) ")\n" //printf("# axyz %12lg %12lg %12lg\n", ob.ax, ob.ay, ob.az); //printf("# bxyz %12lg %12lg %12lg\n", ob.bx, ob.by, ob.bz); //printf("# cxyz %12lg %12lg %12lg\n", ob.cx, ob.cy, ob.cz); // Whale out the output trajectory. if (pathChange) "ipo = Ipo.New('Object', '" ob.nm "Ipo')\n" "obj.setIpo(ipo)\n" AddChannel("lxc", "LocX"); AddChannel("lyc", "LocY"); AddChannel("lzc", "LocZ"); AddChannel("rxc", "RotX"); AddChannel("ryc", "RotY"); AddChannel("rzc", "RotZ"); end if (ob.isCamera && !fixedLens) // on CAMERA data-block's IPO, not the Camera Node Object ipo "lipo = camipo.addCurve('Lens')\n" end if (pathChange) for (frame = firstFrame; frame <= lastFrame; frame++) "lxc.addBezier((" frame+bias ", " ob.px*xscl "))\n" "lxc.update()\n" "lyc.addBezier((" frame+bias ", " ob.py*yscl "))\n" "lyc.update()\n" "lzc.addBezier((" frame+bias ", " ob.pz*zscl "))\n" "lzc.update()\n" "rxc.addBezier((" frame+bias ", " RX(ob.trans) "))\n" "rxc.update()\n" "ryc.addBezier((" frame+bias ", " RY(ob.trans) "))\n" "ryc.update()\n" "rzc.addBezier((" frame+bias ", " RZ(ob.trans) "))\n" "rzc.update()\n" end end if (!ob.isCamera) // re-used camera is already in the scene "scene.link(obj)\n" // Do late to avoid blender problem end "synwld.makeParent([obj])\n" if (ob.isCamera && !fixedLens) for (frame = firstFrame; frame <= lastFrame; frame++) "lipo.addBezier((" frame+bias ", " Lens(ob.fov) "))\n" "lipo.update()\n" end end SetFrame(firstFrame) // if (ob.isCamera) // Screen(ob) // end // The trackers frame = firstFrame for (tk in ob.trk) if (!tk.isSolved || !tk.isExported) continue end // Always output wvec, not ovec. Are same if isCamera. If isObject, // need to output world anyway, so that when parent is changed, // the right spot results. "ntk = Blender.Object.New('Empty', '" tk.nm "')\n" // Changed by Bo Johansen/CineGobs 14/8-2009 ---> printf("ntk.setLocation(%lg,%lg,%lg)\n", tk.wvec.x*xscl, tk.wvec.y*yscl, tk.wvec.z*zscl) // if (ob.isCamera) // printf("ntk.setLocation(%lg,%lg,%lg)\n", // tk.wvec.x*xscl, tk.wvec.y*yscl, tk.wvec.z*zscl) // else // printf("ntk.setLocation(%lg,%lg,%lg)\n", // tk.ovec.x*xscl, tk.ovec.y*yscl, tk.ovec.z*zscl) // end // <--- // Drive out far tracker paths in their full glory if camera moves if (tk.isSolved == 2 || tk.isFar && (pathChange & 1) != 0) for (frame = firstFrame; frame <= lastFrame; frame++) SetFrame(frame) printf("ntk.setLocation(%lg,%lg,%lg)\n", tk.wvec.x*xscl, tk.wvec.y*yscl, tk.wvec.z*zscl) end "ipo = Ipo.New('Object', '" tk.nm "Ipo')\n" "ntk.setIpo(ipo)\n" AddChannel("lxc", "LocX"); AddChannel("lyc", "LocY"); AddChannel("lzc", "LocZ"); for (frame = firstFrame; frame <= lastFrame; frame++) "lxc.addBezier((" frame+bias ", " tk.wvec.x*xscl "))\n" "lxc.update()\n" "lyc.addBezier((" frame+bias ", " tk.wvec.y*yscl "))\n" "lyc.update()\n" "lzc.addBezier((" frame+bias ", " tk.wvec.z*zscl "))\n" "lzc.update()\n" end end "scene.link(ntk)\n" if (ob.isCamera) "synwld.makeParent([ntk])\n" else "obj.makeParent([ntk])\n" end end "scene.update()\n" end function AddChannel(chvar, chnam) chvar " = ipo.addCurve('" chnam "')\n" chvar ".setInterpolation('Linear')\n" chvar ".setExtrapolation('Constant')\n" end function BuildTexture(cmo) frame = firstFrame if (shot.imageName != "") "img = Image.Load(\"" fix_slashes(shot.imageName) "\")\n" else "img = Image.Load(\"" fix_slashes(shot.nm) "\")\n" end "tex = Texture.New('" cmo.nm "Tex')\n" "tex.setType('Image')\n" if (shot.imageName == "") "# tex.setImageFlags('Movie')\n" end "tex.setImage(img)\n" "tex.animFrames = " lastFrame - firstFrame+1 "\n" "# tex.animLength = " shot.length "\n" "tex.animOffset = " firstFrame "\n" "tex.animStart = " firstFrame + bias "\n" "mtl = Material.New('" cmo.nm "Mtl')\n" "mtl.setTexture(0, tex)\n" end function Screen(cob) SetFrame(firstFrame) "scn = doc.CreatePlane(4,4)\n" "scn.Name = \"" cob.nm "Screen\"\n" wdis = cob.worldSize * scnDis if (fixedLens) frame = firstFrame SetFrame(frame) wdis = cob.worldSize * scnDis hsz = 2* wdis * tan(0.5*cob.fov) vsz = hsz / cob.shot.aspect printf("scn.Scale = (%lg,%lg,%lg)\n", -hsz, vsz, 0) end for (frame = firstFrame; frame <= lastFrame; frame++) SetFrame(frame) if (!fixedLens) hsz = 2* wdis * tan(0.5*cob.fov) vsz = hsz / cob.shot.aspect printf("scn.Scale = (%lg,%lg,%lg)\n", hsz, vsz, 0) end printf("scn.setLocation(%lg,%lg,%lg)\n", (cob.px - wdis*cob.cx)*xscl, (cob.py - wdis*cob.cy)*yscl, (cob.pz - wdis*cob.cz)*zscl) printf("scn.setEuler((%lg,%lg,%lg))\n", EX(cob.trans), EY(cob.trans), EZ(cob.trans)) //" scn.SetFrame()\n" end end // Create the light. Based on a createNode example for a sphere // Animated light arises only when light is driven from trackers // on moving objects. function OutLight(ob) frame = firstFrame litenm = ob.nm "liteobj = Object.New('Lamp', '" litenm "')\n" "lite = Blender.Lamp.New('" ((ob.kind == "directional") ? "Sun" : "Lamp") "', '" litenm "Data')\n" "liteobj.link(lite)\n" "scene.link(liteobj)\n" // Do late to avoid blender problem "synwld.makeParent([liteobj])\n" printf("liteobj.setLocation(%lg,%lg,%lg)\n", ob.px*xscl, ob.py*yscl, ob.pz*zscl) printf("liteobj.setEuler((%lg,%lg,%lg))\n", EX(ob.trans), EY(ob.trans), EZ(ob.trans)) end // // Meshes // function OutMesh(msh) frame = firstFrame mshnm = msh.nm "mshobj = Blender.Mesh.New('" mshnm "Mesh')\n" sx = msh.sx sy = msh.sy sz = msh.sz "coords = [\n" for (i = 1; i <= #msh.vtx; i++) " [ " msh.vtx[i].x * sx ", " msh.vtx[i].y * sy ", " msh.vtx[i].z * sz"]" if (i != #msh.vtx) "," end "\n" end "]\n" "faces = [\n" for (i = 1; i <= #msh.face; i++) " [ " msh.face[i].vA -1 ", " msh.face[i].vB -1 ", " msh.face[i].vC -1 "]" if (i != #msh.face) "," end "\n" end "]\n" "mshobj.verts.extend(coords)\n" "mshobj.faces.extend(faces)\n" "ob = scene.objects.new(mshobj, '" mshnm "')\n" if (msh.obj == 0) "synwld.makeParent([ob])\n" else // Changed by Bo Johansen/CineGobs 14/8-2009 ---> "pobj = Blender.Object.Get ('" msh.obj.nm "')\n" "pobj.makeParent([ob])\n" // msh.obj.nm ".makeParent([ob])\n" // <--- end printf("ob.setLocation(%lg,%lg,%lg)\n", // Changed by Bo Johansen/CineGobs 14/8-2009 ---> msh.wx, msh.wy, msh.wz) // msh.x*xscl, msh.y*yscl, msh.z*zscl) printf("ob.setEuler((%lg,%lg,%lg))\n", WE(msh.wrx), WE(msh.wry), WE(msh.wrz)) // EX(msh.trans), EY(msh.trans), EZ(msh.trans)) // <--- end // Support routines. // for use by IPO function RX(tra) return 0.1*tra.rx //* Pi/180.0 end function RY(tra) return 0.1*tra.ry //* Pi/180.0 end function RZ(tra) return 0.1*tra.rz //* Pi/180.0 end // For use by setEuler function EX(tra) return tra.rx * Pi/180.0 end function EY(tra) return tra.ry * Pi/180.0 end function EZ(tra) return tra.rz * Pi/180.0 end // Added by Bo Johansen/CineGobs 14/8-2009 ---> function WE(r) return r * Pi/180.0 end // <--- function Lens(fov) return 16 / tan(0.5*fov) // or pixasp? end function SetFrame(frm) "Blender.Set('curframe', " frm+bias ")\n" end // returns 2: rot changes, 1: pos changes, 0: both stationary (3:both chg) function DoesTransformChange(ob) frame = firstFrame tr0 = ob.trans pos0 = ob.rowP rv = 0; for (frame = firstFrame+1; frame <= lastFrame; frame++) tr = ob.trans rv |= ((tr.rowP != pos0) ? 1 : 0) rv |= ((tr != tr0) ? 2 : 0) end return rv; end function DoesMeshTransformChange(ob) // Use ONLY for meshes frame = firstFrame tr0 = ob.wtrans pos0 = ob.rowP rv = 0; for (frame = firstFrame+1; frame <= lastFrame; frame++) tr = ob.wtrans rv |= ((tr.rowP != pos0) ? 1 : 0) rv |= ((tr != tr0) ? 2 : 0) end return rv; end // Check to see if the lens fov is fixed, indep of mode function IsLensFixed(cam) frame = firstFrame fov0 = cam.fov //+ 0 for (frame = firstFrame+1; frame <= lastFrame; frame++) if (cam.fov != fov0) return 0 end end return 1 end function fix_slashes(slname) newname = "" for (chno = 1; chno <= length(slname); chno++) chr = substr(slname, chno, 1) if (chr == "\\") chr = "\\\\" end newname = (newname chr) end return newname end