def run(self): """ 'Selects' objects and sets according values for defined attributes/custom properties.""" instances = self.config.get_list("instances", []) for instance in instances: # separating defined part with the selector from ambiguous part with attribute names and their values to set set_params = {} sel_objs = {} for key in instance.keys(): # if its not a selector -> to the set parameters dict if key != 'selector': set_params[key] = instance[key] else: sel_objs[key] = instance[key] # create Config objects params_conf = Config(set_params) sel_conf = Config(sel_objs) # invoke a Getter, get a list of objects to manipulate objects = sel_conf.get_list("selector") for key in params_conf.data.keys(): # get raw value from the set parameters config object result = params_conf.get_raw_value(key) for obj in objects: # if an attribute with such name exists for this object if hasattr(obj, key): # set the value setattr(obj, key, result) # if not, then treat it as a custom property. Values will be overwritten for existing custom # property, but if the name is new then new custom property will be created else: obj[key] = result
def run(self): """ Returns the result of processing of the list of values. """ transform_by = self.config.get_string("transform_by") elements = self.config.get_list("elements") raw_result = [] for element in elements: element_conf = Config({"element": element}) if isinstance(element, list): raw_result.append(element_conf.get_vector3d("element")) else: raw_result.append(element_conf.get_raw_value("element")) if len(raw_result) > 0: if self._check_compatibility(raw_result): if transform_by == "sum": ref_result = self._sum(raw_result) elif transform_by == "avg": ref_result = self._avg(raw_result) else: raise RuntimeError("Unknown 'transform_by' operation: " + transform_by) else: raise RuntimeError( "Provider output types don't match. All must either int, float, or mathutils.Vector." ) else: raise RuntimeError( "List of resulting values of `elements` is empty. Please, check Provider configuration." ) return ref_result
def _get_the_set_params(self, params_conf: Config) -> dict: """ Extracts actual values to set from a Config object. :param params_conf: Object with all user-defined data. :return: Parameters to set as {name of the parameter: it's value} pairs. """ params = {} for key in params_conf.data.keys(): result = None if key == "cf_color_link_to_displacement": result = params_conf.get_float(key) elif key == "cf_change_to_vertex_color": result = params_conf.get_string(key) elif key == "cf_textures": result = {} paths_conf = Config(params_conf.get_raw_dict(key)) for text_key in paths_conf.data.keys(): text_path = paths_conf.get_string(text_key) result.update({text_key: text_path}) elif key == "cf_switch_to_emission_shader": result = {} emission_conf = Config(params_conf.get_raw_dict(key)) for emission_key in emission_conf.data.keys(): if emission_key == "color": attr_val = emission_conf.get_list( "color", [1, 1, 1, 1]) elif emission_key == "strength": attr_val = emission_conf.get_float("strength", 1.0) else: attr_val = emission_conf.get_raw_value(emission_key) result.update({emission_key: attr_val}) elif key == "cf_infuse_texture": result = Config(params_conf.get_raw_dict(key)) elif key == "cf_infuse_material": result = Config(params_conf.get_raw_dict(key)) elif key == "cf_add_dust": result = params_conf.get_raw_dict(key) elif "cf_set_" in key or "cf_add_" in key: result = params_conf.get_raw_value(key) else: result = params_conf.get_raw_value(key) params.update({key: result}) return params
def run(self): """ 'Selects' entities and sets according values for defined attributes/custom properties.""" # separating defined part with the selector from ambiguous part with attribute names and their values to set set_params = {} sel_objs = {} for key in self.config.data.keys(): # if its not a selector -> to the set parameters dict if key != 'selector': set_params[key] = self.config.data[key] else: sel_objs[key] = self.config.data[key] # create Config objects params_conf = Config(set_params) sel_conf = Config(sel_objs) # invoke a Getter, get a list of entities to manipulate entities = sel_conf.get_list("selector") op_mode = self.config.get_string("mode", "once_for_each") for key in params_conf.data.keys(): # get raw value from the set parameters if it is to be sampled once for all selected entities if op_mode == "once_for_all": result = params_conf.get_raw_value(key) for entity in entities: if op_mode == "once_for_each": # get raw value from the set parameters if it is to be sampled anew for each selected entity result = params_conf.get_raw_value(key) # check if the key is a requested custom property requested_custom_property = False if key.startswith('cp_'): requested_custom_property = True key = key[3:] # if an attribute with such name exists for this entity if hasattr(entity, key) and not requested_custom_property: # set the value setattr(entity, key, result) # if not, then treat it as a custom property. Values will be overwritten for existing custom # property, but if the name is new then new custom property will be created else: entity[key] = result # update all entities matrices bpy.context.view_layer.update()
def run(self): """ Sets according values of defined attributes/custom properties or applies custom functions to the selected entities. 1. Select objects. 2. For each parameter to modify, set it's value to all selected objects. """ # separating defined part with the selector from ambiguous part with attribute names and their values to set set_params = {} sel_objs = {} for key in self.config.data.keys(): # if its not a selector -> to the set parameters dict if key != 'selector': set_params[key] = self.config.data[key] else: sel_objs[key] = self.config.data[key] # create Config objects params_conf = Config(set_params) sel_conf = Config(sel_objs) # invoke a Getter, get a list of entities to manipulate entities = sel_conf.get_list("selector") op_mode = self.config.get_string("mode", "once_for_each") if len(entities) == 0: raise RuntimeError("No objects are returned by Provider. Check defined conditions.") else: print("Amount of objects to modify: {}.".format(len(entities))) for key in params_conf.data.keys(): # get raw value from the set parameters if it is to be sampled once for all selected entities if op_mode == "once_for_all": result = params_conf.get_raw_value(key) for entity in entities: if op_mode == "once_for_each": # get raw value from the set parameters if it is to be sampled anew for each selected entity result = params_conf.get_raw_value(key) # used so we don't modify original key when having more than one entity key_copy = key # check if the key is a requested custom property demanded_custom_property = False if key.startswith('cp_'): demanded_custom_property = True key_copy = key[3:] demanded_custom_function = False if key.startswith('cf_'): demanded_custom_function = True key_copy = key[3:] # if an attribute with such name exists for this entity if hasattr(entity, key_copy) and not demanded_custom_property: # set the value setattr(entity, key_copy, result) # if key had a cf_ prefix - treat it as a custom function. elif demanded_custom_function: self._apply_function(entity, key_copy, result) # if key had a cp_ prefix - treat it as a custom property. Values will be overwritten for existing # custom property, but if the name is new then new custom property will be created elif demanded_custom_property: entity[key_copy] = result # update all entities matrices bpy.context.view_layer.update()