[Bf-blender-cvs] [2e52b3206cc] master: PyDoc: Fix warnings from out of bounds include statement
Aaron Carlisle
noreply at git.blender.org
Wed Jun 3 00:21:42 CEST 2020
Commit: 2e52b3206cc6a108e2b832c0a5c38684402565ea
Author: Aaron Carlisle
Date: Tue Jun 2 18:20:15 2020 -0400
Branches: master
https://developer.blender.org/rB2e52b3206cc6a108e2b832c0a5c38684402565ea
PyDoc: Fix warnings from out of bounds include statement
===================================================================
M doc/python_api/examples/gpu.1.py
M doc/python_api/examples/gpu.10.py
D doc/python_api/examples/gpu.11.py
M doc/python_api/examples/gpu.2.py
M doc/python_api/examples/gpu.3.py
M doc/python_api/examples/gpu.4.py
M doc/python_api/examples/gpu.5.py
M doc/python_api/examples/gpu.6.py
M doc/python_api/examples/gpu.7.py
M doc/python_api/examples/gpu.8.py
M doc/python_api/examples/gpu.9.py
===================================================================
diff --git a/doc/python_api/examples/gpu.1.py b/doc/python_api/examples/gpu.1.py
index 831349e5430..a014e69c2d2 100644
--- a/doc/python_api/examples/gpu.1.py
+++ b/doc/python_api/examples/gpu.1.py
@@ -119,4 +119,24 @@ Examples
To try these examples, just copy them into Blenders text editor and execute them.
To keep the examples relatively small, they just register a draw function that can't easily be removed anymore.
Blender has to be restarted in order to delete the draw handlers.
+
+3D Lines with Single Color
+--------------------------
"""
+
+import bpy
+import gpu
+from gpu_extras.batch import batch_for_shader
+
+coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)]
+shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
+batch = batch_for_shader(shader, 'LINES', {"pos": coords})
+
+
+def draw():
+ shader.bind()
+ shader.uniform_float("color", (1, 1, 0, 1))
+ batch.draw(shader)
+
+
+bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
diff --git a/doc/python_api/examples/gpu.10.py b/doc/python_api/examples/gpu.10.py
index a4db576ecc0..2f255b7127d 100644
--- a/doc/python_api/examples/gpu.10.py
+++ b/doc/python_api/examples/gpu.10.py
@@ -1,41 +1,65 @@
"""
-Rendering the 3D View into a Texture
-------------------------------------
+Custom Shader for dotted 3D Line
+--------------------------------
-The scene has to have a camera for this example to work.
-You could also make this independent of a specific camera,
-but Blender does not expose good functions to create view and projection matrices yet.
+In this example the arc length (distance to the first point on the line) is calculated in every vertex.
+Between the vertex and fragment shader that value is automatically interpolated
+for all points that will be visible on the screen.
+In the fragment shader the ``sin`` of the arc length is calculated.
+Based on the result a decision is made on whether the fragment should be drawn or not.
"""
import bpy
-import bgl
import gpu
-from gpu_extras.presets import draw_texture_2d
+from random import random
+from mathutils import Vector
+from gpu_extras.batch import batch_for_shader
-WIDTH = 512
-HEIGHT = 256
+vertex_shader = '''
+ uniform mat4 u_ViewProjectionMatrix;
-offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT)
+ in vec3 position;
+ in float arcLength;
+ out float v_ArcLength;
-def draw():
- context = bpy.context
- scene = context.scene
+ void main()
+ {
+ v_ArcLength = arcLength;
+ gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);
+ }
+'''
+
+fragment_shader = '''
+ uniform float u_Scale;
+
+ in float v_ArcLength;
- view_matrix = scene.camera.matrix_world.inverted()
+ void main()
+ {
+ if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;
+ gl_FragColor = vec4(1.0);
+ }
+'''
- projection_matrix = scene.camera.calc_matrix_camera(
- context.evaluated_depsgraph_get(), x=WIDTH, y=HEIGHT)
+coords = [Vector((random(), random(), random())) * 5 for _ in range(5)]
- offscreen.draw_view3d(
- scene,
- context.view_layer,
- context.space_data,
- context.region,
- view_matrix,
- projection_matrix)
+arc_lengths = [0]
+for a, b in zip(coords[:-1], coords[1:]):
+ arc_lengths.append(arc_lengths[-1] + (a - b).length)
- bgl.glDisable(bgl.GL_DEPTH_TEST)
- draw_texture_2d(offscreen.color_texture, (10, 10), WIDTH, HEIGHT)
+shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
+batch = batch_for_shader(
+ shader, 'LINE_STRIP',
+ {"position": coords, "arcLength": arc_lengths},
+)
+
+
+def draw():
+ shader.bind()
+ matrix = bpy.context.region_data.perspective_matrix
+ shader.uniform_float("u_ViewProjectionMatrix", matrix)
+ shader.uniform_float("u_Scale", 10)
+ batch.draw(shader)
-bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
+bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
diff --git a/doc/python_api/examples/gpu.11.py b/doc/python_api/examples/gpu.11.py
deleted file mode 100644
index 2f255b7127d..00000000000
--- a/doc/python_api/examples/gpu.11.py
+++ /dev/null
@@ -1,65 +0,0 @@
-"""
-Custom Shader for dotted 3D Line
---------------------------------
-
-In this example the arc length (distance to the first point on the line) is calculated in every vertex.
-Between the vertex and fragment shader that value is automatically interpolated
-for all points that will be visible on the screen.
-In the fragment shader the ``sin`` of the arc length is calculated.
-Based on the result a decision is made on whether the fragment should be drawn or not.
-"""
-import bpy
-import gpu
-from random import random
-from mathutils import Vector
-from gpu_extras.batch import batch_for_shader
-
-vertex_shader = '''
- uniform mat4 u_ViewProjectionMatrix;
-
- in vec3 position;
- in float arcLength;
-
- out float v_ArcLength;
-
- void main()
- {
- v_ArcLength = arcLength;
- gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);
- }
-'''
-
-fragment_shader = '''
- uniform float u_Scale;
-
- in float v_ArcLength;
-
- void main()
- {
- if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;
- gl_FragColor = vec4(1.0);
- }
-'''
-
-coords = [Vector((random(), random(), random())) * 5 for _ in range(5)]
-
-arc_lengths = [0]
-for a, b in zip(coords[:-1], coords[1:]):
- arc_lengths.append(arc_lengths[-1] + (a - b).length)
-
-shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
-batch = batch_for_shader(
- shader, 'LINE_STRIP',
- {"position": coords, "arcLength": arc_lengths},
-)
-
-
-def draw():
- shader.bind()
- matrix = bpy.context.region_data.perspective_matrix
- shader.uniform_float("u_ViewProjectionMatrix", matrix)
- shader.uniform_float("u_Scale", 10)
- batch.draw(shader)
-
-
-bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
diff --git a/doc/python_api/examples/gpu.2.py b/doc/python_api/examples/gpu.2.py
index d1e8ac32589..4bee9acebe3 100644
--- a/doc/python_api/examples/gpu.2.py
+++ b/doc/python_api/examples/gpu.2.py
@@ -1,19 +1,45 @@
"""
-3D Lines with Single Color
---------------------------
+Triangle with Custom Shader
+---------------------------
"""
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
-coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)]
-shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
-batch = batch_for_shader(shader, 'LINES', {"pos": coords})
+vertex_shader = '''
+ uniform mat4 viewProjectionMatrix;
+
+ in vec3 position;
+ out vec3 pos;
+
+ void main()
+ {
+ pos = position;
+ gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
+ }
+'''
+
+fragment_shader = '''
+ uniform float brightness;
+
+ in vec3 pos;
+
+ void main()
+ {
+ gl_FragColor = vec4(pos * brightness, 1.0);
+ }
+'''
+
+coords = [(1, 1, 1), (2, 0, 0), (-2, -1, 3)]
+shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
+batch = batch_for_shader(shader, 'TRIS', {"position": coords})
def draw():
shader.bind()
- shader.uniform_float("color", (1, 1, 0, 1))
+ matrix = bpy.context.region_data.perspective_matrix
+ shader.uniform_float("viewProjectionMatrix", matrix)
+ shader.uniform_float("brightness", 0.5)
batch.draw(shader)
diff --git a/doc/python_api/examples/gpu.3.py b/doc/python_api/examples/gpu.3.py
index 4bee9acebe3..0c86b52bcf5 100644
--- a/doc/python_api/examples/gpu.3.py
+++ b/doc/python_api/examples/gpu.3.py
@@ -1,45 +1,29 @@
"""
-Triangle with Custom Shader
----------------------------
+Wireframe Cube using Index Buffer
+---------------------------------
"""
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
-vertex_shader = '''
- uniform mat4 viewProjectionMatrix;
+coords = (
+ (-1, -1, -1), (+1, -1, -1),
+ (-1, +1, -1), (+1, +1, -1),
+ (-1, -1, +1), (+1, -1, +1),
+ (-1, +1, +1), (+1, +1, +1))
- in vec3 position;
- out vec3 pos;
+indices = (
+ (0, 1), (0, 2), (1, 3), (2, 3),
+ (4, 5), (4, 6), (5, 7), (6, 7),
+ (0, 4), (1, 5), (2, 6), (3, 7))
- void main()
- {
- pos = position;
- gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
- }
-'''
-
-fragment_shader = '''
- uniform float brightness;
-
- in vec3 pos;
-
- void main()
- {
- gl_FragColor = vec4(pos * brightness, 1.0);
- }
-'''
-
-coords = [(1, 1, 1), (2, 0, 0), (-2, -1, 3)]
-shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
-batch = batch_for_shader(shader, 'TRIS', {"position": coords})
+shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
+batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)
def draw():
shader.bind()
- matrix = bpy.context.region_data.perspective_matrix
- shader.uniform_float("viewProjectionMatrix", matrix)
- shader.uniform_float("brightness", 0.5)
+ shader.uniform_float("color", (1, 0, 0, 1))
batch.draw(shader)
diff --git a/doc/python_api/examples/gpu.4.py b/doc/python_api/examples/gpu.4.py
index 0c86b52bcf5..e05290a9442 100644
--- a/doc/python_api/examples/gpu.4.py
+++ b/doc/python_api/examples/gpu.4.py
@@ -1,30 +1,39 @@
"""
-Wireframe Cube using Index Buffer
----------------------------------
+Mesh with Random Vertex Colors
+------------------------------
"""
import bpy
import gpu
+import bgl
+import numpy as np
+from random import random
from gpu_extras.batch import batch_for_shader
-coords = (
- (-1, -1, -1), (+1, -1, -1),
- (-1, +1, -1), (+1, +1, -1),
- (-1, -1, +1), (+1, -1, +1),
- (-1, +1, +1), (+1, +1, +1))
+mesh = bpy.context.active_object.data
+mesh.calc_loop_triangles()
-indices = (
- (0, 1), (0, 2), (1, 3), (2, 3),
- (4, 5), (4, 6), (5, 7), (6, 7),
- (0, 4), (1, 5), (2, 6), (3, 7))
+vertices = np.empty((len(mesh.vertices), 3), 'f')
+indices = np.empty((len(mesh.loop_triangles), 3), 'i')
-shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
-batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)
+mesh.vertices.foreach_get(
+ "co", np.reshape(vertices, len(mesh.v
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list