Page MenuHome

python error in batch_for_shader when using more than one "in" parameter in a custom shader
Open, Needs Triage by DeveloperPublic

Description

System Information
Operating system: Ubuntu
Graphics card: GTX 780

Blender Version
blender-2.80-14884cda1ff5-linux-glibc224-x86_64

Short description of error
ValueError: Unknown attribute name

2.80/scripts/modules/gpu_extras/batch.py", line 55, in batch_for_shader

vbo.attr_fill(id, data)

The error occurs with the 2nd "in" attribute of my custom vertex shader (namely: in vec3 norm;)

Exact steps for others to reproduce the error

With a simple mesh named "ground" just run the following python script

import bpy
import gpu
import numpy as np
from random import random
from gpu_extras.batch import batch_for_shader


vertex_shader = '''
uniform mat4 ModelViewProjectionMatrix;

in vec3 pos;
in vec3 norm;

void main()
{
        gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
}

'''

fragment_shader = '''
out vec4 fragColor;

void main()
{
    fragColor = vec4(1.0, 0.0, 0.0, 0.1);
}
'''

obj = bpy.data.meshes.get('ground')
mesh = obj
mesh.calc_loop_triangles()

vertices = np.empty((len(mesh.vertices), 3), 'f')
normals = np.empty((len(mesh.vertices), 3), 'f')
indices = np.empty((len(mesh.loop_triangles), 3), 'i')

mesh.vertices.foreach_get(
    "co", np.reshape(vertices, len(mesh.vertices) * 3))
mesh.vertices.foreach_get(
    "normal", np.reshape(normals, len(mesh.vertices) * 3))
mesh.loop_triangles.foreach_get(
    "vertices", np.reshape(indices, len(mesh.loop_triangles) * 3))

shader = gpu.types.GPUShader(vertex_shader, fragment_shader)

batch = batch_for_shader(
    shader, 'TRIS',
    {"pos": vertices, "norm": normals},
    indices=indices,
)

def draw():
    shader.bind()
    matrix = bpy.context.region_data.perspective_matrix
    shader.uniform_float("viewProjectionMatrix", matrix)
    batch.draw(shader)
    
    
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

Details

Type
Bug

Event Timeline

The issue probably is that your "norm" parameter gets optimized out as it is not used.
If I add it pos+norm it works. But crashes:

Thread 1 "blender" received signal SIGSEGV, Segmentation fault.
0x00007ffff745f3de in ?? () from /usr/lib64/libpython3.7m.so.1.0
(gdb) bt
#0  0x00007ffff745f3de in ?? () from /usr/lib64/libpython3.7m.so.1.0
#1  0x00007ffff745fac6 in PyUnicode_FromFormatV () from /usr/lib64/libpython3.7m.so.1.0
#2  0x00007ffff74adffd in PyErr_FormatV () from /usr/lib64/libpython3.7m.so.1.0
#3  0x00007ffff74ad5bc in PyErr_Format () from /usr/lib64/libpython3.7m.so.1.0
#4  0x0000555557cacaf5 in bpygpu_uniform_location_get (shader=0x7fffca457b78, name=0x7fffc87f2498 "viewProjectionMatrix",
    error_prefix=0x55555c4ddc6b "GPUShader.uniform_float") at /home/zed/prog/blender/source/blender/python/gpu/gpu_py_shader.c:88
#5  0x0000555557cad4c7 in bpygpu_shader_uniform_float (self=0x7fffc8ae3e40, Python Exception <class 'gdb.error'> No type named PyUnicodeObject.:
args=) at /home/zed/prog/blender/source/blender/python/gpu/gpu_py_shader.c:447
#6  0x00007ffff73e8c89 in _PyMethodDef_RawFastCallKeywords () from /usr/lib64/libpython3.7m.so.1.0
#7  0x00007ffff73f104a in _PyMethodDescr_FastCallKeywords () from /usr/lib64/libpython3.7m.so.1.0
#8  0x00007ffff73c82ea in _PyEval_EvalFrameDefault () from /usr/lib64/libpython3.7m.so.1.0
#9  0x00007ffff73c0261 in ?? () from /usr/lib64/libpython3.7m.so.1.0
#10 0x00007ffff73e86a4 in _PyFunction_FastCallDict () from /usr/lib64/libpython3.7m.so.1.0
#11 0x0000555557c552af in cb_region_draw (C=0x7fffe3858188, UNUSED_ar=0x7fffca13be08, customdata=0x7fffc962ef10)
    at /home/zed/prog/blender/source/blender/python/intern/bpy_rna_callback.c:70
#12 0x0000555557556936 in ED_region_draw_cb_draw (C=0x7fffe3858188, ar=0x7fffca13be08, type=0)
    at /home/zed/prog/blender/source/blender/editors/space_api/spacetypes.c:276
#13 0x00005555581f55c8 in DRW_draw_render_loop_ex (depsgraph=0x7fffde6a2408, engine_type=0x55555da5b500 <DRW_engine_viewport_eevee_type>, ar=0x7fffca13be08,
    v3d=0x7fffc9d75108, viewport=0x7fffca209668, evil_C=0x7fffe3858188) at /home/zed/prog/blender/source/blender/draw/intern/draw_manager.c:1568
#14 0x00005555581f4f0a in DRW_draw_view (C=0x7fffe3858188) at /home/zed/prog/blender/source/blender/draw/intern/draw_manager.c:1424
#15 0x000055555765d8fd in view3d_draw_view (C=0x7fffe3858188, ar=0x7fffca13be08)
    at /home/zed/prog/blender/source/blender/editors/space_view3d/view3d_draw.c:1388
#16 0x000055555765d9ad in view3d_main_region_draw (C=0x7fffe3858188, ar=0x7fffca13be08)
    at /home/zed/prog/blender/source/blender/editors/space_view3d/view3d_draw.c:1409
#17 0x0000555557b23cd7 in ED_region_do_draw (C=0x7fffe3858188, ar=0x7fffca13be08) at /home/zed/prog/blender/source/blender/editors/screen/area.c:508
#18 0x000055555750e79a in wm_draw_window_offscreen (C=0x7fffe3858188, win=0x7fffca7e3c88, stereo=false)
    at /home/zed/prog/blender/source/blender/windowmanager/intern/wm_draw.c:592
#19 0x000055555750ed18 in wm_draw_window (C=0x7fffe3858188, win=0x7fffca7e3c88) at /home/zed/prog/blender/source/blender/windowmanager/intern/wm_draw.c:725
#20 0x000055555750f224 in wm_draw_update (C=0x7fffe3858188) at /home/zed/prog/blender/source/blender/windowmanager/intern/wm_draw.c:887
#21 0x000055555750bee7 in WM_main (C=0x7fffe3858188) at /home/zed/prog/blender/source/blender/windowmanager/intern/wm.c:427
#22 0x000055555750657f in main (argc=1, argv=0x7fffffffe078) at /home/zed/prog/blender/source/creator/creator.c:503

So I guess something else is wrong with the code.