0
$\begingroup$

I want to draw a point on every vertex of an instanced mesh in a particle system. I put together this short script to do that. It runs without error but nothing happens. The console puts out this message:

File "C:\XXXXXX\XXXXXXX\XXXXXXXX\particle draw.blend\Draw Particle Points", line 23, in draw
TypeError: Vector multiplication: not supported between 'Vector' and 'list' types

So it's not possible to multiply a vector with a list which makes sense. What do i have to do to multiply the vector p.location with all inst_verts_coords list items

import bpy
import gpu
from mathutils import Matrix
from gpu_extras.batch import batch_for_shader

# get the depsgraph and the evaluated object   
dg = bpy.context.evaluated_depsgraph_get()
eva_obj = bpy.context.object.evaluated_get(dg)
# assume context object has a ps, use active
ps = eva_obj.particle_systems.active

#obj = bpy.context.active_object
inst_obj = ps.settings.instance_object
inst_obj_verts = inst_obj.data.vertices[0]
inst_verts_coords = [(inst_obj.matrix_world @ inst_obj_verts.co) for inst_obj_verts in inst_obj.data.vertices]

def draw():
    for p in ps.particles:
        #make a matrix for the particles
        M = p.rotation.to_matrix().to_4x4()
        M.translation = p.location

        #translate instance object vertex coords to particle coords
        part_obj_coords = [p.location @ inst_verts_coords]   
        #store vertex coordinates of instance object
        shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
        batch = batch_for_shader(shader, 'POINTS', {"pos": part_obj_coords})

        shader.bind()
        shader.uniform_float("color", (1, 1, 0, 1))
        batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

In case I'm missing something else give me a heads up.

$\endgroup$
2
$\begingroup$

inst_verts_coords is a list of coordinates (Vectors). So you won't be able to apply a matrix to this list. Maybe you want to change the following line (assuming you want to apply matrix to each individual Vector element of the list):

part_obj_coords = [p.location @ inst_verts_coords]

to:

part_obj_coords = [M @ co for co in inst_verts_coords]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.