Changeset View
Standalone View
tests/python/modules/test_object_generator.py
- This file was added.
| import bpy | |||||
| import re | |||||
| import os | |||||
| import sys | |||||
| offset_x = 5 | |||||
| def validating_user_input(mesh, vert_set): | |||||
| for i in vert_set: | |||||
| if not isinstance(i, int) or i <= 0: | |||||
| raise Exception("Please enter a valid index(integer).") | |||||
| max_vert = len(mesh.data.vertices) | |||||
| for i in vert_set: | |||||
| if i > max_vert-1: | |||||
| raise Exception("Index is greater than maximum number of vertices.") | |||||
zazizizou: This is redundant. You already have max value so just check if max is larger than the allowed… | |||||
| def do_selection(mesh, vert_set: set): | |||||
zazizizouUnsubmitted Done Inline Actionsthe function is called do_selection but it also assigns vertex group? You should avoid this for two reasons:
zazizizou: the function is called do_selection but it also assigns vertex group? You should avoid this for… | |||||
| bpy.ops.object.mode_set(mode='EDIT') | |||||
| bpy.ops.mesh.select_all(action='DESELECT') | |||||
| bpy.ops.object.mode_set(mode='OBJECT') | |||||
| for index in vert_set: | |||||
| mesh.data.vertices[index].select = True | |||||
| bpy.ops.object.mode_set(mode='EDIT') | |||||
| bpy.ops.object.vertex_group_assign() | |||||
| bpy.ops.object.mode_set(mode='OBJECT') | |||||
Done Inline ActionsIt looks like you are passing an object and not a mesh, so argument name seems wrong... zazizizou: It looks like you are passing an object and not a mesh, so argument name seems wrong... | |||||
Not Done Inline ActionsYou were right I think, object is a keyword in Python and it's probably best to choose something else. zazizizou: You were right I think, `object` is a keyword in Python and it's probably best to choose… | |||||
| def create_vertex_group(obj_name, vg_name, vg_vert_set): | |||||
zazizizouUnsubmitted Done Inline Actionswhat if the object does not exist? zazizizou: what if the object does not exist? | |||||
| mesh = bpy.data.objects[obj_name] | |||||
| mesh.vertex_groups.new(name=vg_name) | |||||
Done Inline ActionsUse True/False instead zazizizou: Use `True`/`False` instead | |||||
| vert_set = vg_vert_set | |||||
| validating_user_input(mesh, vert_set) | |||||
zazizizouUnsubmitted Done Inline ActionsYou should validate function input before doing anything, otherwise the function exists with an exception with the job half way done. You also probably don't need an extra function to validate input. Just write the validation code at the beginning of this function zazizizou: You should validate function input before doing anything, otherwise the function exists with an… | |||||
| do_selection(mesh, vert_set) | |||||
| def get_last_location(): | |||||
| farthest = 0 | |||||
| all_y_locs = [] | |||||
| for obj in bpy.data.objects: | |||||
| all_y_locs.append(obj.location.y) | |||||
| for y in all_y_locs: | |||||
| if y > farthest: | |||||
| farthest = y | |||||
| return farthest | |||||
| def name_test_object(obj_name): | |||||
| bpy.context.active_object.name = "testObj" + obj_name | |||||
| def name_exp_object(obj_name): | |||||
| bpy.context.active_object.name = "expObj" + obj_name | |||||
zazizizouUnsubmitted Done Inline ActionsNot sure why you need a function here, you are wrapping only 1 line? zazizizou: Not sure why you need a function here, you are wrapping only 1 line? | |||||
| def create_test_objects(obj_dict): | |||||
| offset_y = get_last_location() + 5 | |||||
| for obj_name, obj_type in obj_dict.items(): | |||||
| print("Object type:", obj_type, "\nObject name:", obj_name) | |||||
| test_obj_name = "testObj" + obj_name | |||||
| exp_obj_name = "expObj" + obj_name | |||||
| if test_obj_name not in bpy.data.objects.keys(): | |||||
| if obj_type == "Circle": | |||||
| bpy.ops.mesh.primitive_circle_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_circle_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
zazizizouUnsubmitted Done Inline ActionsYou don't have to duplicate this code for every obj_type, just write it once after all the if statments zazizizou: You don't have to duplicate this code for every obj_type, just write it once after all the if… | |||||
| elif obj_type == "Cube": | |||||
| bpy.ops.mesh.primitive_cube_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_cube_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Plane": | |||||
| bpy.ops.mesh.primitive_plane_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_plane_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Sphere": | |||||
| bpy.ops.mesh.primitive_uv_sphere_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_uv_sphere_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Cone": | |||||
| bpy.ops.mesh.primitive_cone_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_cone_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Cylinder": | |||||
| bpy.ops.mesh.primitive_cylinder_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_cylinder_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Icosphere": | |||||
| bpy.ops.mesh.primitive_ico_sphere_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_ico_sphere_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Torus": | |||||
| bpy.ops.mesh.primitive_torus_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_torus_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Monkey": | |||||
| bpy.ops.mesh.primitive_monkey_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_monkey_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| elif obj_type == "Grid": | |||||
| bpy.ops.mesh.primitive_grid_add(location=(0, offset_y, 0)) | |||||
| name_test_object(obj_name) | |||||
| bpy.ops.mesh.primitive_grid_add(location=(offset_x, offset_y, 0)) | |||||
| name_exp_object(obj_name) | |||||
| offset_y += 5 | |||||
| else: | |||||
| print("Object already present.") | |||||
| pattern = re.compile(r"[A-Z]:?[\\\/].*\.blend|[\\\/].*\.blend") | |||||
Done Inline ActionsThis is one more reason why a class would be better here, where you save all internal states within the class instead of having to declare global variables. Also, flag is not descriptive enough. Something like success is more commonly used zazizizou: This is one more reason why a class would be better here, where you save all internal states… | |||||
| args = list(sys.argv) | |||||
| print(args) | |||||
| for cmd in args: | |||||
| mo = pattern.search(cmd) | |||||
| if mo is not None: | |||||
| path_to_file = mo.group() | |||||
zazizizouUnsubmitted Done Inline ActionsThis has to be removed. The script must have a well defined list of arguments. The user as well as the underlying function can 'agree' on what the arguments mean beforehand. Ideally you should print a help message about how the script is supposed to be used, what it does and so on... zazizizou: This has to be removed. The script must have a well defined list of arguments. The user as well… | |||||
| print(path_to_file) | |||||
| if os.path.exists(path_to_file): | |||||
| bpy.ops.wm.open_mainfile(filepath=path_to_file) | |||||
| else: | |||||
| bpy.ops.wm.save_as_mainfile(filepath=path_to_file) | |||||
zazizizouUnsubmitted Done Inline ActionsThe file will get created even when there are errors later on. Is this intentional? It's probably best to either create a file that has been generated correctly or don't create anything at all, right? zazizizou: The file will get created even when there are errors later on. Is this intentional?
It's… | |||||
| bpy.ops.object.select_all(action='SELECT') | |||||
| bpy.ops.object.delete(use_global=False) | |||||
| create_test_objects({'Cube': 'Cube', 'Cy1': "Cylinder"}) | |||||
Not Done Inline Actionsis conversion to str really necessary? I thought it could work without it.. zazizizou: is conversion to str really necessary? I thought it could work without it.. | |||||
Done Inline Actionsyes calra: yes | |||||
| create_vertex_group('Cube', "vg_solidify7", {1, 2, 3, 4}) | |||||
| bpy.ops.wm.save_mainfile(filepath=path_to_file) | |||||
Done Inline ActionsDoes this really work? flag is set inside the function create_test_objects() but it only gets checked before that function is called... zazizizou: Does this really work? `flag` is set inside the function `create_test_objects()` but it only… | |||||
Done Inline ActionsThis one's extra, and will be removed in the next commit. calra: This one's extra, and will be removed in the next commit. | |||||
This is redundant. You already have max value so just check if max is larger than the allowed maximum. You can do the same for negative values with min(..)