Skip to content

Commit

Permalink
#449 Convert to collider from Name
Browse files Browse the repository at this point in the history
  • Loading branch information
Weisl committed Jan 28, 2024
1 parent 5c04549 commit bc0293c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions collider_conversion/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from . import convert_to_collider
from . import convert_to_mesh
from . import regenerate_name
from . import convert_from_name

classes = (
convert_to_collider.OBJECT_OT_convert_to_collider,
convert_to_mesh.OBJECT_OT_convert_to_mesh,
convert_from_name.OBJECT_OT_convert_from_name,
regenerate_name.OBJECT_OT_regenerate_name,
)

Expand Down
90 changes: 90 additions & 0 deletions collider_conversion/convert_from_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import bpy
from bpy.types import Operator
import re

class OBJECT_OT_convert_from_name(Operator):
"""Convert selected colliders to mesh objects"""
bl_idname = "object.convert_from_name"
bl_label = "Collider from Naming"
bl_description = 'Assign collider attributes from the object naming.'


@classmethod
def poll(cls, context):
return len(context.selected_objects) > 0

def execute(self, context):
colSettings = context.scene.collider_tools
count = 0

prefs = context.preferences.addons[__package__.split('.')[0]].preferences

for obj in bpy.context.selected_objects.copy():
name = obj.name

col_suffix = prefs.collision_string_suffix
col_prefix = prefs.collision_string_prefix

isCollider = False

user_group_01 = prefs.user_group_01,
user_group_02 = prefs.user_group_02,
user_group_03 = prefs.user_group_03


regexp = re.compile(str(user_group_03))
if regexp.search(name):
obj['collider_group'] = 'USER_03'
isCollider = True

regexp = re.compile(str(user_group_02))
if regexp.search(name):
obj['collider_group'] = 'USER_02'
isCollider = True

regexp = re.compile(str(user_group_01))
if regexp.search(name):
obj['collider_group'] = 'USER_01'
isCollider = True


shape = prefs.box_shape
regexp = re.compile(str(shape))
if regexp.search(name):
obj['collider_shape'] = 'box_shape'
isCollider = True

shape = prefs.sphere_shape
regexp = re.compile(str(shape))
if regexp.search(name):
obj['collider_shape'] = 'sphere_shape'
isCollider = True

shape = prefs.capsule_shape
regexp = re.compile(str(shape))
if regexp.search(name):
obj['collider_shape'] = 'capsule_shape'
isCollider = True

shape = prefs.convex_shape
regexp = re.compile(str(shape))
if regexp.search(name):
obj['collider_shape'] = 'convex_shape'
isCollider = True

shape = prefs.mesh_shape
regexp = re.compile(str(shape))
if regexp.search(name):
obj['collider_shape'] = 'mesh_shape'
isCollider = True

if isCollider:
obj['isCollider'] = True


if count == 0:
self.report({'WARNING'}, 'No collider has been detected')
else:
self.report({'INFO'}, f"{count} colliders have been converted")

return {'FINISHED'}
3 changes: 3 additions & 0 deletions ui/properties_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ def draw_creation_menu(context, layout, settings=False):
row = layout.row(align=True)
row.operator('object.regenerate_name', icon='FILE_REFRESH')

row = layout.row(align=True)
row.operator('object.convert_from_name', icon='NONE')

layout.separator()

# row = layout.row(align=True)
Expand Down

0 comments on commit bc0293c

Please sign in to comment.