def voronoiCube(context, obj, parts, vol, walls, mat_index): re_unwrap = obj.destruction.re_unwrap smart_angle = obj.destruction.smart_angle dissolve_angle = obj.destruction.dissolve_angle # applyscale before # global start start = clock() loc = Vector(obj.location) obj.destruction.origLoc = loc if vol != None and vol != "": print("USING VOL") volobj = context.scene.objects[vol] volobj.select = True # ops.object.origin_set(type='ORIGIN_GEOMETRY') ops.object.transform_apply(scale=True, rotation=True) volobj.select = False print("I: ", dd.DataStore.impactLocation) vxmin, vxmax, vymin, vymax, vzmin, vzmax = corners(volobj, dd.DataStore.impactLocation - loc) vxmin += loc[0] vxmax += loc[0] vymin += loc[1] vymax += loc[1] vzmin += loc[2] vzmax += loc[2] [deselect(o) for o in bpy.data.objects] context.scene.objects.active = obj obj.select = True mesh_center = Vector((0, 0, 0)) diff = Vector((0, 0, 0)) if not walls: # memorize old origin and mesh_center(bounds) area = None for a in context.screen.areas: if a.type == "VIEW_3D": area = a ctx = context.copy() ctx["area"] = area # ops.object.mode_set(mode = 'EDIT') # ctx["edit_object"] = obj # ops.mesh.select_all(action = 'SELECT') ops.view3d.snap_cursor_to_selected(ctx) old_orig = context.scene.cursor_location.copy() ops.object.origin_set(type="ORIGIN_GEOMETRY", center="BOUNDS") ops.view3d.snap_cursor_to_selected(ctx) mesh_center = context.scene.cursor_location.copy() # ops.mesh.select_all(action = 'DESELECT') # ops.object.mode_set(mode = 'OBJECT') diff = old_orig - mesh_center print(mesh_center, old_orig, diff) obj.destruction.tempLoc = diff context.scene.cursor_location = old_orig ops.object.origin_set(type="ORIGIN_CURSOR") ops.object.origin_set(type="GEOMETRY_ORIGIN", center="BOUNDS") else: ops.object.origin_set(type="ORIGIN_GEOMETRY", center="BOUNDS") ops.object.transform_apply(scale=True, location=True, rotation=True) [select(o) for o in context.scene.objects] xmin, xmax, ymin, ymax, zmin, zmax = corners(obj) xmin += loc[0] xmax += loc[0] ymin += loc[1] ymax += loc[1] zmin += loc[2] zmax += loc[2] nx = 12 ny = 12 nz = 12 particles = parts print(xmin, xmax, ymin, ymax, zmin, zmax) if vol != None and vol != "" and context.object.destruction.voro_exact_shape: volobj = context.scene.objects[vol] particles = len(volobj.data.vertices) partsystem = None if context.object.destruction.voro_particles != "": partsystemname = context.object.destruction.voro_particles volobj = context.scene.objects[vol] partsystem = volobj.particle_systems[partsystemname] particles = len(partsystem.particles) # enlarge container a bit, so parts near the border wont be cut off theta = 0.5 if walls: theta = 10 con = voronoi.domain( xmin - theta, xmax + theta, ymin - theta, ymax + theta, zmin - theta, zmax + theta, nx, ny, nz, False, False, False, particles, ) if vol != None and vol != "": xmin = vxmin xmax = vxmax ymin = vymin ymax = vymax zmin = vzmin zmax = vzmax print("VOL: ", xmin, xmax, ymin, ymax, zmin, zmax) bm = obj.data if walls: colist = [] i = 0 for poly in bm.polygons: # n = p.calc_center_median() n = poly.normal v = bm.vertices[poly.vertices[0]].co d = n.dot(v) # print("Displacement: ", d) colist.append([n[0], n[1], n[2], d, i]) i = i + 1 # add a wall object per face con.add_wall(colist) values = [] if vol != None and vol != "" and context.object.destruction.voro_exact_shape and partsystem == None: volobj = context.scene.objects[vol] # context.scene.objects.active = volobj # volobj.select = True # ops.object.transform_apply(scale=True, location = True, rotation = True) # volobj.select = False # impact location comes from game engine, is 0 by default # in game engine is location of volobj 0 by default, so this expression should be correct. for v in volobj.data.vertices: values.append( ( v.co[0] + dd.DataStore.impactLocation[0] + volobj.location[0], v.co[1] + dd.DataStore.impactLocation[1] + volobj.location[1], v.co[2] + dd.DataStore.impactLocation[2] + volobj.location[2], ) ) # print("VER", values) elif partsystem != None: for p in partsystem.particles: values.append( ( p.location[0] + dd.DataStore.impactLocation[0], p.location[1] + dd.DataStore.impactLocation[1], p.location[2] + dd.DataStore.impactLocation[2], ) ) else: for i in range(0, particles): print(xmin, xmax, ymin, ymax, zmin, zmax) randX = random.uniform(xmin, xmax) randY = random.uniform(ymin, ymax) randZ = random.uniform(zmin, zmax) values.append((randX, randY, randZ)) for i in range(0, particles): x = values[i][0] y = values[i][1] z = values[i][2] # if con.point_inside(x, y, z): print("Inserting", x, y, z) con.put(i, x, y, z) # d.add_wall(colist) name = obj.destruction.voro_path con.print_custom("%P v %t", name) del con # oldnames = [o.name for o in context.scene.objects] print("Library Time ", clock() - start) start = clock() records = parseFile(name) print("Parsing Time ", clock() - start) start = clock() # do a remesh here if desired and try to fix non-manifolds context.scene.objects.active = obj if not walls: if obj.destruction.remesh_depth > 0: rem = obj.modifiers.new("Remesh", "REMESH") rem.mode = "SHARP" rem.octree_depth = obj.destruction.remesh_depth rem.scale = 0.9 rem.sharpness = 1.0 rem.remove_disconnected_pieces = False # rem.threshold = 1.0 # context.scene.objects.active = obj ctx = context.copy() ctx["object"] = obj ctx["modifier"] = rem ops.object.modifier_apply(ctx, apply_as="DATA", modifier=rem.name) # [deselect(o) for o in context.scene.objects] # try to fix non-manifolds... # fixNonManifolds(obj) objs = buildCellMesh(records, obj.name, walls, diff, mat_index, re_unwrap, smart_angle, dissolve_angle) print("Mesh Construction Time ", clock() - start) return objs
def voronoiCube(context, obj, parts, vol, walls, mat_index): re_unwrap = obj.destruction.re_unwrap smart_angle = obj.destruction.smart_angle dissolve_angle = obj.destruction.dissolve_angle #applyscale before # global start start = clock() loc = Vector(obj.location) obj.destruction.origLoc = loc if vol != None and vol != "": print("USING VOL") volobj = context.scene.objects[vol] volobj.select = True #ops.object.origin_set(type='ORIGIN_GEOMETRY') ops.object.transform_apply(scale=True, rotation=True) volobj.select = False print("I: ", dd.DataStore.impactLocation) vxmin, vxmax, vymin, vymax, vzmin, vzmax = corners( volobj, dd.DataStore.impactLocation - loc) vxmin += loc[0] vxmax += loc[0] vymin += loc[1] vymax += loc[1] vzmin += loc[2] vzmax += loc[2] [deselect(o) for o in bpy.data.objects] context.scene.objects.active = obj obj.select = True mesh_center = Vector((0, 0, 0)) diff = Vector((0, 0, 0)) oldCur = context.scene.cursor_location.copy() if not walls: #memorize old origin and mesh_center(bounds) area = None for a in context.screen.areas: if a.type == 'VIEW_3D': area = a ctx = context.copy() ctx["area"] = area #ops.object.mode_set(mode = 'EDIT') #ctx["edit_object"] = obj #ops.mesh.select_all(action = 'SELECT') ops.view3d.snap_cursor_to_selected(ctx) old_orig = context.scene.cursor_location.copy() ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS') ops.view3d.snap_cursor_to_selected(ctx) mesh_center = context.scene.cursor_location.copy() #ops.mesh.select_all(action = 'DESELECT') # ops.object.mode_set(mode = 'OBJECT') diff = old_orig - mesh_center print(mesh_center, old_orig, diff) obj.destruction.tempLoc = diff context.scene.cursor_location = old_orig ops.object.origin_set(type='ORIGIN_CURSOR') ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS') else: context.scene.cursor_location = (0, 0, 0) ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS') ops.object.transform_apply(scale=True, location=True, rotation=True) [select(o) for o in context.scene.objects] xmin, xmax, ymin, ymax, zmin, zmax = corners(obj) xmin += loc[0] xmax += loc[0] ymin += loc[1] ymax += loc[1] zmin += loc[2] zmax += loc[2] nx = 12 ny = 12 nz = 12 particles = parts print(xmin, xmax, ymin, ymax, zmin, zmax) if vol != None and vol != "" and context.object.destruction.voro_exact_shape: volobj = context.scene.objects[vol] particles = len(volobj.data.vertices) partsystem = None if context.object.destruction.voro_particles != "": partsystemname = context.object.destruction.voro_particles volobj = context.scene.objects[vol] partsystem = volobj.particle_systems[partsystemname] particles = len(partsystem.particles) #enlarge container a bit, so parts near the border wont be cut off theta = 0.5 if walls: theta = 10 con = voronoi.domain(xmin - theta, xmax + theta, ymin - theta, ymax + theta, zmin - theta, zmax + theta, nx, ny, nz, False, False, False, particles) if vol != None and vol != "": xmin = vxmin xmax = vxmax ymin = vymin ymax = vymax zmin = vzmin zmax = vzmax print("VOL: ", xmin, xmax, ymin, ymax, zmin, zmax) bm = obj.data if walls: colist = [] i = 0 for poly in bm.polygons: # n = p.calc_center_median() n = poly.normal v = bm.vertices[poly.vertices[0]].co d = n.dot(v) # print("Displacement: ", d) colist.append([n[0], n[1], n[2], d, i]) i = i + 1 #add a wall object per face con.add_wall(colist) values = [] if vol != None and vol != "" and context.object.destruction.voro_exact_shape and partsystem == None: volobj = context.scene.objects[vol] #context.scene.objects.active = volobj #volobj.select = True # ops.object.transform_apply(scale=True, location = True, rotation = True) #volobj.select = False # impact location comes from game engine, is 0 by default # in game engine is location of volobj 0 by default, so this expression should be correct. for v in volobj.data.vertices: values.append( (v.co[0] + dd.DataStore.impactLocation[0] + volobj.location[0], v.co[1] + dd.DataStore.impactLocation[1] + volobj.location[1], v.co[2] + dd.DataStore.impactLocation[2] + volobj.location[2])) #print("VER", values) elif partsystem != None: for p in partsystem.particles: values.append((p.location[0] + dd.DataStore.impactLocation[0], p.location[1] + dd.DataStore.impactLocation[1], p.location[2] + dd.DataStore.impactLocation[2])) else: for i in range(0, particles): print(xmin, xmax, ymin, ymax, zmin, zmax) randX = random.uniform(xmin, xmax) randY = random.uniform(ymin, ymax) randZ = random.uniform(zmin, zmax) values.append((randX, randY, randZ)) for i in range(0, particles): x = values[i][0] y = values[i][1] z = values[i][2] #if con.point_inside(x, y, z): print("Inserting", x, y, z) con.put(i, x, y, z) # d.add_wall(colist) name = obj.destruction.voro_path con.print_custom("%P v %t", name) del con #oldnames = [o.name for o in context.scene.objects] print("Library Time ", clock() - start) start = clock() records = parseFile(name) print("Parsing Time ", clock() - start) start = clock() #do a remesh here if desired and try to fix non-manifolds context.scene.objects.active = obj if not walls: if obj.destruction.remesh_depth > 0: rem = obj.modifiers.new("Remesh", 'REMESH') rem.mode = 'SHARP' rem.octree_depth = obj.destruction.remesh_depth rem.scale = 0.9 rem.sharpness = 1.0 rem.remove_disconnected_pieces = False # rem.threshold = 1.0 #context.scene.objects.active = obj ctx = context.copy() ctx["object"] = obj ctx["modifier"] = rem ops.object.modifier_apply(ctx, apply_as='DATA', modifier=rem.name) #[deselect(o) for o in context.scene.objects] #try to fix non-manifolds... #fixNonManifolds(obj) objs = buildCellMesh(records, obj.name, walls, diff, mat_index, re_unwrap, smart_angle, dissolve_angle) print("Mesh Construction Time ", clock() - start) context.scene.cursor_location = oldCur return objs
def voronoiCube(context, obj, parts, vol, walls): #applyscale before # global start start = clock() loc = Vector(obj.location) obj.destruction.tempLoc = loc if vol != None and vol != "": print("USING VOL") volobj = context.scene.objects[vol] volobj.select = True ops.object.origin_set(type='GEOMETRY_ORIGIN') ops.object.transform_apply(scale=True, rotation = True) volobj.select = False vxmin, vxmax, vymin, vymax, vzmin, vzmax = corners(volobj) vxmin += loc[0] vxmax += loc[0] vymin += loc[1] vymax += loc[1] vzmin += loc[2] vzmax += loc[2] context.scene.objects.active = obj obj.select = True if not walls: ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS') else: ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS') ops.object.transform_apply(scale=True, location = True, rotation=True) obj.select = False xmin, xmax, ymin, ymax, zmin, zmax = corners(obj) xmin += loc[0] xmax += loc[0] ymin += loc[1] ymax += loc[1] zmin += loc[2] zmax += loc[2] nx = 12 ny = 12 nz = 12 particles = parts print(xmin, xmax, ymin, ymax, zmin, zmax) if vol != None and vol != "" and context.object.destruction.voro_exact_shape: volobj = context.scene.objects[vol] particles = len(volobj.data.vertices) partsystem = None if context.object.destruction.voro_particles != "": partsystemname = context.object.destruction.voro_particles volobj = context.scene.objects[vol] partsystem = volobj.particle_systems[partsystemname] particles = len(partsystem.particles) #enlarge container a bit, so parts near the border wont be cut off theta = 0.25 if walls: theta = 10 con = voronoi.domain(xmin-theta,xmax+theta,ymin-theta,ymax+theta,zmin-theta,zmax+theta,nx,ny,nz,False, False, False, particles) if vol != None and vol != "": xmin = vxmin xmax = vxmax ymin = vymin ymax = vymax zmin = vzmin zmax = vzmax bm = obj.data if walls: colist = [] i = 0 for poly in bm.polygons: # n = p.calc_center_median() n = poly.normal v = bm.vertices[poly.vertices[0]].co d = n.dot(v) # print("Displacement: ", d) colist.append([n[0], n[1], n[2], d, i]) i = i+1 #add a wall object per face con.add_wall(colist) values = [] if vol != None and vol != "" and context.object.destruction.voro_exact_shape: volobj = context.scene.objects[vol] context.scene.objects.active = volobj volobj.select = True ops.object.transform_apply(scale=True, location = True, rotation = True) volobj.select = False for v in volobj.data.vertices: values.append((v.co[0], v.co[1], v.co[2])) elif partsystem != None: for p in partsystem.particles: values.append((p.location[0], p.location[1], p.location[2])) else: for i in range(0, particles): print (xmin, xmax, ymin, ymax, zmin, zmax) randX = random.uniform(xmin, xmax) randY = random.uniform(ymin, ymax) randZ = random.uniform(zmin, zmax) values.append((randX, randY, randZ)) for i in range(0, particles): x = values[i][0] y = values[i][1] z = values[i][2] #if con.point_inside(x, y, z): print("Inserting", x, y, z) con.put(i, x, y, z) # d.add_wall(colist) name = obj.destruction.voro_path con.print_custom("%P v %t", name ) del con oldnames = [o.name for o in context.scene.objects] print("Library Time ", clock() - start) start = clock() records = parseFile(name) print("Parsing Time ", clock() - start) start = clock() context.scene.objects.active = obj buildCellMesh(records, obj.name, walls) print("Mesh Construction Time ", clock() - start) start = clock() if not walls: if obj.destruction.remesh_depth > 0: rem = obj.modifiers.new("Remesh", 'REMESH') rem.mode = 'SHARP' rem.octree_depth = obj.destruction.remesh_depth rem.scale = 0.9 rem.sharpness = 1.0 rem.remove_disconnected_pieces = False # rem.threshold = 1.0 context.scene.objects.active = obj ctx = context.copy() ctx["object"] = obj ops.object.modifier_apply(ctx, apply_as='DATA', modifier = rem.name) for o in context.scene.objects: if o.name not in oldnames: context.scene.objects.active = o booleanIntersect(context, o, obj) if len(o.data.vertices) == 0: context.scene.objects.unlink(o) else: oldnames.append(o.name) print("Boolean Time ", clock() - start)