Advertisement
davegimo

Untitled

Apr 2nd, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import bpy
  2. import bmesh
  3.  
  4.  
  5. def importObject(n):
  6.     blendfile = 'C:/Users/david/Desktop/tt.blend'
  7.     section   = '\\Object\\'
  8.     objects    = ['C1','C2'] ###<-- Add the name of objects you want to append
  9.     directory = blendfile + section
  10.     for obj in objects:  
  11.         filename  = obj
  12.         bpy.ops.wm.append(filename=filename, directory=directory)
  13.  
  14. def bmesh_copy_from_object(obj, transform=True, triangulate=True, apply_modifiers=False):
  15.     """
  16.    Returns a transformed, triangulated copy of the mesh
  17.    """
  18.  
  19.     assert(obj.type == 'MESH')
  20.  
  21.     if apply_modifiers and obj.modifiers:
  22.         me = obj.to_mesh(bpy.context.collection, True, 'PREVIEW', calc_tessface=False)
  23.         bm = bmesh.new()
  24.         bm.from_mesh(me)
  25.         bpy.data.meshes.remove(me)
  26.     else:
  27.         me = obj.data
  28.         if obj.mode == 'EDIT':
  29.             bm_orig = bmesh.from_edit_mesh(me)
  30.             bm = bm_orig.copy()
  31.         else:
  32.             bm = bmesh.new()
  33.             bm.from_mesh(me)
  34.  
  35.     # Remove custom data layers to save memory
  36.     for elem in (bm.faces, bm.edges, bm.verts, bm.loops):
  37.         for layers_name in dir(elem.layers):
  38.             if not layers_name.startswith("_"):
  39.                 layers = getattr(elem.layers, layers_name)
  40.                 for layer_name, layer in layers.items():
  41.                     layers.remove(layer)
  42.  
  43.     if transform:
  44.         bm.transform(obj.matrix_world)
  45.  
  46.     if triangulate:
  47.         bmesh.ops.triangulate(bm, faces=bm.faces)
  48.  
  49.     return bm
  50.  
  51. def bmesh_check_intersect_objects(obj, obj2):
  52.     """
  53.    Check if any faces intersect with the other object
  54.  
  55.    returns a boolean
  56.    """
  57.     assert(obj != obj2)
  58.  
  59.     # Triangulate
  60.     bm = bmesh_copy_from_object(obj, transform=True, triangulate=True)
  61.     bm2 = bmesh_copy_from_object(obj2, transform=True, triangulate=True)
  62.  
  63.     # If bm has more edges, use bm2 instead for looping over its edges
  64.     # (so we cast less rays from the simpler object to the more complex object)
  65.     if len(bm.edges) > len(bm2.edges):
  66.         bm2, bm = bm, bm2
  67.  
  68.     # Create a real mesh (lame!)
  69.     scene = bpy.context.collection
  70.     me_tmp = bpy.data.meshes.new(name="~temp~")
  71.     bm2.to_mesh(me_tmp)
  72.     bm2.free()
  73.     obj_tmp = bpy.data.objects.new(name=me_tmp.name, object_data=me_tmp)
  74.     scene.objects.link(obj_tmp)
  75.     #scene.update()
  76.     bpy.context.view_layer.update()
  77.     ray_cast = obj_tmp.ray_cast
  78.  
  79.     intersect = False
  80.  
  81.     EPS_NORMAL = 0.000001
  82.     EPS_CENTER = 0.01  # should always be bigger
  83.  
  84.     #for ed in me_tmp.edges:
  85.     for ed in bm.edges:
  86.         v1, v2 = ed.verts
  87.  
  88.         # setup the edge with an offset
  89.         co_1 = v1.co.copy()
  90.         co_2 = v2.co.copy()
  91.         co_mid = (co_1 + co_2) * 0.5
  92.         no_mid = (v1.normal + v2.normal).normalized() * EPS_NORMAL
  93.         co_1 = co_1.lerp(co_mid, EPS_CENTER) + no_mid
  94.         co_2 = co_2.lerp(co_mid, EPS_CENTER) + no_mid
  95.  
  96.         co, no, index = ray_cast(co_1, co_2)
  97.         if index != -1:
  98.             intersect = True
  99.             break
  100.  
  101.  
  102.    
  103.     scene.objects.unlink(obj_tmp)
  104.     bpy.data.objects.remove(obj_tmp)
  105.     bpy.data.meshes.remove(me_tmp)
  106.  
  107.     #scene.update()
  108.     bpy.context.view_layer.update()
  109.  
  110.     return intersect
  111.  
  112. bpy.ops.object.select_all(action='DESELECT')
  113. bpy.data.objects['Cube'].select_set(True) # Blender 2.8x
  114. bpy.ops.object.delete()
  115.  
  116. importObject(1)
  117.  
  118. obj = bpy.data.objects["C1"]
  119. obj2 = bpy.data.objects["C2"]
  120. intersect = bmesh_check_intersect_objects(obj, obj2)
  121.  
  122. print("There are%s intersections." % ("" if intersect else " NO"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement