Changeset View
Changeset View
Standalone View
Standalone View
tests/python/modules/mesh_test.py
| Context not available. | |||||
| " with parameters: " + str(self.modifier_parameters) | " with parameters: " + str(self.modifier_parameters) | ||||
| class PhysicsSpec: | |||||
| """ | |||||
| Holds one Physics modifier and its parameters. | |||||
| """ | |||||
| def __init__(self, modifier_name: str, modifier_type: str, modifier_parameters: dict, frame_end: int): | |||||
| """ | |||||
| Constructs a physics spec. | |||||
| :param modifier_name: str - name of object modifier, e.g. "Cloth" | |||||
| :param modifier_type: str - type of object modifier, e.g. "CLOTH" | |||||
| :param modifier_parameters: dict - {name : val} dictionary giving modifier parameters, e.g. {"quality" : 4} | |||||
| :param frame_end:int - the last frame of the simulation at which it is baked | |||||
| """ | |||||
brecht: `frame_number` -> `frame_end` | |||||
Done Inline ActionsModifier -> Physics Modifier brecht: Modifier -> Physics Modifier | |||||
| self.modifier_name = modifier_name | |||||
| self.modifier_type = modifier_type | |||||
| self.modifier_parameters = modifier_parameters | |||||
| self.frame_end = frame_end | |||||
| def __str__(self): | |||||
| return "Physics Modifier: " + self.modifier_name + " of type " + self.modifier_type + \ | |||||
| " with parameters: " + str(self.modifier_parameters) + " with frame end: " + str(self.frame_end) | |||||
| class OperatorSpec: | class OperatorSpec: | ||||
| """ | """ | ||||
| Holds one operator and its parameters. | Holds one operator and its parameters. | ||||
Done Inline ActionsCode style: no empty line right after def line. brecht: Code style: no empty line right after `def` line. | |||||
Done Inline ActionsTwo empty lines still here. brecht: Two empty lines still here. | |||||
| Context not available. | |||||
| if self.apply_modifier: | if self.apply_modifier: | ||||
| bpy.ops.object.modifier_apply(modifier=modifier_spec.modifier_name) | bpy.ops.object.modifier_apply(modifier=modifier_spec.modifier_name) | ||||
| def bake_current_simulation(self, obj, test_mod_type, test_mod_name, frame_end): | |||||
| for scene in bpy.data.scenes: | |||||
| for modifier in obj.modifiers: | |||||
| if modifier.type == test_mod_type: | |||||
| obj.modifiers[test_mod_name].point_cache.frame_end = frame_end | |||||
| override = {'scene': scene, 'active_object': obj, 'point_cache': modifier.point_cache} | |||||
| bpy.ops.ptcache.bake(override, bake=True) | |||||
Done Inline ActionsAvoid using operators when possible, this can probably be scene.frame_set or similar? It's not even clear to me what this line does, which frame is it supposed to jump to? brecht: Avoid using operators when possible, this can probably be `scene.frame_set` or similar?
It's… | |||||
| break | |||||
| def _apply_physics_settings(self, test_object, physics_spec: PhysicsSpec): | |||||
| """ | |||||
| Apply Physics settings to test objects. | |||||
| """ | |||||
| scene = bpy.context.scene | |||||
| scene.frame_set(1) | |||||
| modifier = test_object.modifiers.new(physics_spec.modifier_name, | |||||
Done Inline ActionsCode style: use only one empty line as separator inside functions. brecht: Code style: use only one empty line as separator inside functions. | |||||
| physics_spec.modifier_type) | |||||
| physics_setting = modifier.settings | |||||
| if self.verbose: | |||||
| print("Created modifier '{}' of type '{}'.". | |||||
| format(physics_spec.modifier_name, physics_spec.modifier_type)) | |||||
| for param_name in physics_spec.modifier_parameters: | |||||
| try: | |||||
| setattr(physics_setting, param_name, physics_spec.modifier_parameters[param_name]) | |||||
| if self.verbose: | |||||
| print("\t set parameter '{}' with value '{}'". | |||||
| format(param_name, physics_spec.modifier_parameters[param_name])) | |||||
| except AttributeError: | |||||
| # Clean up first | |||||
Done Inline ActionsThis can also just call scene.frame_set? brecht: This can also just call `scene.frame_set`? | |||||
| bpy.ops.object.delete() | |||||
| raise AttributeError("Modifier '{}' has no parameter named '{}'". | |||||
Not Done Inline ActionsEasier to just pass the physics_spec entirely to this function. brecht: Easier to just pass the `physics_spec` entirely to this function. | |||||
Done Inline ActionsAccessing using physics_spec feels unnecessarily long. calra: Accessing using `physics_spec` feels unnecessarily long. | |||||
| format(physics_spec.modifier_type, param_name)) | |||||
| scene.frame_set(physics_spec.frame_end+1) | |||||
| self.bake_current_simulation(test_object, physics_spec.modifier_type, physics_spec.modifier_name, physics_spec.frame_end) | |||||
| if self.apply_modifier: | |||||
| bpy.ops.object.modifier_apply(modifier=physics_spec.modifier_name) | |||||
| def _apply_operator(self, test_object, operator: OperatorSpec): | def _apply_operator(self, test_object, operator: OperatorSpec): | ||||
| """ | """ | ||||
| Apply operator on test object. | Apply operator on test object. | ||||
| Context not available. | |||||
| elif isinstance(operation, OperatorSpec): | elif isinstance(operation, OperatorSpec): | ||||
| self._apply_operator(evaluated_test_object, operation) | self._apply_operator(evaluated_test_object, operation) | ||||
| elif isinstance(operation, PhysicsSpec): | |||||
| self._apply_physics_settings(evaluated_test_object, operation) | |||||
| else: | else: | ||||
| raise ValueError("Expected operation of type {} or {}. Got {}". | raise ValueError("Expected operation of type {} or {} or {}. Got {}". | ||||
| format(type(ModifierSpec), type(OperatorSpec), | format(type(ModifierSpec), type(OperatorSpec), type(PhysicsSpec), | ||||
| type(operation))) | type(operation))) | ||||
| # Compare resulting mesh with expected one. | # Compare resulting mesh with expected one. | ||||
| Context not available. | |||||
frame_number -> frame_end