#!/usr/bin/python # Naming conventions follow RhinoScript, not PEP8. import rhinoscriptsyntax as rs def Main(): """Draw a surface and a cone and then render both objects.""" DrawSurface() DrawCone() RenderObjects() def DrawSurface(): """Draw a rendered NURBS surface using four edge curves.""" # Set the corners at the base of the surface in the x-y plane. frontLeftCorner = (-12,-6,0) frontRightCorner = (12,-6,0) rearLeftCorner = (-6,10,0) rearRightCorner = (6,10,0) # Set first/last `degree` knots to force curves to start/end at corners. degree = 3 knots = (0, 0, 0, 1, 2, 3, 4, 4, 4) # Vary the control point heights in the z direction based on the edge. frontHeights = (0, 7, 10, 12, 10, 7, 0) rearHeights = (0, 1, 2, 3, 2, 1, 0) leftHeights = (0, 2, 3, 3, 2, 1, 0) rightHeights = leftHeights # Create the control points for each edge. frontPoints = CreatePoints(frontLeftCorner, frontRightCorner, frontHeights) rearPoints = CreatePoints(rearLeftCorner, rearRightCorner, rearHeights) leftPoints = CreatePoints(frontLeftCorner, rearLeftCorner, leftHeights) rightPoints = CreatePoints(frontRightCorner, rearRightCorner, rightHeights) # Create NURBS curves for each edge. frontEdge = rs.AddNurbsCurve(frontPoints, knots, degree) rearEdge = rs.AddNurbsCurve(rearPoints, knots, degree) leftEdge = rs.AddNurbsCurve(leftPoints, knots, degree) rightEdge = rs.AddNurbsCurve(rightPoints, knots, degree) # Create a NURBS surface using (a tuple of) the edges. return rs.AddEdgeSrf((frontEdge, leftEdge, rearEdge, rightEdge)) def DrawCone(): """Create a cone in front of the surface to show orientation.""" return rs.AddCone((0,-13,0), (0,-13,2), 1) def RenderObjects(): """Render the objects in all views.""" views = rs.ViewNames() for view in views: rs.ViewDisplayMode(view, 'Rendered') def CreatePoints(start, end, h): """Return points between start and end in x and y, with z from h.""" points = [] for i in xrange(len(h)): points.append(CreatePoint(start, end, h, i)) return points def CreatePoint(start, end, h, i): """Return i^th point between start and end, with z taken from h.""" scalingFactor = float(i)/float(len(h)-1) x = start[0] + scalingFactor * (end[0] - start[0]) y = start[1] + scalingFactor * (end[1] - start[1]) z = h[i] return (x, y, z) # Invoke if this is run as a script. if( __name__ == "__main__" ): Main()