Skip to content

Commit

Permalink
Merge branch 'develop' into #392-keep-original-name-for-conver-to
Browse files Browse the repository at this point in the history
  • Loading branch information
Weisl committed Jun 29, 2023
2 parents 2cfe1b3 + f1c9b39 commit fcf34e1
Show file tree
Hide file tree
Showing 20 changed files with 666 additions and 175 deletions.
84 changes: 26 additions & 58 deletions auto_Convex/add_bounding_auto_convex.py
Expand Up @@ -7,54 +7,7 @@
from bpy.types import Operator

from ..collider_shapes.add_bounding_primitive import OBJECT_OT_add_bounding_object


def bmesh_join(list_of_bmeshes, list_of_matrices, normal_update=False):
# sourcery skip: use-contextlib-suppress
""" takes as input a list of bm references and outputs a single merged bmesh
allows an additional 'normal_update=True' to force _normal_ calculations.
"""
bm = bmesh.new()
add_vert = bm.verts.new
add_face = bm.faces.new
add_edge = bm.edges.new

for bm_to_add, matrix in zip(list_of_bmeshes, list_of_matrices):
bm_to_add.transform(matrix)

for bm_to_add in list_of_bmeshes:
offset = len(bm.verts)

for v in bm_to_add.verts:
add_vert(v.co)

bm.verts.index_update()
bm.verts.ensure_lookup_table()

if bm_to_add.faces:
for face in bm_to_add.faces:
add_face(tuple(bm.verts[i.index + offset] for i in face.verts))
bm.faces.index_update()

if bm_to_add.edges:
for edge in bm_to_add.edges:
edge_seq = tuple(bm.verts[i.index + offset]
for i in edge.verts)
try:
add_edge(edge_seq)
except ValueError:
# edge exists!
pass
bm.edges.index_update()

if normal_update:
bm.normal_update()

me = bpy.data.meshes.new("joined_mesh")
bm.to_mesh(me)

return me

from ..bmesh_operations.mesh_edit import bmesh_join

class VHACD_OT_convex_decomposition(OBJECT_OT_add_bounding_object, Operator):
bl_idname = 'collision.vhacd'
Expand Down Expand Up @@ -118,8 +71,7 @@ def execute(self, context):
# CLEANUP
super().execute(context)

overwrite_path = self.overwrite_executable_path(
self.prefs.executable_path)
overwrite_path = self.overwrite_executable_path(self.prefs.executable_path)
vhacd_exe = self.prefs.default_executable_path if not overwrite_path else overwrite_path
data_path = self.set_temp_data_path(self.prefs.data_path)

Expand All @@ -139,15 +91,27 @@ def execute(self, context):
meshes = []
matrices = []

for obj in self.selected_objects:
for base_ob in self.selected_objects:

# skip if invalid object
if not self.is_valid_object(obj):
if not self.is_valid_object(base_ob):
continue

if base_ob and base_ob.type in self.valid_object_types:
if base_ob.type == 'MESH':
obj = base_ob

else:
# store initial state for operation cancel
user_collections = base_ob.users_collection
self.original_obj_data.append(self.store_initial_obj_state(base_ob, user_collections))
# convert meshes
obj = self.convert_to_mesh(context, base_ob, use_modifiers=self.my_use_modifier_stack)
self.tmp_meshes.append(obj)

context.view_layer.objects.active = obj

if self.obj_mode == "EDIT":
if self.obj_mode == "EDIT" and base_ob.type == 'MESH' and self.active_obj.type == 'MESH':
new_mesh = self.get_mesh_Edit(
obj, use_modifiers=self.my_use_modifier_stack)
else: # mode == "OBJECT":
Expand All @@ -159,7 +123,8 @@ def execute(self, context):

if self.creation_mode[self.creation_mode_idx] == 'INDIVIDUAL':
convex_collision_data = {}
convex_collision_data['parent'] = obj
convex_collision_data['parent'] = base_ob
convex_collision_data['mtx_world'] = base_ob.matrix_world.copy()
convex_collision_data['mesh'] = new_mesh
collider_data.append(convex_collision_data)

Expand All @@ -171,6 +136,7 @@ def execute(self, context):
if self.creation_mode[self.creation_mode_idx] == 'SELECTION':
convex_collision_data = {}
convex_collision_data['parent'] = self.active_obj
convex_collision_data['mtx_world'] = self.active_obj.matrix_world.copy()

bmeshes = []

Expand All @@ -190,6 +156,7 @@ def execute(self, context):
for convex_collision_data in collider_data:
parent = convex_collision_data['parent']
mesh = convex_collision_data['mesh']
mtx_world = convex_collision_data['mtx_world']

joined_obj = bpy.data.objects.new('debug_joined_mesh', mesh.copy())
bpy.context.scene.collection.objects.link(joined_obj)
Expand Down Expand Up @@ -302,25 +269,26 @@ def execute(self, context):
convex_collisions_data = {}
convex_collisions_data['colliders'] = imported
convex_collisions_data['parent'] = parent
convex_collisions_data['mtx_world'] = parent.matrix_world.copy()
convex_decomposition_data.append(convex_collisions_data)

context.view_layer.objects.active = self.active_obj

for convex_collisions_data in convex_decomposition_data:
convex_collision = convex_collisions_data['colliders']
parent = convex_collisions_data['parent']
mtx_world = convex_collisions_data['mtx_world']

for new_collider in convex_collision:
new_collider.name = super().collider_name(basename=parent.name)

self.custom_set_parent(context, parent, new_collider)

if self.creation_mode[self.creation_mode_idx] == 'INDIVIDUAL':
new_collider.matrix_world = parent.matrix_world
# Apply rotation and scale for custom origin to work.
new_collider.matrix_world = mtx_world
self.apply_transform(
new_collider, rotation=True, scale=True)

self.custom_set_parent(context, parent, new_collider)

collections = parent.users_collection
self.primitive_postprocessing(
context, new_collider, collections)
Expand Down
Empty file added bmesh_operations/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions bmesh_operations/box_creation.py
@@ -0,0 +1,63 @@
import bmesh
import bpy

from bpy_extras.object_utils import object_data_add


tmp_name = 'box_collider'

# vertex indizes defining the faces of the cube
face_order = [
(0, 1, 2, 3),
(4, 7, 6, 5),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7),
]


def add_box_object(context, vertices):
"""Generate a new object from the given vertices"""

global tmp_name

verts = vertices
edges = []
faces = [[0, 1, 2, 3], [7, 6, 5, 4], [5, 6, 2, 1], [0, 3, 7, 4], [3, 2, 6, 7], [4, 5, 1, 0]]

mesh = bpy.data.meshes.new(name=tmp_name)
mesh.from_pydata(verts, edges, faces)

return object_data_add(context, mesh, operator=None, name=None)


def verts_faces_to_bbox_collider(self, context, verts_loc):
"""Create box collider for selected mesh area in edit mode"""

global tmp_name

# add new mesh
mesh = bpy.data.meshes.new(tmp_name)
bm = bmesh.new()

# create mesh vertices
for v_co in verts_loc:
bm.verts.new(v_co)

# connect vertices to faces
bm.verts.ensure_lookup_table()
for f_idx in face_order:
bm.faces.new([bm.verts[i] for i in f_idx])

# update bmesh to draw properly in viewport
bm.to_mesh(mesh)
mesh.update()

# create new object from mesh and link it to collection
new_collider = bpy.data.objects.new(tmp_name, mesh)

root_collection = context.scene.collection
root_collection.objects.link(new_collider)

return new_collider
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions bmesh_operations/mesh_edit.py
@@ -0,0 +1,47 @@
import bpy, bmesh

def bmesh_join(list_of_bmeshes, list_of_matrices, normal_update=False):
# sourcery skip: use-contextlib-suppress
""" takes as input a list of bm references and outputs a single merged bmesh
allows an additional 'normal_update=True' to force _normal_ calculations.
"""
bm = bmesh.new()
add_vert = bm.verts.new
add_face = bm.faces.new
add_edge = bm.edges.new

for bm_to_add, matrix in zip(list_of_bmeshes, list_of_matrices):
bm_to_add.transform(matrix)

for bm_to_add in list_of_bmeshes:
offset = len(bm.verts)

for v in bm_to_add.verts:
add_vert(v.co)

bm.verts.index_update()
bm.verts.ensure_lookup_table()

if bm_to_add.faces:
for face in bm_to_add.faces:
add_face(tuple(bm.verts[i.index + offset] for i in face.verts))
bm.faces.index_update()

if bm_to_add.edges:
for edge in bm_to_add.edges:
edge_seq = tuple(bm.verts[i.index + offset]
for i in edge.verts)
try:
add_edge(edge_seq)
except ValueError:
# edge exists!
pass
bm.edges.index_update()

if normal_update:
bm.normal_update()

me = bpy.data.meshes.new("joined_mesh")
bm.to_mesh(me)

return me
10 changes: 6 additions & 4 deletions collider_conversion/__init__.py
@@ -1,9 +1,11 @@
from . import conversion_operators
from . import convert_to_collider
from . import convert_to_mesh
from . import regenerate_name

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


Expand Down

0 comments on commit fcf34e1

Please sign in to comment.