Page MenuHome

python error in batch_for_shader when using more than one "in" parameter in a custom shader
Closed, InvalidPublic


System Information
Operating system: Ubuntu
Graphics card: GTX 780

Blender Version

Short description of error
ValueError: Unknown attribute name

2.80/scripts/modules/gpu_extras/", 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 ='ground')
mesh = obj

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')

    "co", np.reshape(vertices, len(mesh.vertices) * 3))
    "normal", np.reshape(normals, len(mesh.vertices) * 3))
    "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},

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



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/
(gdb) bt
#0  0x00007ffff745f3de in ?? () from /usr/lib64/
#1  0x00007ffff745fac6 in PyUnicode_FromFormatV () from /usr/lib64/
#2  0x00007ffff74adffd in PyErr_FormatV () from /usr/lib64/
#3  0x00007ffff74ad5bc in PyErr_Format () from /usr/lib64/
#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/
#7  0x00007ffff73f104a in _PyMethodDescr_FastCallKeywords () from /usr/lib64/
#8  0x00007ffff73c82ea in _PyEval_EvalFrameDefault () from /usr/lib64/
#9  0x00007ffff73c0261 in ?? () from /usr/lib64/
#10 0x00007ffff73e86a4 in _PyFunction_FastCallDict () from /usr/lib64/
#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.

The crash probably occurs because you are attempting to assign a value to a non-existent uniform "viewProjectionMatrix" instead of "ModelViewProjectionMatrix".
But in fact this should not crash.
I'll take a look.

The crash has been fixed in rBf2da21e0523d but the original report bug is a known limitation in the GLSL code and therefore is not a bug.