 # Blender as Electic-Fields visualizer

Over the days I wrote a little script which calculates the FieldLines of a charged mesh’s volume.

Usage:

• Add charges to the Object as a Particle System
(Every Partice represents a point charge)
• Reconfigure Parameters of the script (charge and numSegments are the important ones)
• Run Script
• Get great results !

Todo: - Multible Particels as Field influence
- Make the whole thing animatable
- Positive charges

Have fun and let me see what you’re doing with it.

Demo Model Twisted Torus with FieldLines:

Screenshot:

Script:

import bpy,math
from mathutils import Vector

charge = 0.0001
numSegments = 250
w = 1

#get the first selected Object
object = bpy.context.selected_objects

#get the charges in the Volume of the mesh (=Input)
charges = object.particle_systems.particles

#so for each “line” do the pyhisics calulation
for j, v in enumerate(object.data.vertices):
#for each segment of the line calculate the points for
#the field lines

``````#generate the resulting fieldline-curve
fieldline = bpy.data.curves.new(
name=object.name+'_fieldLine'+str(j),
type='CURVE')

fieldline.dimensions = '3D'

polyline = fieldline.splines.new('POLY')

#print the current progress of the calculations (how many fieldLines are
print(j,len(object.data.vertices))

for i in range(numSegments):
if(i == 0):
#get the starting point of each fieldLine for easier access
x = v.co.x
y = v.co.y
z = v.co.z
else:
#calculate the resulting field-vector by superposition(=Sum)
#of the charge's force at the current point
fx = 0
fy = 0
fz = 0

for c in charges:
#Get the current vector which connects our fieldLines's point
#with the current charge (From charge To point -&gt; (From - To))
dx = x - c.location.x
dy = y - c.location.y
dz = z - c.location.z
r = math.sqrt(dx*dx+dy*dy+dz*dz)

fx += charge * (dx / (r*r*r))
fy += charge * (dy / (r*r*r))
fz += charge * (dz / (r*r*r))

x += fx
y += fy
z += fz
``````
``````    #add the fieldline's vertices to our calculated result
polyline.points[i].co = (x, y, z, w)

#paste the existing pydata into the object
objectdata = bpy.data.objects.new(object.name+"FieldLine"+str(j), fieldline)
objectdata.location = (0,0,0) #object origin