From 79a04f14967bef68f711dd79655b407cc3682eb8 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Thu, 3 Sep 2020 13:57:06 +0800 Subject: [PATCH] finish add elements --- ballance_blender_plugin/__init__.py | 6 +- ballance_blender_plugin/add_elements.py | 94 +++++++-------------- ballance_blender_plugin/bm_import_export.py | 1 + ballance_blender_plugin/utils.py | 3 +- 4 files changed, 36 insertions(+), 68 deletions(-) diff --git a/ballance_blender_plugin/__init__.py b/ballance_blender_plugin/__init__.py index f42df21..b3b251c 100644 --- a/ballance_blender_plugin/__init__.py +++ b/ballance_blender_plugin/__init__.py @@ -60,8 +60,7 @@ classes = ( no_uv_checker.BALLANCE_OT_no_uv_checker, BALLANCE_MT_ThreeDViewerMenu, - add_elements.BALLANCE_OT_add_sector_related_elements, - add_elements.BALLANCE_OT_add_unique_elements, + add_elements.BALLANCE_OT_add_elements, add_elements.BALLANCE_OT_add_rail ) @@ -76,8 +75,7 @@ def menu_func_ballance_add(self, context): layout = self.layout layout.separator() layout.label(text="Ballance") - layout.operator_menu_enum("ballance.add_sector_related_elements", "elements_type", icon='MESH_ICOSPHERE', text="Normal elements") - layout.operator_menu_enum("ballance.add_unique_elements", "elements_type", icon='MESH_ICOSPHERE', text="Unique elements") + layout.operator_menu_enum("ballance.add_elements", "elements_type", icon='MESH_ICOSPHERE', text="Elements") layout.operator("ballance.add_rail", icon='MESH_CUBE', text="Rail section") def register(): diff --git a/ballance_blender_plugin/add_elements.py b/ballance_blender_plugin/add_elements.py index 61bb3d0..bf41234 100644 --- a/ballance_blender_plugin/add_elements.py +++ b/ballance_blender_plugin/add_elements.py @@ -1,53 +1,23 @@ import bpy,mathutils -from . import utils - -sectorRelatedElements = [ - "P_Extra_Life", - "P_Extra_Point", - "P_Trafo_Paper", - "P_Trafo_Stone", - "P_Trafo_Wood", - "P_Ball_Paper", - "P_Ball_Stone", - "P_Ball_Wood", - "P_Box", - "P_Dome", - "P_Modul_01", - "P_Modul_03", - "P_Modul_08", - "P_Modul_17", - "P_Modul_18", - "P_Modul_19", - "P_Modul_25", - "P_Modul_26", - "P_Modul_29", - "P_Modul_30", - "P_Modul_34", - "P_Modul_37", - "P_Modul_41", - "PR_Resetpoint", - "PC_TwoFlames" -] - -uniqueElements = [ - "PE_Balloon", - "PS_FourFlames" -] +from . import utils, config, bm_import_export # ================================================= actual add -class BALLANCE_OT_add_sector_related_elements(bpy.types.Operator): +class BALLANCE_OT_add_elements(bpy.types.Operator): """Add sector related elements""" - bl_idname = "ballance.add_sector_related_elements" - bl_label = "Add normal elements" + bl_idname = "ballance.add_elements" + bl_label = "Add elements" bl_options = {'UNDO'} elements_type: bpy.props.EnumProperty( name="Type", description="This element type", - items=tuple(map(lambda x: (x, x, ""), sectorRelatedElements)), + items=tuple(map(lambda x: (x, x, ""), config.component_list)), ) + attentionElements = ["PC_TwoFlames", "PR_Resetpoint"] + uniqueElements = ["PS_FourFlames", "PE_Balloon"] + elements_sector: bpy.props.IntProperty( name="Sector", description="Define which sector the object will be grouped in", @@ -57,6 +27,17 @@ class BALLANCE_OT_add_sector_related_elements(bpy.types.Operator): ) def execute(self, context): + # get name + if self.elements_type in self.uniqueElements: + finalObjectName = self.elements_type + "_01" + else: + finalObjectName = self.elements_type + "_0" + str(self.elements_sector) + "_" + + # create object + loadedMesh = bm_import_export.load_component(config.component_list.index(self.elements_type)) + obj = bpy.data.objects.new(finalObjectName, loadedMesh) + addSceneAndChangePos(obj) + return {'FINISHED'} def invoke(self, context, event): @@ -66,30 +47,10 @@ class BALLANCE_OT_add_sector_related_elements(bpy.types.Operator): def draw(self, context): layout = self.layout layout.prop(self, "elements_type") - layout.prop(self, "elements_sector") - -class BALLANCE_OT_add_unique_elements(bpy.types.Operator): - """Add unique elements""" - bl_idname = "ballance.add_unique_elements" - bl_label = "Add unique elements" - bl_options = {'UNDO'} - - elements_type: bpy.props.EnumProperty( - name="Type", - description="This element type", - items=tuple(map(lambda x: (x, x, ""), uniqueElements)), - ) - - def execute(self, context): - return {'FINISHED'} - - def invoke(self, context, event): - wm = context.window_manager - return wm.invoke_props_dialog(self) - - def draw(self, context): - layout = self.layout - layout.prop(self, "elements_type") + if self.elements_type not in self.uniqueElements: + layout.prop(self, "elements_sector") + if self.elements_type in self.attentionElements: + layout.label(text="Please note that sector is suffix.") class BALLANCE_OT_add_rail(bpy.types.Operator): """Add rail""" @@ -128,4 +89,11 @@ class BALLANCE_OT_add_rail(bpy.types.Operator): layout = self.layout layout.prop(self, "rail_type") layout.prop(self, "rail_radius") - layout.prop(self, "rail_span") \ No newline at end of file + layout.prop(self, "rail_span") + +def addSceneAndChangePos(obj): + obj.matrix_world = bpy.context.scene.cursor.matrix + + view_layer = bpy.context.view_layer + collection = view_layer.active_layer_collection.collection + collection.objects.link(obj) \ No newline at end of file diff --git a/ballance_blender_plugin/bm_import_export.py b/ballance_blender_plugin/bm_import_export.py index e3ee7f5..93ba4e8 100644 --- a/ballance_blender_plugin/bm_import_export.py +++ b/ballance_blender_plugin/bm_import_export.py @@ -622,6 +622,7 @@ class info_block_helper(): self.offset = offset self.blenderData = None +# NOTE: this function also used by add_elements.py def load_component(component_id): # get file first compName = config.component_list[component_id] diff --git a/ballance_blender_plugin/utils.py b/ballance_blender_plugin/utils.py index 1e247dd..545dd66 100644 --- a/ballance_blender_plugin/utils.py +++ b/ballance_blender_plugin/utils.py @@ -1,8 +1,9 @@ import bpy +from bpy_extras.io_utils import unpack_list def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'): def draw(self, context): self.layout.label(text=message) - bpy.context.window_manager.popup_menu(draw, title = title, icon = icon) \ No newline at end of file + bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)