2022-04-03 22:48:12 +08:00
|
|
|
import bpy, mathutils
|
2023-01-30 11:12:15 +08:00
|
|
|
from . import UTILS_constants, UTILS_functions, UTILS_icons_manager
|
2022-04-03 22:48:12 +08:00
|
|
|
|
2023-01-30 22:49:49 +08:00
|
|
|
# =============== Common Class ================
|
|
|
|
class common_add_component_props(bpy.types.Operator):
|
|
|
|
attentionElements = ("PC_TwoFlames", "PR_Resetpoint")
|
|
|
|
uniqueElements = ("PS_FourFlames", "PE_Balloon")
|
2022-04-03 22:48:12 +08:00
|
|
|
|
2023-01-30 22:49:49 +08:00
|
|
|
elements_sector: bpy.props.IntProperty(
|
|
|
|
name="Sector",
|
|
|
|
description="Define which sector the object will be grouped in",
|
|
|
|
min=1, max=8,
|
|
|
|
default=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_component_name(self, raw_comp_name):
|
|
|
|
if raw_comp_name in self.uniqueElements:
|
|
|
|
return raw_comp_name + "_01"
|
|
|
|
elif raw_comp_name in self.attentionElements:
|
|
|
|
return raw_comp_name + "_0" + str(self.elements_sector)
|
|
|
|
else:
|
|
|
|
return raw_comp_name + "_0" + str(self.elements_sector) + "_"
|
|
|
|
|
|
|
|
def parent_draw(self, parent_layout, raw_comp_name):
|
|
|
|
if raw_comp_name not in self.uniqueElements:
|
|
|
|
parent_layout.prop(self, 'elements_sector')
|
|
|
|
|
|
|
|
class BALLANCE_OT_add_components(common_add_component_props):
|
|
|
|
"""Add Elements"""
|
2022-04-03 22:48:12 +08:00
|
|
|
bl_idname = "ballance.add_components"
|
2023-01-30 22:49:49 +08:00
|
|
|
bl_label = "Add Elements"
|
2022-04-03 22:48:12 +08:00
|
|
|
bl_options = {'UNDO'}
|
|
|
|
|
|
|
|
elements_type: bpy.props.EnumProperty(
|
|
|
|
name="Type",
|
|
|
|
description="This element type",
|
2023-01-30 11:12:15 +08:00
|
|
|
#items=tuple(map(lambda x: (x, x, ""), UTILS_constants.bmfile_componentList)),
|
|
|
|
items=tuple(
|
|
|
|
# token, display name, descriptions, icon, index
|
|
|
|
(blk, blk, "", UTILS_icons_manager.get_element_icon(blk), idx)
|
|
|
|
for idx, blk in enumerate(UTILS_constants.bmfile_componentList)
|
|
|
|
),
|
2022-04-03 22:48:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
# get name
|
2023-01-30 22:49:49 +08:00
|
|
|
finalObjectName = self.get_component_name(self.elements_type)
|
2022-04-03 22:48:12 +08:00
|
|
|
|
|
|
|
# create object
|
|
|
|
loadedMesh = UTILS_functions.load_component(
|
2023-01-30 22:49:49 +08:00
|
|
|
UTILS_constants.bmfile_componentList.index(self.elements_type)
|
|
|
|
)
|
2022-04-03 22:48:12 +08:00
|
|
|
obj = bpy.data.objects.new(finalObjectName, loadedMesh)
|
|
|
|
UTILS_functions.add_into_scene_and_move_to_cursor(obj)
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
wm = context.window_manager
|
|
|
|
return wm.invoke_props_dialog(self)
|
|
|
|
|
|
|
|
def draw(self, context):
|
|
|
|
layout = self.layout
|
2023-01-09 11:10:13 +08:00
|
|
|
# attension notice
|
|
|
|
if self.elements_type in self.attentionElements:
|
2023-01-30 22:49:49 +08:00
|
|
|
layout.label(text="NOTE: Check Sector ID carefully.")
|
|
|
|
if self.elements_type in self.uniqueElements:
|
|
|
|
layout.label(text="NOTE: This element have unique name.")
|
2023-01-09 11:10:13 +08:00
|
|
|
|
|
|
|
# cfg
|
2022-04-03 22:48:12 +08:00
|
|
|
layout.prop(self, "elements_type")
|
2023-01-30 22:49:49 +08:00
|
|
|
self.parent_draw(layout, self.elements_type)
|
|
|
|
|
2023-01-31 10:47:47 +08:00
|
|
|
@classmethod
|
|
|
|
def draw_blc_menu(self, layout):
|
|
|
|
for item in UTILS_constants.bmfile_componentList:
|
|
|
|
cop = layout.operator(
|
|
|
|
self.bl_idname, text=item,
|
|
|
|
icon_value = UTILS_icons_manager.get_element_icon(item))
|
|
|
|
cop.elements_type = item
|
2023-01-30 22:49:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BALLANCE_OT_add_components_dup(common_add_component_props):
|
|
|
|
"""Add Duplicated Elements"""
|
|
|
|
bl_idname = "ballance.add_components_dup"
|
|
|
|
bl_label = "Add Duplicated Elements"
|
|
|
|
bl_options = {'UNDO'}
|
|
|
|
|
2023-01-31 10:47:47 +08:00
|
|
|
can_duplicated_elements = (
|
|
|
|
'P_Extra_Point', 'P_Modul_18', 'P_Modul_26'
|
|
|
|
)
|
|
|
|
|
2023-01-30 22:49:49 +08:00
|
|
|
elements_type: bpy.props.EnumProperty(
|
|
|
|
name="Type",
|
|
|
|
description="This element type",
|
|
|
|
#items=tuple(map(lambda x: (x, x, ""), UTILS_constants.bmfile_componentList)),
|
|
|
|
items=tuple(
|
|
|
|
# token, display name, descriptions, icon, index
|
|
|
|
(blk, blk, "", UTILS_icons_manager.get_element_icon(blk), idx)
|
2023-01-31 10:47:47 +08:00
|
|
|
for idx, blk in enumerate(can_duplicated_elements)
|
2023-01-30 22:49:49 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
elements_dup_times: bpy.props.IntProperty(
|
|
|
|
name="Duplication Count",
|
|
|
|
description="How many this element should be duplicated.",
|
|
|
|
min=2, max=64,
|
|
|
|
soft_min=2, soft_max=32,
|
|
|
|
default=2,
|
|
|
|
)
|
2023-01-09 11:10:13 +08:00
|
|
|
|
2023-01-30 22:49:49 +08:00
|
|
|
def execute(self, context):
|
|
|
|
# get name
|
|
|
|
finalObjectName = self.get_component_name(self.elements_type)
|
|
|
|
|
|
|
|
# load mesh
|
|
|
|
loadedMesh = UTILS_functions.load_component(
|
|
|
|
UTILS_constants.bmfile_componentList.index(self.elements_type)
|
|
|
|
)
|
|
|
|
# create object
|
|
|
|
for i in range(self.elements_dup_times):
|
|
|
|
obj = bpy.data.objects.new(finalObjectName, loadedMesh)
|
|
|
|
UTILS_functions.add_into_scene_and_move_to_cursor(obj)
|
|
|
|
|
|
|
|
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")
|
|
|
|
self.parent_draw(layout, self.elements_type)
|
|
|
|
layout.prop(self, "elements_dup_times")
|
|
|
|
|
2023-01-31 10:47:47 +08:00
|
|
|
@classmethod
|
|
|
|
def draw_blc_menu(self, layout):
|
|
|
|
for item in self.can_duplicated_elements:
|
|
|
|
cop = layout.operator(
|
|
|
|
self.bl_idname, text=item,
|
|
|
|
icon_value = UTILS_icons_manager.get_element_icon(item))
|
|
|
|
cop.elements_type = item
|
2023-01-30 22:49:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BALLANCE_OT_add_components_series(common_add_component_props):
|
|
|
|
"""Add Elements with a Series."""
|
|
|
|
bl_idname = "ballance.add_components_series"
|
|
|
|
bl_label = "Add Series Elements"
|
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
|
|
|
|
supported_series = {
|
|
|
|
# format: key: (description: str, real_component: str, unit_transition: mathutils.Vector, default_span: float)
|
|
|
|
# key will become enum property's identifier
|
|
|
|
"MODUL_41": ('Tilting Block Series', 'P_Modul_41', mathutils.Vector((1.0, 0.0, 0.0)), 6.0022),
|
|
|
|
"MODUL_18_V": ('Fan Vertical Series', 'P_Modul_18', mathutils.Vector((0.0, 0.0, 1.0)), 15),
|
|
|
|
"MODUL_18_H": ('Fan Horizonal Series', 'P_Modul_18', mathutils.Vector((1.0, 0.0, 0.0)), 30),
|
|
|
|
}
|
|
|
|
|
|
|
|
# the updator for default span
|
|
|
|
def element_type_updated(self, context):
|
|
|
|
# set span
|
|
|
|
self.elements_span = BALLANCE_OT_add_components_series.supported_series[self.elements_type][3]
|
|
|
|
|
|
|
|
# blender required
|
|
|
|
return None
|
|
|
|
|
|
|
|
elements_type: bpy.props.EnumProperty(
|
|
|
|
name="Type",
|
|
|
|
description="This element type",
|
|
|
|
#items=tuple(map(lambda x: (x, x, ""), UTILS_constants.bmfile_componentList)),
|
|
|
|
items=tuple(
|
|
|
|
# token, display name, descriptions, icon, index
|
|
|
|
(skey, sitem[0], "", UTILS_icons_manager.get_element_icon(sitem[1]), idx)
|
|
|
|
for (idx, (skey, sitem)) in enumerate(supported_series.items())
|
|
|
|
),
|
|
|
|
default=0,
|
|
|
|
update=element_type_updated
|
|
|
|
)
|
|
|
|
|
|
|
|
elements_dup_times: bpy.props.IntProperty(
|
|
|
|
name="Duplication Count",
|
|
|
|
description="How many this element should be duplicated.",
|
|
|
|
min=2, max=64,
|
|
|
|
soft_min=2, soft_max=32,
|
|
|
|
default=2,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements_span: bpy.props.FloatProperty(
|
|
|
|
name="Elements Span",
|
|
|
|
description="The span between each elements.",
|
|
|
|
min=0.0,
|
|
|
|
default=0.0,
|
|
|
|
)
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
# force trigger span update once to treat span normally
|
|
|
|
self.element_type_updated(context)
|
|
|
|
|
|
|
|
return self.execute(context)
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
# get unit span and real element name for loading mesh and creating name
|
|
|
|
(_, real_element_name, unit_span, _) = self.supported_series[self.elements_type]
|
|
|
|
|
|
|
|
# get name
|
|
|
|
finalObjectName = self.get_component_name(real_element_name)
|
|
|
|
# load mesh
|
|
|
|
loadedMesh = UTILS_functions.load_component(
|
|
|
|
UTILS_constants.bmfile_componentList.index(real_element_name)
|
|
|
|
)
|
|
|
|
|
|
|
|
# create object
|
|
|
|
for i in range(self.elements_dup_times):
|
|
|
|
obj = bpy.data.objects.new(finalObjectName, loadedMesh)
|
|
|
|
UTILS_functions.add_into_scene_and_move_to_cursor(obj)
|
|
|
|
obj.matrix_world.translation += unit_span * (self.elements_span * i)
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
def draw(self, context):
|
|
|
|
layout = self.layout
|
|
|
|
layout.prop(self, "elements_type")
|
|
|
|
self.parent_draw(layout, self.elements_type)
|
|
|
|
layout.prop(self, "elements_dup_times")
|
|
|
|
layout.prop(self, "elements_span")
|
2023-01-31 10:47:47 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def draw_blc_menu(self, layout):
|
|
|
|
for key, item in self.supported_series.items():
|
|
|
|
cop = layout.operator(
|
|
|
|
self.bl_idname, text=item[0],
|
|
|
|
icon_value = UTILS_icons_manager.get_element_icon(item[1]))
|
|
|
|
cop.elements_type = key
|