def Render(folder, count): """ Defines the Rendering action Saves the render to the browsed folder Adds the name of the view and the name of the layer state to the naming of the view """ FileName = '"'+folder+'/img_'+str(count)+'"' FileName = str(FileName) rs.Command ("!_-Render") rs.Command ("_-SaveRenderWindowAs "+FileName) rs.Sleep(2000) rs.Command ("_-CloseRenderWindow") rs.Sleep(1000) return 1
def viewportclock(): now = datetime.datetime.now() textobject_id = rs.AddText(now, (0, 0, 0), 20) if textobject_id is None: return while True: rs.Sleep(1000) now = datetime.datetime.now() rs.TextObjectText(textobject_id, now)
def screenshot_current_view(path, width=1920, height=1080, scale=1, draw_grid=False, draw_world_axes=False, draw_cplane_axes=False, background=False): """Take a screenshot of the current view. Parameters ---------- path : str The filepath for saving the screenshot. Other Parameters ---------------- width : int, optional height : int, optional scale : float, optional draw_grid : bool, optional draw_world_axes : bool, optional draw_cplane_axes : bool, optional background : bool, optional Returns ------- bool True if the command was successful. False otherwise. """ properties = [draw_grid, draw_world_axes, draw_cplane_axes, background] properties = ["Yes" if item else "No" for item in properties] scale = max(1, scale) # the rhino command requires a scale > 1 rs.EnableRedraw(True) rs.Sleep(0) result = rs.Command("-_ViewCaptureToFile \"" + os.path.abspath(path) + "\"" " Width=" + str(width) + " Height=" + str(height) + " Scale=" + str(scale) + " DrawGrid=" + properties[0] + " DrawWorldAxes=" + properties[1] + " DrawCPlaneAxes=" + properties[2] + " TransparentBackground=" + properties[3] + " _enter", False) rs.EnableRedraw(False) return result
def screenshot_current_view(path, width=1920, height=1080, scale=1, draw_grid=False, draw_world_axes=False, draw_cplane_axes=False, background=False): properties = [draw_grid, draw_world_axes, draw_cplane_axes, background] properties = ["Yes" if item else "No" for item in properties] scale = max(1, scale) # the rhino command requires a scale > 1 rs.EnableRedraw(True) rs.Sleep(0) result = rs.Command( "-_ViewCaptureToFile \"" + path + "\"" " Width=" + str(width) + " Height=" + str(height) + " Scale=" + str(scale) + " DrawGrid=" + properties[0] + " DrawWorldAxes=" + properties[1] + " DrawCPlaneAxes=" + properties[2] + " TransparentBackground=" + properties[3] + " _enter", False) rs.EnableRedraw(False) return result
# Clear the board rs.DeleteObjects(rs.AllObjects(select=True)) rabbit = Prey() rabbit.penup() rabbit.position = Point3d(20, 40, 0) rabbit.color = Color.Blue rabbit.pendown() fox = Predator(8) fox.color = Color.Red rabbitIcon = rs.AddSphere(rabbit.position, 0.5) rs.ObjectColor(rabbitIcon, rabbit.color) rabbit.decorate(rhinoturtle.Decorator(rabbitIcon)) foxIcon = rs.AddCone(Plane.WorldXY, 2, 1) rs.ObjectColor(foxIcon, fox.color) fox.decorate(rhinoturtle.Decorator(foxIcon)) caught = False while not caught: rabbit.wander() caught = fox.chase(rabbit) rs.Sleep(500) rs.DeleteObject(rabbitIcon)
def main(): skNum = (datetime.date.today()-datetime.date(2020, 03, 29)).days + 201 if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" rs.UnselectAllObjects() init_time = time.time() version = 'b' anim = mp4.Animation(os.path.splitext(os.path.basename(__file__))[0] + version) numFrames = 150 numPasses = 100 anim.fps = 30 td = TempDisplay() display = HUD(os.path.splitext(os.path.basename(__file__))[0], numFrames) s = Scene() ################################ #SETUP mObj = meshObj() ################################ for i in range(numFrames): start_time = time.time() print "Frame {}".format(i) if sc.escape_test(False): anim.Cleanup(); return ################################ #MAIN LOOP #if i%10 == 0: # mObj.MessUp() mObj.Update(i) mObj.UpdateDisplay() ################################ #HUD #display.UpdateParam1('boxes: ' + str(len(bSystem.boxes))) #display.UpdateParam2('links: ' + str(len(bSystem.links))) #display.UpdateParam3('z: ' + str(ball.pos.Z)) display.UpdateScaleBar() ################################ sc.doc.Views.Redraw() display.Update(i) anim.AddFrame(numberOfPasses = numPasses) rs.Sleep(500) ################################ #Framerate frameTime = time.time() - start_time timeLeft = (numFrames - i) * frameTime timeLeftStr = str(datetime.timedelta(seconds=timeLeft)) print "Time remaining: {}".format(timeLeftStr) frameTime = time.time() - init_time timeLeftStr = str(datetime.timedelta(seconds=frameTime)) print "Total Time: {}".format(timeLeftStr) if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" if os.path.isdir(r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim"): anim.Create(r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim", frames2Keep = [i/2, i-1]) else: anim.Create(r"C:\Tim\300 Days\anim", frames2Keep = [i/2, i-1])
def main(): skNum = (datetime.date.today() - datetime.date(2020, 03, 29)).days + 201 if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" rs.UnselectAllObjects() init_time = time.time() version = 'a' anim = mp4.Animation( os.path.splitext(os.path.basename(__file__))[0] + version) numFrames = 150 numPasses = 100 anim.fps = 30 td = TempDisplay() display = HUD(os.path.splitext(os.path.basename(__file__))[0], numFrames) s = Scene() ################################ #SETUP targets = [] vSystems = [] for i in range(5): targets.append(geo.Particle(geo.RandomPoint(), geo.RandomVector3d(5))) targets[-1].radius = 5 for i in range(5): print(i * .05) + .3 vSystems.append(VoxelSystem(targets, i * .05 + .3)) ################################ for i in range(numFrames): start_time = time.time() print "Frame {}".format(i) if sc.escape_test(False): anim.Cleanup() return ################################ #MAIN LOOP for target in targets: target.Update() for vSystem in vSystems: vSystem.Update() vSystem.UpdateDisplay() ################################ #HUD #display.UpdateParam1('boxes: ' + str(len(bSystem.boxes))) #display.UpdateParam2('links: ' + str(len(bSystem.links))) #display.UpdateParam3('z: ' + str(ball.pos.Z)) display.UpdateScaleBar() ################################ sc.doc.Views.Redraw() display.Update(i) anim.AddFrame(numberOfPasses=numPasses) rs.Sleep(500) ################################ #Framerate frameTime = time.time() - start_time timeLeft = (numFrames - i) * frameTime timeLeftStr = str(datetime.timedelta(seconds=timeLeft)) print "Time remaining: {}".format(timeLeftStr) frameTime = time.time() - init_time timeLeftStr = str(datetime.timedelta(seconds=frameTime)) print "Total Time: {}".format(timeLeftStr) if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" if os.path.isdir( r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim"): anim.Create(r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim", frames2Keep=[i / 2, i - 1]) else: anim.Create(r"C:\Tim\300 Days\anim", frames2Keep=[i / 2, i - 1])
def viewCapture(fileName, directory, viewNames, image_width, image_height, keepAspectRatio, saveAlpha): fullPathList = [] if saveAlpha == None: saveAlpha = False for viewName in viewNames: if viewName in rs.ViewNames(): rs.CurrentView(viewName, True) else: # change to RhinoDoc to get access to NamedViews sc.doc = rc.RhinoDoc.ActiveDoc namedViews = rs.NamedViews() if viewName in namedViews: viewName = rs.RestoreNamedView(viewName) else: viewName = None # change back to Grasshopper sc.doc = ghdoc viewName = rs.CurrentView(viewName, True) rs.CurrentView(viewName) sc.doc.Views.Find(viewName, False) viewtoCapture = sc.doc.Views.ActiveView try: dispMode = rc.Display.DisplayModeDescription.FindByName( dispModeStr) sc.doc.Views.ActiveView.ActiveViewport.DisplayMode = dispMode except: pass if image_height == None: image_h = viewtoCapture.ActiveViewport.Size.Height else: image_h = image_height if image_width == None: image_w = viewtoCapture.ActiveViewport.Size.Width else: image_w = image_width # aspectRatio if keepAspectRatio: if image_height == None and image_width != None: image_h = image_h * (image_w / viewtoCapture.ActiveViewport.Size.Width) elif image_height != None and image_width == None: image_w = image_w * (image_h / viewtoCapture.ActiveViewport.Size.Height) fullPath = os.path.join(directory, fileName + '_' + viewName + '.png') fullPathList.append(fullPath) # Set the image size rc.RhinoDoc.ActiveDoc.RenderSettings.UseViewportSize = False viewSize = System.Drawing.Size(int(image_w), int(image_h)) rc.RhinoDoc.ActiveDoc.RenderSettings.ImageSize = viewSize try: VRayForRhinoNETInterface.VRayInterface.SetRenderOutputSize( int(image_w), int(image_h)) except: pass print "Image dimensions set to (" + str(int(image_w)) + "x" + str( int(image_h)) + ")" # Render the image and save it. try: # V-Ray is the renderer. VRayForRhinoNETInterface.VRayInterface.HasRenderFinished() rs.Command("!_Render", echo=False) print "Rendering " + viewName + "..." while not VRayForRhinoNETInterface.VRayInterface.HasRenderFinished( ): if sc.escape_test(False): print("Rendering cancelled") rs.Command("_CloseRenderWindow", echo=False) rs.GetPlugInObject("V-Ray for Rhino").SetBatchRenderOn( True) vray.CancelRender() rs.GetPlugInObject("V-Ray for Rhino").SetBatchRenderOn( False) esc = True break rs.Sleep(10) rs.Command("_-SaveRenderWindowAs \"" + directory + "\\" + fileName + '_' + viewName + ".png\"") rs.Command( "_-CloseRenderWindow" ) #close the rendered window when in saving mode to avoid stacking a series of renderWindows when running on Rhino renderer. if saveAlpha == False: try: alphaPath = os.path.join( directory, fileName + '_' + viewName + '.Alpha.png') os.remove(alphaPath) except: pass except: if renderTime_ != None: # V-Ray is probably the renderer. start = time.clock() rs.Command("!_Render", echo=False) print "Rendering " + viewName + "..." while float(time.clock() - start) < renderTime_: rs.Sleep(10) rs.Command("_-SaveRenderWindowAs \"" + directory + "\\" + fileName + '_' + viewName + ".jpeg\"") rs.Command( "_-CloseRenderWindow" ) #close the rendered window when in saving mode to avoid stacking a series of renderWindows when running on Rhino renderer. if saveAlpha == False: try: alphaPath = os.path.join( directory, fileName + '_' + viewName + '.Alpha.jpeg') os.remove(alphaPath) except: pass else: # Hopefully Rhino is the renderer. print "Rendering " + viewName + "..." rs.Command("!_render") rs.Command("_-SaveRenderWindowAs \"" + directory + "\\" + fileName + '_' + viewName + ".png\"") rs.Command( "_-CloseRenderWindow" ) #close the rendered window when in saving mode to avoid stacking a series of renderWindows when running on Rhino renderer. return fullPathList
def main(): skNum = (datetime.date.today() - datetime.date(2020, 03, 29)).days + 201 if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" rs.UnselectAllObjects() init_time = time.time() version = 'a' anim = mp4.Animation( os.path.splitext(os.path.basename(__file__))[0] + version) numFrames = 150 numPasses = 100 anim.fps = 30 td = TempDisplay() display = HUD(os.path.splitext(os.path.basename(__file__))[0], numFrames) s = Scene() ################################ #SETUP pt = rg.Point3d(10, 50, 70) vec = rg.Vector3d(0, -10, 10) plane = rg.Plane(pt, vec) xint = rg.Interval(0, 80) yint = rg.Interval(0, 80) zint = rg.Interval(0, 40) xcount = 50 ycount = 50 zcount = 20 box = rg.Box(plane, xint, yint, zint) #m = rg.Mesh.CreateFromBox(box, xcount, ycount, zcount) m = rg.Mesh.CreateFromPlane(plane, xint, yint, xcount, ycount) mObj = vSystem(m) ################################ for i in range(numFrames): start_time = time.time() print "Frame {}".format(i) if sc.escape_test(False): anim.Cleanup() return ################################ #MAIN LOOP if i % 5 == 0: mObj.Update() mObj.UpdateDisplay() ################################ #HUD #display.UpdateParam1('boxes: ' + str(len(bSystem.boxes))) #display.UpdateParam2('links: ' + str(len(bSystem.links))) #display.UpdateParam3('z: ' + str(ball.pos.Z)) display.UpdateScaleBar() ################################ sc.doc.Views.Redraw() display.Update(i) anim.AddFrame(numberOfPasses=numPasses) rs.Sleep(500) ################################ #Framerate frameTime = time.time() - start_time timeLeft = (numFrames - i) * frameTime timeLeftStr = str(datetime.timedelta(seconds=timeLeft)) print "Time remaining: {}".format(timeLeftStr) frameTime = time.time() - init_time timeLeftStr = str(datetime.timedelta(seconds=frameTime)) print "Total Time: {}".format(timeLeftStr) if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" if os.path.isdir( r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim"): anim.Create(r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim", frames2Keep=[i / 2, i - 1]) else: anim.Create(r"C:\Tim\300 Days\anim", frames2Keep=[i / 2, i - 1])
def main(): skNum = (datetime.date.today() - datetime.date(2020, 03, 29)).days + 201 if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" rs.UnselectAllObjects() init_time = time.time() version = 'c' anim = mp4.Animation( os.path.splitext(os.path.basename(__file__))[0] + version) numFrames = 150 numPasses = 100 anim.fps = 30 td = TempDisplay() display = HUD(os.path.splitext(os.path.basename(__file__))[0], numFrames) s = Scene() ################################ #SETUP #bSystem = BoxSystem() #pSystem = ParticleSystem() #mesh = rg.Mesh() ptObjs = [] vecObjs = [] pts = [] ids = [] for i in range(10): pos = rg.Point3d(random.uniform(0, 100), random.uniform(0, 100), random.uniform(0, 100)) ptObjs.append(PointObj(pos, geo.RandomVector3d(1))) ptObjs[-1].size = 4 pts.append(pos) ################################ for i in range(numFrames): start_time = time.time() print "Frame {}".format(i) if sc.escape_test(False): anim.Cleanup() return ################################ #MAIN LOOP for id in ids: sc.doc.Objects.Delete(id, True) pts = [] for ptObj in ptObjs: ptObj.Update() ptObj.UpdateDisplay() pts.append(ptObj.pos) h = Hull(pts) for edge in h.edges: ids.append(Pipe(pts, edge, 4)) ################################ #HUD #display.UpdateParam1('boxes: ' + str(len(bSystem.boxes))) #display.UpdateParam2('links: ' + str(len(bSystem.links))) #display.UpdateParam3('z: ' + str(ball.pos.Z)) display.UpdateScaleBar() ################################ sc.doc.Views.Redraw() display.Update(i) anim.AddFrame(numberOfPasses=numPasses) rs.Sleep(500) ################################ #Framerate frameTime = time.time() - start_time timeLeft = (numFrames - i) * frameTime timeLeftStr = str(datetime.timedelta(seconds=timeLeft)) print "Time remaining: {}".format(timeLeftStr) frameTime = time.time() - init_time timeLeftStr = str(datetime.timedelta(seconds=frameTime)) print "Total Time: {}".format(timeLeftStr) if int(skNum) > int(os.path.splitext(os.path.basename(__file__))[0]): print "!!!!SAVE THE SKETCH WITH A NEW NAME!!!!" if os.path.isdir( r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim"): anim.Create(r"D:\Files\Work\LIBRARY\06_RHINO\10_Python\300 DAYS\anim", frames2Keep=[i / 2, i - 1]) else: anim.Create(r"C:\Tim\300 Days\anim", frames2Keep=[i / 2, i - 1])
import rhinoturtle import celestial import math def createSphereDecorator(body): radius = math.log(body.radius) / 100 sphere = rs.AddSphere(body.position, radius) rs.ObjectColor(sphere, body.color) return rhinoturtle.Decorator(sphere) def createLabelDecorator(body): text = rs.AddTextDot(body.name, body.position + Vector3d.ZAxis * 0.25) rs.ObjectColor(text, body.color) return rhinoturtle.Decorator(text) # Clear the board rs.DeleteObjects(rs.AllObjects(select=True)) solarSystem = celestial.System.fromJSON('./solar-system.json') for body in solarSystem.bodies: body.decorate(createSphereDecorator(body)) body.decorate(createLabelDecorator(body)) for i in range(730): solarSystem.advance(1) rs.Sleep(10)