def AddLightActor (r): if (r!=0): print('pre: r_exits') lights = r.GetLIghts() lights.InitTraversal() l = lights.GetNextItem() while(l!=0): angle = l.GetConeAngle() if(l.LIghtTypeIsSceneLight() and l.GetPositional() and angle<180. ): la = vtk.vtkLightActor() la.SetLight(l) r.AddViewProp(la) la.Delete l = lights .GetNextItem()
def light( self, pos=(1, 1, 1), fp=(0, 0, 0), deg=25, diffuse="y", ambient="r", specular="b", showsource=False, ): """ Generate a source of light placed at pos, directed to focal point fp. :param fp: focal Point, if this is a ``vtkActor`` use its position. :type fp: vtkActor, list :param deg: aperture angle of the light source :param showsource: if `True`, will show a vtk representation of the source of light as an extra actor .. hint:: |lights.py|_ """ if isinstance(fp, vtk.vtkActor): fp = fp.GetPosition() light = vtk.vtkLight() light.SetLightTypeToSceneLight() light.SetPosition(pos) light.SetPositional(1) light.SetConeAngle(deg) light.SetFocalPoint(fp) light.SetDiffuseColor(colors.getColor(diffuse)) light.SetAmbientColor(colors.getColor(ambient)) light.SetSpecularColor(colors.getColor(specular)) save_int = self.interactive self.show(interactive=0) self.interactive = save_int if showsource: lightActor = vtk.vtkLightActor() lightActor.SetLight(light) self.renderer.AddViewProp(lightActor) self.renderer.AddLight(light) return light
def __init__(self, position=None, focal_point=None, color=None, light_type='scene light'): """Initialize the light.""" super().__init__() if position is not None: self.position = position if focal_point is not None: self.focal_point = focal_point if color is not None: self.ambient_color = color self.diffuse_color = color self.specular_color = color if isinstance(light_type, str): # be forgiving: ignore spaces and case light_type_orig = light_type type_normalized = light_type.replace(' ', '').lower() mapping = {'headlight': LightType.HEADLIGHT, 'cameralight': LightType.CAMERA_LIGHT, 'scenelight': LightType.SCENE_LIGHT, } try: light_type = mapping[type_normalized] except KeyError: raise ValueError(f'Invalid light_type "{light_type_orig}"') from None elif not isinstance(light_type, int): raise TypeError('Parameter light_type must be int or str,' f' not {type(light_type).__name__}.') # LightType is an int subclass self.light_type = light_type self._actor = vtk.vtkLightActor() self._actor.SetLight(self) self._actor.VisibilityOff()
def main(): interactor = vtk.vtkRenderWindowInteractor() renderWindow = vtk.vtkRenderWindow() renderWindow.SetSize(400, 400) renderWindow.SetMultiSamples(0) renderWindow.SetAlphaBitPlanes(1) interactor.SetRenderWindow(renderWindow) renderer = vtk.vtkOpenGLRenderer() renderWindow.AddRenderer(renderer) renderWindow.SetSize(640, 480) rectangleSource = vtk.vtkPlaneSource() rectangleSource.SetOrigin(-5.0, 0.0, 5.0) rectangleSource.SetPoint1(5.0, 0.0, 5.0) rectangleSource.SetPoint2(-5.0, 0.0, -5.0) rectangleSource.SetResolution(100, 100) rectangleMapper = vtk.vtkPolyDataMapper() rectangleMapper.SetInputConnection(rectangleSource.GetOutputPort()) rectangleMapper.SetScalarVisibility(0) shadows = vtk.vtkShadowMapPass() seq = vtk.vtkSequencePass() passes = vtk.vtkRenderPassCollection() passes.AddItem(shadows.GetShadowMapBakerPass()) passes.AddItem(shadows) seq.SetPasses(passes) cameraP = vtk.vtkCameraPass() cameraP.SetDelegatePass(seq) # tell the renderer to use our render pass pipeline glrenderer = renderer glrenderer.SetPass(cameraP) colors = vtk.vtkNamedColors() boxColor = colors.GetColor3d("Tomato") rectangleColor = colors.GetColor3d("Beige") coneColor = colors.GetColor3d("Peacock") sphereColor = colors.GetColor3d("Banana") rectangleActor = vtk.vtkActor() rectangleActor.SetMapper(rectangleMapper) rectangleActor.VisibilityOn() rectangleActor.GetProperty().SetColor(rectangleColor) boxSource = vtk.vtkCubeSource() boxSource.SetXLength(2.0) boxNormals = vtk.vtkPolyDataNormals() boxNormals.SetInputConnection(boxSource.GetOutputPort()) boxNormals.ComputePointNormalsOff() boxNormals.ComputeCellNormalsOn() boxNormals.Update() boxNormals.GetOutput().GetPointData().SetNormals(None) boxMapper = vtk.vtkPolyDataMapper() boxMapper.SetInputConnection(boxNormals.GetOutputPort()) boxMapper.ScalarVisibilityOff() boxActor = vtk.vtkActor() boxActor.SetMapper(boxMapper) boxActor.VisibilityOn() boxActor.SetPosition(-2.0, 2.0, 0.0) boxActor.GetProperty().SetColor(boxColor) coneSource = vtk.vtkConeSource() coneSource.SetResolution(24) coneSource.SetDirection(1.0, 1.0, 1.0) coneMapper = vtk.vtkPolyDataMapper() coneMapper.SetInputConnection(coneSource.GetOutputPort()) coneMapper.SetScalarVisibility(0) coneActor = vtk.vtkActor() coneActor.SetMapper(coneMapper) coneActor.VisibilityOn() coneActor.SetPosition(0.0, 1.0, 1.0) coneActor.GetProperty().SetColor(coneColor) sphereSource = vtk.vtkSphereSource() sphereSource.SetThetaResolution(32) sphereSource.SetPhiResolution(32) sphereMapper = vtk.vtkPolyDataMapper() sphereMapper.SetInputConnection(sphereSource.GetOutputPort()) sphereMapper.ScalarVisibilityOff() sphereActor = vtk.vtkActor() sphereActor.SetMapper(sphereMapper) sphereActor.VisibilityOn() sphereActor.SetPosition(2.0, 2.0, -1.0) sphereActor.GetProperty().SetColor(sphereColor) renderer.AddViewProp(rectangleActor) renderer.AddViewProp(boxActor) renderer.AddViewProp(coneActor) renderer.AddViewProp(sphereActor) # Spotlights. # lighting the box. l1 = vtk.vtkLight() l1.SetPosition(-4.0, 4.0, -1.0) l1.SetFocalPoint(boxActor.GetPosition()) l1.SetColor(1.0, 1.0, 1.0) l1.PositionalOn() renderer.AddLight(l1) l1.SwitchOn() # lighting the sphere l2 = vtk.vtkLight() l2.SetPosition(4.0, 5.0, 1.0) l2.SetFocalPoint(sphereActor.GetPosition()) l2.SetColor(1.0, 0.0, 1.0) l2.PositionalOn() renderer.AddLight(l2) l2.SwitchOn() # For each spotlight, add a light frustum wireframe representation and a cone # wireframe representation, colored with the light color. angle = l1.GetConeAngle() if l1.LightTypeIsSceneLight() and l1.GetPositional( ) and angle < 180.0: # spotlight la = vtk.vtkLightActor() la.SetLight(l1) renderer.AddViewProp(la) angle = l2.GetConeAngle() if l2.LightTypeIsSceneLight() and l2.GetPositional( ) and angle < 180.0: # spotlight la = vtk.vtkLightActor() la.SetLight(l2) renderer.AddViewProp(la) renderer.SetBackground2(colors.GetColor3d("Silver")) renderer.SetBackground(colors.GetColor3d("LightGrey")) renderer.SetGradientBackground(True) renderWindow.Render() renderWindow.SetWindowName('ShadowsLightsDemo') renderer.ResetCamera() camera = renderer.GetActiveCamera() camera.Azimuth(40.0) camera.Elevation(10.0) renderWindow.Render() interactor.Start()
def __init__(self,parent = None): super(robotWindow,self).__init__(parent = None) self.setupUi(self) self.robot = palmModel() self.vtkWidget = QVTKRenderWindowInteractor(self.robotWidget) self.ren = vtk.vtkRenderer() lightPosition = [0,0,1] lightFocalPoint = [0,0,0] light = vtk.vtkLight() light.SetLightTypeToSceneLight() light.SetPosition(lightPosition[0], lightPosition[1], lightPosition[2]) light.SetPositional(True) light.SetConeAngle(10) light.SetFocalPoint(lightFocalPoint[0], lightFocalPoint[1], lightFocalPoint[2]) light.SetDiffuseColor(1,0,0) light.SetAmbientColor(0,1,0) light.SetSpecularColor(0,0,1) lightActor = vtk.vtkLightActor() lightActor.SetLight(light) self.ren.AddViewProp(lightActor) self.ren.SetBackground(0, 0.6, 0.9) self.renderWindow = self.vtkWidget.GetRenderWindow() self.renderWindow.AddRenderer(self.ren) self.style = vtk.vtkInteractorStyleTrackballCamera() self.vtkWidget.SetInteractorStyle(self.style) self.checkBox = [self.thumbChectBox,self.foreCheckBox,self.middleCheckBox, self.ringCheckBox,self.littleCheckBox] if not self.thumbChectBox.isChecked(): self.robot = robotModel() self.setArmConnect() self.finger = [self.robot.thumb.finger,self.robot.fingerOne,self.robot.fingerTwo, self.robot.fingerThree,self.robot.fingerFour] self.fingerSender = [self.thumbYSlide,self.thumbSlide,self.foreSlide, self.middleSlide,self.ringSlide,self.littleSlide] self.setPalmConnect() map(self.connectFun,self.checkBox, [self.changeCheckBoxConnect for i in range(len(self.checkBox))], [SIGNAL("clicked()") for i in range(len(self.checkBox))]) self.fingerTogether = [] self.slideTogether = [] self.connect(self.palmModelCheckBox,SIGNAL("stateChanged(int)"),self.changeRobotModel) self.ren.AddActor(self.robot.getAssembly()) self.axes = vtk.vtkAxesActor() self.axes.SetTotalLength(100,100,100) center = self.ren.GetCenter() self.axes.SetPosition(0,0,0) #self.ren.AddActor(self.axes) self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() self.iren.Initialize()