macroScript QuadSphere category:"Objects Primitives" internalCategory:"Objects Primitives" tooltip:"Quad Sphere" ButtonText:"Quad Sphere" icon:#("standard", 2) ( on execute do StartObjectCreation QuadSphere on isChecked return (mcrUtils.IsCreating QuadSphere) ) --Version 1.1: --Added ability to set settings as default plugin simpleObject QuadSphere name:"Quad Sphere" category:"Standard Primitives" classID:#(0x5474b0af, 0x7ca5c47c) ( --variables related to the file naming and paths for saving defaults local ScriptName = "QuadSphere" local ScriptsFolder = pathconfig.getdir #userscripts local DefaultsFolder = pathconfig.appendPath ScriptsFolder "\\PrimitiveObejcts" local DefaultsFilePath = pathconfig.appendPath DefaultsFolder ("\\" + ScriptName + "_Defaults.ini") ---------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------ SAVE & LOAD DEFAULTS FUNTIONS ------------------------------------------ ---------------------------------------------------------------------------------------------------------------------------------- --A function which is used to save defaults to a file fn SaveToFile FilePath SectionString KeyString ValueString = ( local theFile = FilePath as string local theSection = SectionString as string local theKey = KeyString as string local theValue = ValueString as string if not doesFileExist theFile then ( --take directory path part of a full file name local Directory = getFilenamePath theFile --Create a directory makeDir Directory --OutPut to log format "Directory % has been created\n" Directory ) --if write sucessfull if (setINISetting theFile theSection theKey theValue) then format "File has been updated sucessfully in %\n" theFile else format "ERROR: File was not saved in %\n" theFile )--end of function --Function that return value by its name which was readed from ini file. fn LoadValueFromFile FilePath SectionString KeyString = ( local theFile = FilePath as string local theSection = SectionString as string local theKey = KeyString as string -- If file is not exists if not doesFileExist theFile then ( return undefined ) return getINISetting theFile theSection theKey )--end of function ---------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- parameters main rollout:params ( radius type:#float ui:RadiusSpnr default:25.0 Segments type:#float ui:SegmentsSpnr default:2 ) rollout params "Parameters" ( spinner RadiusSpnr"Radius "range:[0,10000,50.0] spinner SegmentsSpnr "Segments "range:[2,200,2] type:#integer button AsDefaultBtn "Set as Default" on AsDefaultBtn pressed do ( SaveToFile DefaultsFilePath "Defaults" "Segments" Segments SaveToFile DefaultsFilePath "Defaults" "Radius" radius ) ) --this function takes vertex coord as argument and place it like on a sphere, also correction done for a better quad distribution fn SpherifyVertex vert objpos raduis = ( --find direction vector local dirv = vert - objpos --normalize direction vector dirv /= length dirv --compute power x2 = dirv.x * dirv.x y2 = dirv.y * dirv.y z2 = dirv.z * dirv.z local dirv2 = [0,0,0] --topology correction dirv2.x = dirv.x*sqrt(1.0-(y2/2.0)-(z2/2.0)+((y2*z2)/3.0)) dirv2.y = dirv.y*sqrt(1.0-(x2/2.0)-(z2/2.0)+((x2*z2)/3.0)) dirv2.z = dirv.z*sqrt(1.0-(x2/2.0)-(y2/2.0)+((x2*y2)/3.0)) --normalize corrected direction vector dirv2/= length dirv2 --spherify dirv2*raduis )--end of SpherifyVertex on buildMesh do ( obj = createinstance box width: 1 height: 1 length: 1 lengthsegs: segments heightsegs: segments widthsegs: segments objmesh = copy obj.mesh numvert = getNumVerts objmesh local objpos = [0,0,0.5] for i = 1 to numvert do ( local vert = getVert objmesh i local SphereVert = SpherifyVertex vert objpos radius setVert objmesh i SphereVert ) meshop.autoSmooth objmesh #{1..objmesh.numfaces} 180.0 mesh = objmesh )--end buildMesh on create do ( local Segs try(segs = LoadValueFromFile DefaultsFilePath "Defaults" "Segments" as integer)catch() if Segs != undefined and Segs != 0 then ( Segments = Segs ) local Rad try(Rad = LoadValueFromFile DefaultsFilePath "Defaults" "Radius" as integer)catch() if Rad != undefined and Rad != 0 then ( radius = Rad ) ) tool create ( on mousePoint click do ( case click of ( 1: coordsys grid (nodeTM.translation = gridPoint) ) ) on mouseMove click do ( case click of ( 2: ( local x = abs(gridDist.x) local y = abs(gridDist.y) local z = abs(gridDist.z) local maxvalue = amax x y z radius = maxvalue ) 3: (#stop) ) ) )--end create )--end plugin