-
-
Save armindocachada/88d3733d69a3318e0cde5c4514a8f390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import bmesh | |
import math | |
import numpy as np | |
def getGeometryCenter(obj): | |
vcos = [ obj.matrix_world @ v.co for v in obj.data.vertices ] | |
findCenter = lambda l: ( max(l) + min(l) ) / 2 | |
x,y,z = [ [ v[i] for v in vcos ] for i in range(3) ] | |
center = [ findCenter(axis) for axis in [x,y,z] ] | |
print(center) | |
return center | |
def setOrigin(obj): | |
oldLoc = obj.location | |
newLoc = getGeometryCenter(obj) | |
for vert in obj.data.vertices: | |
vert.co[0] -= newLoc[0] - oldLoc[0] | |
vert.co[1] -= newLoc[1] - oldLoc[1] | |
vert.co[2] -= newLoc[2] - oldLoc[2] | |
obj.location = newLoc | |
def neuronsLayer(numberOfNodes=(10,10), origin=(0,0,0)): | |
# Create an empty mesh and the object. | |
mesh = bpy.data.meshes.new('Basic_Sphere') | |
basic_sphere = bpy.data.objects.new("Basic_Sphere", mesh) | |
basic_sphere.location= origin | |
# Add the object into the scene. | |
bpy.context.collection.objects.link(basic_sphere) | |
# Select the newly created object | |
bpy.context.view_layer.objects.active = basic_sphere | |
basic_sphere.select_set(True) | |
(nrows, ncols) = numberOfNodes | |
# Construct the bmesh sphere and assign it to the blender mesh. | |
bm = bmesh.new() | |
bmesh.ops.create_uvsphere(bm, u_segments=32, v_segments=16, diameter=1) | |
bm.to_mesh(mesh) | |
bm.free() | |
mod = basic_sphere.modifiers.new('Array', 'ARRAY') | |
mod.relative_offset_displace[0] = 1.5 | |
mod.relative_offset_displace[1] = 0 | |
mod.count = nrows | |
bpy.ops.object.modifier_apply(modifier='Array') | |
mod = basic_sphere.modifiers.new('Array', 'ARRAY') | |
mod.relative_offset_displace[0] = 0 | |
mod.relative_offset_displace[1] = 0 | |
mod.relative_offset_displace[2] = 1.5 | |
mod.count = ncols | |
bpy.ops.object.modifier_apply(modifier='Array') | |
print(basic_sphere.dimensions) | |
setOrigin(basic_sphere) | |
basic_sphere.location=origin | |
return basic_sphere | |
#neuron_layer1 = neuronsLayer(numberOfNodes=(12,12), origin=(0,0,0)) | |
#neuron_layer2 = neuronsLayer(numberOfNodes=(100,100), origin=(0,10,0)) | |
def midpoint(location_1, location_2): | |
(x1, y1, z1) = location_1 | |
(x2, y2, z2) = location_2 | |
x12 = (x1 +x2) / 2 | |
y12 = (y1+ y2) /2 | |
z12 = (z1 + z2)/ 2 | |
return (x12, y12, z12) | |
def distance(location_1, location_2): | |
(x1, y1, z1) = location_1 | |
(x2, y2, z2) = location_2 | |
distance = math.sqrt( (x2- x1)**2 + (y2- y1)**2 + (z2- z1)**2) | |
return distance | |
def determine_angle(vectora, vectorb): | |
(xa, ya, za) = vectora | |
(xb, yb, zb) = vectorb | |
dot_product= (xa * xb + ya * yb + za * zb) | |
sqrt_vectora = math.sqrt((xa ** 2 + ya**2 + za**2)) | |
sqrt_vectorb = math.sqrt((xb ** 2 + yb**2 + zb**2)) | |
product_sqrt = sqrt_vectora * sqrt_vectorb | |
angle = math.acos(dot_product / product_sqrt) | |
return angle | |
def connectNeurons( location_start, location_end): | |
x = location_end[0] - location_start[0] | |
y = location_end[1] - location_start[1] | |
z = location_end[2] - location_start[2] | |
dist = distance(location_end, location_start) | |
mag = math.sqrt(x**2 + y**2 ) | |
if x == 0: | |
beta = 0 | |
else: | |
beta = math.atan(y/x) | |
if (x <= 0 and y >= 0) or (x <= 0 and y <= 0): | |
beta = pi + beta | |
if mag == 0: | |
theta = pi/2 | |
else: | |
theta = math.atan(z/mag) | |
bpy.ops.mesh.primitive_cylinder_add( radius=0.1, depth=dist, scale=(1,1,1)) | |
gap = mag | |
cylinder = bpy.context.scene.objects["Cylinder"] | |
#cylinder.scale.z = gap/2 | |
cylinder.location = location_start + (location_end - location_start)/2 | |
cylinder.rotation_euler.z = beta | |
cylinder.rotation_euler.y = -theta + math.pi/2 | |
#def connectNeurons( location_start, location_end): | |
# dist = distance(location_start, location_end) | |
# midpt = midpoint(location_start, location_end) | |
# | |
# | |
# (mx, my, mz) = midpt | |
# | |
# | |
# rotate_z_vector = (mx, my, mz+1) | |
# print(rotate_z_vector) | |
# rotate_y_vector = (mx, my +1, mz) | |
# rotate_x_vector = (mx + 1, my, mz) | |
# | |
# angle_x = determine_angle( rotate_x_vector, rotate_z_vector) | |
# angle_y = determine_angle( rotate_y_vector,rotate_z_vector) | |
# | |
# | |
# print(f"midpoint{midpt}") | |
# angle = determine_angle((0.5,0, 1), location_end) | |
# print(f"angle={angle}") | |
# bpy.ops.mesh.primitive_cylinder_add( radius=0.1, depth=dist, location=midpt, scale=(1,1,1)) | |
# | |
# bpy.ops.transform.rotate(value=angle_x, orient_axis='X') | |
# bpy.ops.transform.rotate(value=angle_y, orient_axis='Y') | |
# #bpy.ops.transform.rotate(value=angle, orient_axis='Z') | |
print(midpoint((0,0,0), (1, 1, 1))) | |
print(distance((0,0,0), (1, 1, 1))) | |
connectNeurons(np.array((0,0,0)), np.array((1, 1, 1))) | |
#print(math.degrees(determine_angle( (1, 1, 2), (-4, -8, 6)))) | |
#math.radians(x) | |
#Convert angle x from degrees to radians. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment