@realazthat/

Triplaner-mesh-split

Python

No description

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8628551589622415","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"CmZyb20gY29sbGVjdGlvbnMgaW1wb3J0IGRlZmF1bHRkaWN0CmltcG9ydCBjb3B5Cgp2ZXJ0aWNlcyA9IFtdCnRyaWFuZ2xlcyA9IFtdCgojIG1hcCB7KG9sZCB2ZXJ0ZXggaWQsIG5ldyBtYXRlcmlhbF9pZCkgPT4gbmV3IHZlcnRleCBpZH0KbmV3X3ZlcnRleF9tYXBwaW5ncyA9IHt9CgpkZWYgaGFzX2R1cGxpY2F0ZV92ZXJ0ZXhfd2l0aF90aGlzX21hdGVyaWFsKHZfaWR4MCwgbWF0ZXJpYWxfaWQpOgogIHJldHVybiAodl9pZHgwLCBtYXRlcmlhbF9pZCkgaW4gbmV3X3ZlcnRleF9tYXBwaW5ncwoKZGVmIGhhc19jb25mbGljdGluZ192ZXJ0ZXgodHJpYW5nbGUpOgogIGksaixrID0gdHJpYW5nbGUKICB1LHYsdyA9IHZlcnRpY2VzW2ldLCB2ZXJ0aWNlc1tqXSwgdmVydGljZXNba10KICAKICByZXR1cm4gdi5tYXRlcmlhbF9pZCAhPSB1Lm1hdGVyaWFsX2lkIG9yIHcubWF0ZXJpYWxfaWQgIT0gdS5tYXRlcmlhbF9pZAoKZGVmIGdldF9tYWpvcml0eV9tYXRlcmlhbCh0cmlhbmdsZSk6CiAgIyBkaWN0aW9uYXJ5IHRoYXQgZGVmYXVsdHMgdG8gMAogIGNvdW50cyA9IGRlZmF1bHRkaWN0KGxhbWJkYTogMCkKICAKICBpLGosayA9IHRyaWFuZ2xlCiAgdSx2LHcgPSB2ZXJ0aWNlc1tpXSwgdmVydGljZXNbal0sIHZlcnRpY2VzW2tdCiAgCiAgCiAgaWYgdS5tYXRlcmlhbF9pZCA9PSB2Lm1hdGVyaWFsX2lkOgogICAgc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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from collections import defaultdict
import copy

vertices = []
triangles = []

# map {(old vertex id, new material_id) => new vertex id}
new_vertex_mappings = {}

def has_duplicate_vertex_with_this_material(v_idx0, material_id):
  return (v_idx0, material_id) in new_vertex_mappings

def has_conflicting_vertex(triangle):
  i,j,k = triangle
  u,v,w = vertices[i], vertices[j], vertices[k]
  
  return v.material_id != u.material_id or w.material_id != u.material_id

def get_majority_material(triangle):
  # dictionary that defaults to 0
  counts = defaultdict(lambda: 0)
  
  i,j,k = triangle
  u,v,w = vertices[i], vertices[j], vertices[k]
  
  
  if u.material_id == v.material_id:
    return u.material_id
  
  if u.material_id == w.material_id:
    return u.material_id
  
  if v.material_id == w.material_id:
    return v.material_id
  
  
  # No majority ... :(
  
  # We have a few choices of what to do here, but the simplest is just to
  # find an ordering.
  
  return min([u.material_id, v.material_id, w.material_id])
  


for triangle in triangles:
  if not has_conflicting_vertex(triangle):
    continue
  
  # majority or decide how to determine material for this special case here:
  majority_material = get_majority_material(triangle)
  
  
  for t_i in range(3):
    v_idx0 = triangle[t_i]
    v = vertices[v_idx0]
    if v.material_id == majority_material:
      # all good already.
      continue
  
    if has_duplicate_vertex_with_this_material(v_idx0, majority_material):
      # Check if we already duplicated this vertex to have this material.
      
      # Get the duplicate 
      duplicate_idx = new_vertex_mappings[ (v_idx0, majority_material) ]
      triangle[t_i] = duplicate_idx
    else:
      # Make a copy of the vertex
      duplicate_v = copy.deepcopy(v)
      
      # Except the copy should have the changed material id.
      duplicate_v.material_id = majority_material
      
      # Put the copy in the vertices buffer.
      duplicate_idx = len(vertices)
      vertices.append(duplicate_v)
      
      # Record the mapping from the old vertex to the new one.
      new_vertex_mappings[ (v_idx0, majority_material) ] = duplicate_idx
      triangle[t_i] = duplicate_idx