import bpy import os import json import sys import datetime from ..project_config import PROJECT_CONFIG sys.path.append(PROJECT_CONFIG.executable_dir_path) sys.path.append(PROJECT_CONFIG.library_dir_path) sys.path.append(PROJECT_CONFIG.workplace_dir_path) supported_family = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] class LatticeLibraryOperator(bpy.types.Operator): bl_idname: str = "designauto.lattice_library" bl_label: str = "纹理库" bl_options = {"REGISTER", "UNDO"} family_items = [("-1","全部纹理","")] for family in supported_family : family_items.append((str(family),"纹理" + str(family),"")) lattice_family: bpy.props.EnumProperty(name = "lattice_family", items=family_items) lattice_type : bpy.props.StringProperty(name = "lattice") start_id = 1 end_id = 61 out_type = -1 cur_dir = os.path.dirname(os.path.abspath(__file__)) line3valid_file_path = os.path.join(PROJECT_CONFIG.workplace_dir_path, "parameters", "line3_valid.json") def invoke(self, context, event): wm = context.window_manager return wm.invoke_props_dialog(self) def draw(self, context): layout = self.layout layout.ui_units_x = 40 row = layout.row() # row.prop(self,"lattice_type",text="Type") row.prop(self,"lattice_family",text="相连性类别") button = row.operator("designauto.search_lattice",text="查询纹理") button.lattice_family = self.lattice_family # string row = layout.row() button = row.operator("designauto.edit_lattice_para",text="修改生成纹理参数") box =layout.box() box.ui_units_y = 20 row = box.row() #从数据库中取的晶格列表 lattices_id_list=self.getFromJson(self.start_id,self.end_id,self.out_type) #i记录一行中的按钮数 i = 0 for lattice_info in lattices_id_list: name = "Type " + str(lattice_info['id']) button = row.operator("designauto.load_obj",text = name) real_obj_path = os.path.join(PROJECT_CONFIG.workplace_dir_path, "parameters", lattice_info['obj_path']) button.obj_path = real_obj_path #五个一行,超过换行 i = i + 1 if i % 5 == 0 : col =box.column() row = col.row() row = box.row() row = box.row() row.operator("designauto.pre_page",text="上一页") row.operator("designauto.nex_page",text="下一页") def getFromJson(self, id_start, id_end, out_type): lattices_list = [] with open(self.line3valid_file_path, encoding='utf-8') as f: all_lattices = json.load(f) for lattice in all_lattices: if (out_type == -1) or (lattice.get("out_type") == str(out_type)): lattices_list.append({ 'id': lattice.get("id"), 'obj_path': lattice.get("obj_path") }) return lattices_list[id_start-1:id_end-1] class LoadOBJOperator(bpy.types.Operator): bl_idname: str = "designauto.load_obj" bl_label: str = "加载纹理" bl_options = {"REGISTER", "UNDO"} obj_path: bpy.props.StringProperty(name="Object Path") def execute(self, context): bpy.ops.import_scene.obj(filepath=self.obj_path) return {'FINISHED'} class PrePageOperator(bpy.types.Operator): bl_idname: str = "designauto.pre_page" bl_label: str = "上一页" bl_options = {"REGISTER", "UNDO"} def execute(self, context): LatticeLibraryOperator.start_id -= 60 LatticeLibraryOperator.end_id -= 60 return {'FINISHED'} def invoke(self, context, event): return self.execute(self) class NexPageOperator(bpy.types.Operator): bl_idname: str = "designauto.nex_page" bl_label: str = "下一页" bl_options = {"REGISTER", "UNDO"} def execute(self, context): LatticeLibraryOperator.start_id += 60 LatticeLibraryOperator.end_id += 60 return {'FINISHED'} def invoke(self, context, event): return self.execute(self) class EditLatticeParaOperator(bpy.types.Operator): bl_idname: str = "designauto.edit_lattice_para" bl_label: str = "修改纹理参数" bl_options = {"REGISTER", "UNDO"} radius : bpy.props.FloatProperty(name="半径",default=0.02,min=0.01,max=0.09,step=0.01) offset : bpy.props.FloatProperty(name="深度",default=0.5,min=0.1,max=0.9,step=0.1) sample : bpy.props.FloatProperty(name="凹凸",default=0.8,min=0.1,max=1,step=0.1) def execute(self, context): path = LatticeLibraryOperator.cur_dir + "/user_setting.txt" with open(path,'w') as f: f.truncate() round(self.radius,2) radius_lines = str(round(self.radius,2))+",lattice_radius;\n" offset_lines = str(round(self.offset,2))+",lattice_offset;\n" sample_lines = str(self.sample)+",lattice_sample;\n" txtlist = [radius_lines,offset_lines,sample_lines] f.writelines(txtlist) return {'FINISHED'} def invoke(self, context, event): path = LatticeLibraryOperator.cur_dir + "/user_setting.txt" with open(path,'r') as f: lines=f.readlines() self.radius = float(lines[0].split(",")[0]) self.offset = float(lines[1].split(",")[0]) self.sample = int(lines[2].split(",")[0]) wm = context.window_manager return wm.invoke_props_dialog(self) class SearchLatticeOperator(bpy.types.Operator): bl_idname: str = "designauto.search_lattice" bl_label: str = "搜索纹理" bl_options = {"REGISTER", "UNDO"} lattice_family : bpy.props.StringProperty(name = 'family') def execute(self, context): LatticeLibraryOperator.start_id = 1 LatticeLibraryOperator.end_id = 61 LatticeLibraryOperator.out_type = int(self.lattice_family) return {'FINISHED'} def invoke(self, context, event): return self.execute(self)