Skip to content

Commit

Permalink
feat: add extra transform for rail creation
Browse files Browse the repository at this point in the history
- add extra transform for rail creation.
- remove scale from extra transform because it is rarely used. because Ballance can not handle the physicalization of scaled object.
  • Loading branch information
yyc12345 committed May 13, 2024
1 parent 190be6e commit 084e7fb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 28 deletions.
14 changes: 1 addition & 13 deletions bbp_ng/OP_ADDS_bme.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ def bme_struct_type_updated(self, context):
step = 100, # We choosen 100, mean 1. Sync with property window.
default = (0.0, 0.0, 0.0)
) # type: ignore
extra_scale: bpy.props.FloatVectorProperty(
name = "Extra Scale",
description = "The extra scale applied to object after moving to cursor.",
size = 3,
subtype = 'XYZ',
step = 1, # We choosen 1, mean 0.01. Sync with property window.
default = (1.0, 1.0, 1.0)
) # type: ignore

@classmethod
def poll(cls, context):
Expand Down Expand Up @@ -218,7 +210,7 @@ def execute(self, context):
obj.matrix_world = obj.matrix_world @ mathutils.Matrix.LocRotScale(
mathutils.Vector(self.extra_translation),
mathutils.Euler(self.extra_rotation, 'XYZ'),
mathutils.Vector(self.extra_scale)
mathutils.Vector((1.0, 1.0, 1.0)) # no scale
)
# select created object
UTIL_functions.select_certain_objects((obj, ))
Expand Down Expand Up @@ -277,10 +269,6 @@ def draw(self, context):
layout.label(text = 'Rotation')
hbox_layout = layout.row()
hbox_layout.prop(self, 'extra_rotation', text = '')
# scale
layout.label(text = 'Scale')
hbox_layout = layout.row()
hbox_layout.prop(self, 'extra_scale', text = '')

@classmethod
def draw_blc_menu(cls, layout: bpy.types.UILayout):
Expand Down
96 changes: 81 additions & 15 deletions bbp_ng/OP_ADDS_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,50 @@

#region Operator Helpers

class SharedExtraTransform():
"""
This class is served for all rail creation which allow user
provide extra transform after moving created rail to cursor.
For "what you look is what you gotten" experience, this extra transform is essential.
"""

extra_translation: bpy.props.FloatVectorProperty(
name = "Extra Translation",
description = "The extra translation applied to object after moving to cursor.",
size = 3,
subtype = 'TRANSLATION',
step = 50, # same step as the float entry of BBP_PG_bme_adder_cfgs
default = (0.0, 0.0, 0.0)
) # type: ignore
extra_rotation: bpy.props.FloatVectorProperty(
name = "Extra Rotation",
description = "The extra rotation applied to object after moving to cursor.",
size = 3,
subtype = 'EULER',
step = 100, # We choosen 100, mean 1. Sync with property window.
default = (0.0, 0.0, 0.0)
) # type: ignore

def draw_extra_transform_input(self, layout: bpy.types.UILayout) -> None:
# show extra transform props
# forcely order that each one are placed horizontally
layout.label(text = "Extra Transform:")
# translation
layout.label(text = 'Translation')
row = layout.row()
row.prop(self, 'extra_translation', text = '')
# rotation
layout.label(text = 'Rotation')
row = layout.row()
row.prop(self, 'extra_rotation', text = '')

def general_get_extra_transform(self) -> mathutils.Matrix:
return mathutils.Matrix.LocRotScale(
mathutils.Vector(self.extra_translation),
mathutils.Euler(self.extra_rotation, 'XYZ'),
mathutils.Vector((1.0, 1.0, 1.0)) # no scale
)

class SharedRailSectionInputProperty():
"""
This class is served for user to pick the transition type of rail.
Expand Down Expand Up @@ -147,7 +191,8 @@ def execute(self, context):
lambda bm: _create_rail_section(
bm, self.general_get_is_monorail(),
c_DefaultRailRadius, c_DefaultRailSpan
)
),
mathutils.Matrix.Identity(4)
)
return {'FINISHED'}

Expand All @@ -163,15 +208,16 @@ class BBP_OT_add_transition_section(bpy.types.Operator):

def execute(self, context):
_rail_creator_wrapper(
lambda bm: _create_transition_section(bm, c_DefaultRailRadius, c_DefaultRailSpan)
lambda bm: _create_transition_section(bm, c_DefaultRailRadius, c_DefaultRailSpan),
mathutils.Matrix.Identity(4)
)
return {'FINISHED'}

def draw(self, context):
layout = self.layout
layout.label(text = 'No Options Available')

class BBP_OT_add_straight_rail(SharedRailSectionInputProperty, SharedRailCapInputProperty, SharedStraightRailInputProperty, bpy.types.Operator):
class BBP_OT_add_straight_rail(SharedExtraTransform, SharedRailSectionInputProperty, SharedRailCapInputProperty, SharedStraightRailInputProperty, bpy.types.Operator):
"""Add Straight Rail"""
bl_idname = "bbp.add_straight_rail"
bl_label = "Straight Rail"
Expand All @@ -184,7 +230,8 @@ def execute(self, context):
self.general_get_is_monorail(), c_DefaultRailRadius, c_DefaultRailSpan,
self.general_get_rail_length(), 0,
self.general_get_rail_start_cap(), self.general_get_rail_end_cap()
)
),
self.general_get_extra_transform()
)
return {'FINISHED'}

Expand All @@ -196,8 +243,10 @@ def draw(self, context):
layout.separator()
layout.label(text = 'Rail Cap')
self.draw_rail_cap_input(layout)
layout.separator()
self.draw_extra_transform_input(layout)

class BBP_OT_add_transition_rail(SharedRailCapInputProperty, SharedStraightRailInputProperty, bpy.types.Operator):
class BBP_OT_add_transition_rail(SharedExtraTransform, SharedRailCapInputProperty, SharedStraightRailInputProperty, bpy.types.Operator):
"""Add Transition Rail"""
bl_idname = "bbp.add_transition_rail"
bl_label = "Transition Rail"
Expand All @@ -210,7 +259,8 @@ def execute(self, context):
c_DefaultRailRadius, c_DefaultRailSpan,
self.general_get_rail_length(),
self.general_get_rail_start_cap(), self.general_get_rail_end_cap()
)
),
self.general_get_extra_transform()
)
return {'FINISHED'}

Expand All @@ -221,8 +271,10 @@ def draw(self, context):
layout.separator()
layout.label(text = 'Rail Cap')
self.draw_rail_cap_input(layout)
layout.separator()
self.draw_extra_transform_input(layout)

class BBP_OT_add_side_rail(SharedRailCapInputProperty, SharedStraightRailInputProperty, bpy.types.Operator):
class BBP_OT_add_side_rail(SharedExtraTransform, SharedRailCapInputProperty, SharedStraightRailInputProperty, bpy.types.Operator):
"""Add Side Rail"""
bl_idname = "bbp.add_side_rail"
bl_label = "Side Rail"
Expand All @@ -246,7 +298,8 @@ def execute(self, context):
self.general_get_rail_length(),
c_NormalSideRailAngle if self.side_rail_type == 'NORMAL' else c_StoneSideRailAngle,
self.general_get_rail_start_cap(), self.general_get_rail_end_cap()
)
),
self.general_get_extra_transform()
)
return {'FINISHED'}

Expand All @@ -258,8 +311,10 @@ def draw(self, context):
layout.separator()
layout.label(text = 'Rail Cap')
self.draw_rail_cap_input(layout)
layout.separator()
self.draw_extra_transform_input(layout)

class BBP_OT_add_arc_rail(SharedRailSectionInputProperty, SharedRailCapInputProperty, SharedScrewRailInputProperty, bpy.types.Operator):
class BBP_OT_add_arc_rail(SharedExtraTransform, SharedRailSectionInputProperty, SharedRailCapInputProperty, SharedScrewRailInputProperty, bpy.types.Operator):
"""Add Arc Rail"""
bl_idname = "bbp.add_arc_rail"
bl_label = "Arc Rail"
Expand All @@ -281,7 +336,8 @@ def execute(self, context):
self.general_get_rail_start_cap(), self.general_get_rail_end_cap(),
math.degrees(self.rail_screw_angle), 0, 1, # blender passed value is in radians
self.general_get_rail_screw_steps(), self.general_get_rail_screw_radius()
)
),
self.general_get_extra_transform()
)
return {'FINISHED'}

Expand All @@ -294,8 +350,10 @@ def draw(self, context):
layout.separator()
layout.label(text = 'Rail Cap')
self.draw_rail_cap_input(layout)
layout.separator()
self.draw_extra_transform_input(layout)

class BBP_OT_add_spiral_rail(SharedRailCapInputProperty, SharedScrewRailInputProperty, bpy.types.Operator):
class BBP_OT_add_spiral_rail(SharedExtraTransform, SharedRailCapInputProperty, SharedScrewRailInputProperty, bpy.types.Operator):
"""Add Spiral Rail"""
bl_idname = "bbp.add_spiral_rail"
bl_label = "Spiral Rail"
Expand Down Expand Up @@ -323,7 +381,8 @@ def execute(self, context):
self.general_get_rail_start_cap(), self.general_get_rail_end_cap(),
360, self.rail_screw_screw, self.rail_screw_iterations,
self.general_get_rail_screw_steps(), self.general_get_rail_screw_radius()
)
),
self.general_get_extra_transform()
)
return {'FINISHED'}

Expand All @@ -336,8 +395,10 @@ def draw(self, context):
layout.separator()
layout.label(text = 'Rail Cap')
self.draw_rail_cap_input(layout)
layout.separator()
self.draw_extra_transform_input(layout)

class BBP_OT_add_side_spiral_rail(SharedRailSectionInputProperty, SharedRailCapInputProperty, SharedScrewRailInputProperty, bpy.types.Operator):
class BBP_OT_add_side_spiral_rail(SharedExtraTransform, SharedRailSectionInputProperty, SharedRailCapInputProperty, SharedScrewRailInputProperty, bpy.types.Operator):
"""Add Side Spiral Rail"""
bl_idname = "bbp.add_side_spiral_rail"
bl_label = "Side Spiral Rail"
Expand All @@ -360,7 +421,8 @@ def execute(self, context):
self.general_get_rail_start_cap(), self.general_get_rail_end_cap(),
360, c_SideSpiralRailScrew, self.rail_screw_iterations,
self.general_get_rail_screw_steps(), self.general_get_rail_screw_radius()
)
),
self.general_get_extra_transform()
)
return {'FINISHED'}

Expand All @@ -372,6 +434,8 @@ def draw(self, context):
layout.separator()
layout.label(text = 'Rail Cap')
self.draw_rail_cap_input(layout)
layout.separator()
self.draw_extra_transform_input(layout)

#endregion

Expand Down Expand Up @@ -460,7 +524,7 @@ def _bmesh_cap(bm: bmesh.types.BMesh, edges: list[bmesh.types.BMEdge]) -> None:

#region Real Rail Creators

def _rail_creator_wrapper(fct_poly_cret: typing.Callable[[bmesh.types.BMesh], None]) -> bpy.types.Object:
def _rail_creator_wrapper(fct_poly_cret: typing.Callable[[bmesh.types.BMesh], None], extra_transform: mathutils.Matrix) -> bpy.types.Object:
# create mesh first
bm: bmesh.types.BMesh = bmesh.new()

Expand Down Expand Up @@ -492,6 +556,8 @@ def _rail_creator_wrapper(fct_poly_cret: typing.Callable[[bmesh.types.BMesh], No

# move to cursor
UTIL_functions.add_into_scene_and_move_to_cursor(obj)
# add extra transform
obj.matrix_world = obj.matrix_world @ extra_transform
# select created object
UTIL_functions.select_certain_objects((obj, ))

Expand Down

0 comments on commit 084e7fb

Please sign in to comment.