def _render_sunskylight(self, name, view): """Get a rendering string for a sunsky light object. This method follows EAFP idiom and will raise exceptions if something goes wrong (missing attribute, inconsistent data...). Parameters: name -- the name of the sunsky light view -- the view of the sunsky light (contains the light data) Returns: a rendering string, obtained from the renderer module """ debug("SunskyLight", name, "Processing") src = view.Source direction = App.Vector(src.SunDirection) turbidity = float(src.Turbidity) # Distance from the sun: distance = App.Units.parseQuantity("151000000 km").Value assert turbidity >= 0,\ translate("Render", "Negative turbidity") assert direction.Length,\ translate("Render", "Sun direction is null") return self._call_renderer("write_sunskylight", name, direction, distance, turbidity)
def _render_arealight(self, name, view): """Get a rendering string for an area light object. This method follows EAFP idiom and will raise exceptions if something goes wrong (missing attribute, inconsistent data...) Parameters: name -- the name of the area light view -- the view of the area light (contains the light data) Returns: a rendering string, obtained from the renderer module """ debug("AreaLight", name, "Processing") # Get properties source = view.Source placement = App.Base.Placement(source.Placement) color = source.Color power = float(source.Power) size_u = float(source.SizeU) size_v = float(source.SizeV) # Send everything to renderer module return self._call_renderer("write_arealight", name, placement, size_u, size_v, color, power)
def _render_pointlight(self, name, view): """Get a rendering string for a point light object. This method follows EAFP idiom and will raise exceptions if something goes wrong (missing attribute, inconsistent data...). Parameters: name -- the name of the point light view -- the view of the point light (contains the light data) Returns: a rendering string, obtained from the renderer module """ debug("PointLight", name, "Processing") source = view.Source # get location, color location = App.Base.Vector(source.Location) color = source.Color # we accept missing Power (default value: 60)... power = getattr(source, "Power", 60) # send everything to renderer module return self._call_renderer("write_pointlight", name, location, color, power)
def _render_imagelight(self, name, view): """Get a rendering string for an image light object. This method follows EAFP idiom and will raise exceptions if something goes wrong (missing attribute, inconsistent data...). Parameters: name -- the name of the image light view -- the view of the image light (contains the light data) Returns: a rendering string, obtained from the renderer module """ debug("ImageLight", name, "Processing") src = view.Source image = src.ImageFile return self._call_renderer("write_imagelight", name, image)
def _render_object(self, name, view): """Get a rendering string for a generic FreeCAD object. This method follows EAFP idiom and will raise exceptions if something goes wrong (missing attribute, inconsistent data...). Parameters: name -- the name of the object view -- a view of the object to render Returns: a rendering string, obtained from the renderer module """ source = view.Source label = getattr(source, "Label", name) debug("Object", label, "Processing") # Build a list of renderables from the object material = view.Material mesher = functools.partial(MeshPart.meshFromShape, LinearDeflection=self.linear_deflection, AngularDeflection=self.angular_deflection, Relative=False) tpboost = self.transparency_boost rends = renderables.get_renderables(source, name, material, mesher, transparency_boost=tpboost) # Check renderables renderables.check_renderables(rends) # Call renderer on renderables, concatenate and return write_object = functools.partial(RendererHandler._call_renderer, self, "write_object") get_mat = rendermaterials.get_rendering_material rdrname = self.renderer_name res = [ write_object(r.name, r.mesh, get_mat(r.material, rdrname, r.defcolor)) for r in rends ] return ''.join(res)
def get_renderables(obj, name, upper_material, mesher, **kwargs): """Get the renderables from a FreeCAD object. A renderable is a tuple (name, mesh, material). There can be several renderables for one object, for instance if the object is a compound of subobjects, so the result of this function is a **list** of renderables. If this function does not know how to extract renderables from the given object, a TypeError is raised Parameters: obj -- the FreeCAD object from which to extract the renderables name -- the name of the object upper_material -- the FreeCAD material inherited from the upper level mesher -- a callable which can convert a shape into a mesh Keywords arguments: ignore_unknown -- a flag to prevent exception raising if 'obj' is of no renderable type transparency_boost -- a factor (positive integer) to boost transparency in shape color Returns: A list of renderables """ obj_is_applink = obj.isDerivedFrom("App::Link") obj_is_partfeature = obj.isDerivedFrom("Part::Feature") obj_is_meshfeature = obj.isDerivedFrom("Mesh::Feature") obj_is_app_part = obj.isDerivedFrom("App::Part") obj_type = getproxyattr(obj, "Type", "") mat = (getattr(obj, "Material", None) if upper_material is None else upper_material) mat = mat if is_valid_material(mat) or is_multimat(mat) else None del upper_material # Should not be used after this point... label = getattr(obj, "Label", name) ignore_unknown = bool(kwargs.get("ignore_unknown", False)) transparency_boost = int(kwargs.get("transparency_boost", 0)) # Link (plain) if obj_is_applink and not obj.ElementCount: debug("Object", label, "'Link (plain)' detected") renderables = \ _get_rends_from_plainapplink(obj, name, mat, mesher, **kwargs) # Link (array) elif obj_is_applink and obj.ElementCount: debug("Object", label, "'Link (array)' detected") renderables = \ _get_rends_from_elementlist(obj, name, mat, mesher, **kwargs) # Array, PathArray elif obj_is_partfeature and obj_type in ("Array", "PathArray"): debug("Object", label, "'%s' detected" % obj_type) expand_array = getattr(obj, "ExpandArray", False) renderables = (_get_rends_from_array(obj, name, mat, mesher, **kwargs) if not expand_array else _get_rends_from_elementlist( obj, name, mat, mesher, **kwargs)) # Window elif obj_is_partfeature and obj_type == "Window": debug("Object", label, "'Window' detected") renderables = _get_rends_from_window(obj, name, mat, mesher, **kwargs) # Wall elif obj_is_partfeature and obj_type == "Wall": debug("Object", label, "'Wall' detected") renderables = _get_rends_from_wall(obj, name, mat, mesher, **kwargs) # App part elif obj_is_app_part: debug("Object", label, "'App::Part' detected") renderables = _get_rends_from_part(obj, name, mat, mesher, **kwargs) # Plain part feature elif obj_is_partfeature: debug("Object", label, "'Part::Feature' detected") renderables = _get_rends_from_feature(obj, name, mat, mesher, **kwargs) # Mesh elif obj_is_meshfeature: debug("Object", label, "'Mesh::Feature' detected") color = _get_shapecolor(obj, transparency_boost) renderables = [Renderable(name, obj.Mesh, mat, color)] # Unhandled else: renderables = [] if not ignore_unknown: ascendants = ", ".join(obj.getAllDerivedFrom()) msg = translate("Render", "Unhandled object type (%s)" % ascendants) raise TypeError(msg) debug("Object", label, "Not renderable") return renderables