|
| 1 | +import warnings |
| 2 | +import numpy as np |
| 3 | +from typing import Union |
| 4 | +from pyrep.backend import sim |
| 5 | +from pyrep.const import ObjectType |
| 6 | +from pyrep.objects.object import Object, object_type_to_class |
| 7 | + |
| 8 | + |
| 9 | +class Light(Object): |
| 10 | + """A light. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self, name_or_handle: Union[str, int]): |
| 14 | + super().__init__(name_or_handle) |
| 15 | + |
| 16 | + def _get_requested_type(self) -> ObjectType: |
| 17 | + return ObjectType.LIGHT |
| 18 | + |
| 19 | + # On and Off |
| 20 | + |
| 21 | + def turn_on(self): |
| 22 | + """ Turn the light on. |
| 23 | + """ |
| 24 | + sim.simSetLightParameters(self._handle, True) |
| 25 | + |
| 26 | + def turn_off(self): |
| 27 | + """ Turn the light off. |
| 28 | + """ |
| 29 | + sim.simSetLightParameters(self._handle, False) |
| 30 | + |
| 31 | + def is_on(self): |
| 32 | + """ Determines whether the light is on. |
| 33 | + return: Boolean |
| 34 | + """ |
| 35 | + return sim.simGetLightParameters(self._handle)[0] |
| 36 | + |
| 37 | + def is_off(self): |
| 38 | + """ Determines whether the light is off. |
| 39 | + return: Boolean |
| 40 | + """ |
| 41 | + return not sim.simGetLightParameters(self._handle)[0] |
| 42 | + |
| 43 | + # Get and Set Color |
| 44 | + |
| 45 | + def get_diffuse(self): |
| 46 | + """ Get the diffuse colors of the light. |
| 47 | + return: 3-vector np.array of diffuse colors |
| 48 | + """ |
| 49 | + return np.asarray(sim.simGetLightParameters(self._handle)[1]) |
| 50 | + |
| 51 | + def set_diffuse(self, diffuse): |
| 52 | + """ Set the diffuse colors of the light. |
| 53 | + """ |
| 54 | + sim.simSetLightParameters(self._handle, self.is_on(), list(diffuse)) |
| 55 | + |
| 56 | + def get_specular(self): |
| 57 | + """ Get the specular colors of the light. |
| 58 | + return: 3-vector np.array of specular colors |
| 59 | + """ |
| 60 | + return np.asarray(sim.simGetLightParameters(self._handle)[2]) |
| 61 | + |
| 62 | + def set_specular(self, specular): |
| 63 | + """ Set the specular colors of the light. |
| 64 | + """ |
| 65 | + sim.simSetLightParameters(self._handle, self.is_on(), specularPart=list(specular)) |
| 66 | + |
| 67 | + # Intensity Properties |
| 68 | + |
| 69 | + def get_intensity_properties(self): |
| 70 | + """ Gets light intensity properties. |
| 71 | +
|
| 72 | + :return: The light intensity properties cast_shadows, spot_exponent, spot_cutoff, const_atten_factor, |
| 73 | + linear_atten_factor, quad_atten_factor |
| 74 | + """ |
| 75 | + cast_shadows = sim.simGetObjectInt32Parameter(self._handle, sim.sim_lightintparam_pov_casts_shadows) |
| 76 | + spot_exponent = sim.simGetObjectFloatParameter(self._handle, sim.sim_lightfloatparam_spot_exponent) |
| 77 | + spot_cutoff = sim.simGetObjectFloatParameter(self._handle, sim.sim_lightfloatparam_spot_cutoff) |
| 78 | + const_atten_factor = sim.simGetObjectFloatParameter(self._handle, sim.sim_lightfloatparam_const_attenuation) |
| 79 | + linear_atten_factor = sim.simGetObjectFloatParameter(self._handle, sim.sim_lightfloatparam_lin_attenuation) |
| 80 | + quad_atten_factor = sim.simGetObjectFloatParameter(self._handle, sim.sim_lightfloatparam_quad_attenuation) |
| 81 | + return bool(cast_shadows), spot_exponent, spot_cutoff, const_atten_factor, linear_atten_factor,\ |
| 82 | + quad_atten_factor |
| 83 | + |
| 84 | + def set_intensity_properties(self, cast_shadows=None, spot_exponent=None, spot_cutoff=None, const_atten_factor=None, |
| 85 | + linear_atten_factor=None, quad_atten_factor=None): |
| 86 | + """ Set light intensity properties. |
| 87 | +
|
| 88 | + :param cast_shadows: POV-Ray light casts shadows |
| 89 | + :param spot_exponent: light spot exponent |
| 90 | + :param spot_cutoff: light spot cutoff |
| 91 | + :param const_atten_factor: light constant attenuation factor, currently not supported |
| 92 | + :param linear_atten_factor: light linear attenuation factor, currently not supported |
| 93 | + :param quad_atten_factor: light quadratic attenuation factor, currently not supported |
| 94 | + """ |
| 95 | + if cast_shadows is not None: |
| 96 | + sim.simSetObjectInt32Parameter( |
| 97 | + self._handle, sim.sim_lightintparam_pov_casts_shadows, int(cast_shadows)) |
| 98 | + if spot_exponent is not None: |
| 99 | + if spot_exponent % 1 != 0: |
| 100 | + warnings.warn('spot exponent must be an integer, rounding input of {} to {}'.format( |
| 101 | + spot_exponent, round(spot_exponent))) |
| 102 | + sim.simSetObjectFloatParameter( |
| 103 | + self._handle, sim.sim_lightfloatparam_spot_exponent, float(round(spot_exponent))) |
| 104 | + if spot_cutoff is not None: |
| 105 | + spot_cutoff = float(spot_cutoff) |
| 106 | + if spot_cutoff > np.pi/2: |
| 107 | + warnings.warn('Tried to set spot_cutoff to {}, but the maximum allowed value is pi/2,' |
| 108 | + 'therefore setting to pi/2'.format(spot_cutoff)) |
| 109 | + sim.simSetObjectFloatParameter( |
| 110 | + self._handle, sim.sim_lightfloatparam_spot_cutoff, float(spot_cutoff)) |
| 111 | + if const_atten_factor is not None: |
| 112 | + raise Exception('CoppeliaSim currently does not support setting attenuation factors') |
| 113 | + # sim.simSetObjectFloatParameter( |
| 114 | + # self._handle, sim.sim_lightfloatparam_const_attenuation, float(const_atten_factor)) |
| 115 | + if linear_atten_factor is not None: |
| 116 | + raise Exception('CoppeliaSim currently does not support setting attenuation factors') |
| 117 | + # sim.simSetObjectFloatParameter( |
| 118 | + # self._handle, sim.sim_lightfloatparam_lin_attenuation, float(linear_atten_factor)) |
| 119 | + if quad_atten_factor is not None: |
| 120 | + raise Exception('CoppeliaSim currently does not support setting attenuation factors') |
| 121 | + # sim.simSetObjectFloatParameter( |
| 122 | + # self._handle, sim.sim_lightfloatparam_quad_attenuation, float(quad_atten_factor)) |
| 123 | + |
| 124 | + |
| 125 | +object_type_to_class[ObjectType.LIGHT] = Light |
0 commit comments