Hello, I am trying to use Bledenr to visualize my molecular dynamics results. I have nearly 30,000 particles and nearly 10,000 chemical bonds. So I had to use particle system for modeling. I have read an related topic and try my code as:
import bpy
import numpy as np
import gsd.hoomd
from mathutils import Vector;
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False,confirm=False)
with gsd.hoomd.open("trajecoryt.gsd") as traj:
snap = traj[-2]
radius = [ 0.01, 0.01, 0.2, 0.2, 0.2, 0.2, 0.2, 4, 4]
boxsize = snap.configuration.box.tolist()[1]
#############################
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_2.80", type='SUN')
light_data.energy = 3
# create new object with our light datablock
light_object = bpy.data.objects.new(name="light_2.80", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object)
# make it active
bpy.context.view_layer.objects.active = light_object
#change location
light_object.location = (50, 50, 50)
###############################
camera_data = bpy.data.cameras.new(name='Camera')
camera_object = bpy.data.objects.new('Camera', camera_data)
bpy.context.scene.collection.objects.link(camera_object)
##################################
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get()
dg.update()
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(-999, -999, -999), scale=(1, 1, 1))
box = bpy.ops.mesh.primitive_cube_add(location = (0,0,0),scale = (boxsize/2,boxsize/2,boxsize/2))
bpy.context.object.name = "Cube"
obj = bpy.context.active_object
bpy.ops.object.shade_smooth()
for index,type in enumerate(snap.particles.types):
obj.modifiers.new(type, type='PARTICLE_SYSTEM')
N_particles = sum(snap.particles.typeid == index)
bpy.context.active_object.particle_systems[type].settings.count = N_particles
bpy.context.active_object.particle_systems[type].settings.emit_from = 'VOLUME'
bpy.context.active_object.particle_systems[type].settings.render_type = 'OBJECT'
bpy.context.active_object.particle_systems[type].settings.instance_object = bpy.data.objects["Sphere"]
bpy.context.active_object.particle_systems[type].settings.particle_size = radius[index]
bpy.context.active_object.particle_systems[type].settings.frame_start = 0
bpy.context.active_object.particle_systems[type].settings.frame_end = 0
bpy.context.active_object.particle_systems[type].settings.normal_factor = 0
bpy.context.active_object.particle_systems[type].settings.effector_weights.gravity = 0
bpy.context.active_object.particle_systems[type].settings.lifetime = 10000
bpy.context.object.show_instancer_for_render = False
bpy.context.object.show_instancer_for_viewport = False
# Dependancy graph
degp = bpy.context.evaluated_depsgraph_get()
# Emitter Object
object = bpy.data.objects["Cube"]
# Evaluate the depsgraph (Important step)
particle_systems = object.evaluated_get(degp).particle_systems
scene = bpy.context.scene
cFrame = scene.frame_current
sFrame = scene.frame_start
# at start-frame, clear the particle cache
if cFrame == sFrame:
psSeed = object.particle_systems[0].seed
object.particle_systems[0].seed = psSeed
# All particles of first particle-system which has index "0"
for index,type in enumerate(snap.particles.types):
particles = particle_systems[type].particles
# Total Particles
totalParticles = len(particles)
# length of 1D array or list = 3*totalParticles, "3" due to XYZ in vector/location.
# If the length is wrong then it will give you an error "internal error setting the array"
# flatList = [0]*(3*totalParticles)
# To get the loaction of all particles
particles.foreach_set("location",snap.particles.position[snap.particles.typeid==index].flatten())
I haven’t considered making the result move, so I only set one frame. And I set up different particle systems for different atoms. The output looks like great as:
However, after rendering, the position of the particls changed. And I even found that it will happend after I switch to the edit model and then back to object model.
I think that It may be related to the source of the particle system. Did I forget something?