Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'doc/python_api/examples')
-rw-r--r--doc/python_api/examples/bpy.types.RenderEngine.py72
-rw-r--r--doc/python_api/examples/gpu.4.py6
-rw-r--r--doc/python_api/examples/gpu.6.py11
-rw-r--r--doc/python_api/examples/gpu.7.py10
-rw-r--r--doc/python_api/examples/gpu.8.py13
-rw-r--r--doc/python_api/examples/gpu.9.py5
-rw-r--r--doc/python_api/examples/mathutils.Matrix.LocRotScale.py5
-rw-r--r--doc/python_api/examples/mathutils.Matrix.py6
8 files changed, 39 insertions, 89 deletions
diff --git a/doc/python_api/examples/bpy.types.RenderEngine.py b/doc/python_api/examples/bpy.types.RenderEngine.py
index 45910194244..0b8795340ad 100644
--- a/doc/python_api/examples/bpy.types.RenderEngine.py
+++ b/doc/python_api/examples/bpy.types.RenderEngine.py
@@ -4,7 +4,9 @@ Simple Render Engine
"""
import bpy
-import bgl
+import array
+import gpu
+from gpu_extras.presets import draw_texture_2d
class CustomRenderEngine(bpy.types.RenderEngine):
@@ -100,8 +102,7 @@ class CustomRenderEngine(bpy.types.RenderEngine):
dimensions = region.width, region.height
# Bind shader that converts from scene linear to display space,
- bgl.glEnable(bgl.GL_BLEND)
- bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA)
+ gpu.state.blend_set('ALPHA_PREMULT')
self.bind_display_space_shader(scene)
if not self.draw_data or self.draw_data.dimensions != dimensions:
@@ -110,7 +111,7 @@ class CustomRenderEngine(bpy.types.RenderEngine):
self.draw_data.draw()
self.unbind_display_space_shader()
- bgl.glDisable(bgl.GL_BLEND)
+ gpu.state.blend_set('NONE')
class CustomDrawData:
@@ -119,68 +120,21 @@ class CustomDrawData:
self.dimensions = dimensions
width, height = dimensions
- pixels = [0.1, 0.2, 0.1, 1.0] * width * height
- pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels)
+ pixels = width * height * array.array('f', [0.1, 0.2, 0.1, 1.0])
+ pixels = gpu.types.Buffer('FLOAT', width * height * 4, pixels)
# Generate texture
- self.texture = bgl.Buffer(bgl.GL_INT, 1)
- bgl.glGenTextures(1, self.texture)
- bgl.glActiveTexture(bgl.GL_TEXTURE0)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
- bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels)
- bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
- bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
+ self.texture = gpu.types.GPUTexture((width, height), format='RGBA16F', data=pixels)
- # Bind shader that converts from scene linear to display space,
- # use the scene's color management settings.
- shader_program = bgl.Buffer(bgl.GL_INT, 1)
- bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)
-
- # Generate vertex array
- self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
- bgl.glGenVertexArrays(1, self.vertex_array)
- bgl.glBindVertexArray(self.vertex_array[0])
-
- texturecoord_location = bgl.glGetAttribLocation(shader_program[0], "texCoord")
- position_location = bgl.glGetAttribLocation(shader_program[0], "pos")
-
- bgl.glEnableVertexAttribArray(texturecoord_location)
- bgl.glEnableVertexAttribArray(position_location)
-
- # Generate geometry buffers for drawing textured quad
- position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
- position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
- texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
- texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)
-
- self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)
-
- bgl.glGenBuffers(2, self.vertex_buffer)
- bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
- bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
- bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None)
-
- bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
- bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
- bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None)
-
- bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
- bgl.glBindVertexArray(0)
+ # Note: This is just a didactic example.
+ # In this case it would be more convenient to fill the texture with:
+ # self.texture.clear('FLOAT', value=[0.1, 0.2, 0.1, 1.0])
def __del__(self):
- bgl.glDeleteBuffers(2, self.vertex_buffer)
- bgl.glDeleteVertexArrays(1, self.vertex_array)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
- bgl.glDeleteTextures(1, self.texture)
+ del self.texture
def draw(self):
- bgl.glActiveTexture(bgl.GL_TEXTURE0)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
- bgl.glBindVertexArray(self.vertex_array[0])
- bgl.glDrawArrays(bgl.GL_TRIANGLE_FAN, 0, 4)
- bgl.glBindVertexArray(0)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
+ draw_texture_2d(self.texture, (0, 0), self.texture.width, self.texture.height)
# RenderEngines also need to tell UI Panels that they are compatible with.
diff --git a/doc/python_api/examples/gpu.4.py b/doc/python_api/examples/gpu.4.py
index e05290a9442..b1a1ba827db 100644
--- a/doc/python_api/examples/gpu.4.py
+++ b/doc/python_api/examples/gpu.4.py
@@ -4,7 +4,6 @@ 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
@@ -31,9 +30,10 @@ batch = batch_for_shader(
def draw():
- bgl.glEnable(bgl.GL_DEPTH_TEST)
+ gpu.state.depth_test_set('LESS_EQUAL')
+ gpu.state.depth_mask_set(True)
batch.draw(shader)
- bgl.glDisable(bgl.GL_DEPTH_TEST)
+ gpu.state.depth_mask_set(False)
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
diff --git a/doc/python_api/examples/gpu.6.py b/doc/python_api/examples/gpu.6.py
index 69af65e163e..334a606055a 100644
--- a/doc/python_api/examples/gpu.6.py
+++ b/doc/python_api/examples/gpu.6.py
@@ -6,11 +6,11 @@ To use this example you have to provide an image that should be displayed.
"""
import bpy
import gpu
-import bgl
from gpu_extras.batch import batch_for_shader
IMAGE_NAME = "Untitled"
image = bpy.data.images[IMAGE_NAME]
+texture = gpu.texture.from_image(image)
shader = gpu.shader.from_builtin('2D_IMAGE')
batch = batch_for_shader(
@@ -21,16 +21,9 @@ batch = batch_for_shader(
},
)
-if image.gl_load():
- raise Exception()
-
-
def draw():
- bgl.glActiveTexture(bgl.GL_TEXTURE0)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode)
-
shader.bind()
- shader.uniform_int("image", 0)
+ shader.uniform_sampler("image", texture)
batch.draw(shader)
diff --git a/doc/python_api/examples/gpu.7.py b/doc/python_api/examples/gpu.7.py
index 80cda45e690..01082e7b6fe 100644
--- a/doc/python_api/examples/gpu.7.py
+++ b/doc/python_api/examples/gpu.7.py
@@ -9,7 +9,6 @@ Generate a texture using Offscreen Rendering
"""
import bpy
import gpu
-import bgl
from mathutils import Matrix
from gpu_extras.batch import batch_for_shader
from gpu_extras.presets import draw_circle_2d
@@ -20,8 +19,8 @@ from gpu_extras.presets import draw_circle_2d
offscreen = gpu.types.GPUOffScreen(512, 512)
with offscreen.bind():
- bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
- bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
+ fb = gpu.state.active_framebuffer_get()
+ fb.clear(color=(0.0, 0.0, 0.0, 0.0))
with gpu.matrix.push_pop():
# reset matrices -> use normalized device coordinates [-1, 1]
gpu.matrix.load_matrix(Matrix.Identity(4))
@@ -75,13 +74,10 @@ batch = batch_for_shader(
def draw():
- bgl.glActiveTexture(bgl.GL_TEXTURE0)
- bgl.glBindTexture(bgl.GL_TEXTURE_2D, offscreen.color_texture)
-
shader.bind()
shader.uniform_float("modelMatrix", Matrix.Translation((1, 2, 3)) @ Matrix.Scale(3, 4))
shader.uniform_float("viewProjectionMatrix", bpy.context.region_data.perspective_matrix)
- shader.uniform_float("image", 0)
+ shader.uniform_sampler("image", offscreen.texture_color)
batch.draw(shader)
diff --git a/doc/python_api/examples/gpu.8.py b/doc/python_api/examples/gpu.8.py
index e67c601def9..664f14a23ca 100644
--- a/doc/python_api/examples/gpu.8.py
+++ b/doc/python_api/examples/gpu.8.py
@@ -7,11 +7,10 @@ If it already exists, it will override the existing one.
Currently almost all of the execution time is spent in the last line.
In the future this will hopefully be solved by implementing the Python buffer protocol
-for :class:`bgl.Buffer` and :class:`bpy.types.Image.pixels` (aka ``bpy_prop_array``).
+for :class:`gpu.types.Buffer` and :class:`bpy.types.Image.pixels` (aka ``bpy_prop_array``).
"""
import bpy
import gpu
-import bgl
import random
from mathutils import Matrix
from gpu_extras.presets import draw_circle_2d
@@ -25,8 +24,8 @@ RING_AMOUNT = 10
offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT)
with offscreen.bind():
- bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
- bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
+ fb = gpu.state.active_framebuffer_get()
+ fb.clear(color=(0.0, 0.0, 0.0, 0.0))
with gpu.matrix.push_pop():
# reset matrices -> use normalized device coordinates [-1, 1]
gpu.matrix.load_matrix(Matrix.Identity(4))
@@ -37,9 +36,7 @@ with offscreen.bind():
(random.uniform(-1, 1), random.uniform(-1, 1)),
(1, 1, 1, 1), random.uniform(0.1, 1), 20)
- buffer = bgl.Buffer(bgl.GL_BYTE, WIDTH * HEIGHT * 4)
- bgl.glReadBuffer(bgl.GL_BACK)
- bgl.glReadPixels(0, 0, WIDTH, HEIGHT, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)
+ buffer = fb.read_color(0, 0, WIDTH, HEIGHT, 4, 0, 'UBYTE')
offscreen.free()
@@ -48,4 +45,6 @@ if not IMAGE_NAME in bpy.data.images:
bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT)
image = bpy.data.images[IMAGE_NAME]
image.scale(WIDTH, HEIGHT)
+
+buffer.dimensions = WIDTH * HEIGHT * 4
image.pixels = [v / 255 for v in buffer]
diff --git a/doc/python_api/examples/gpu.9.py b/doc/python_api/examples/gpu.9.py
index a4db576ecc0..e358cb517bd 100644
--- a/doc/python_api/examples/gpu.9.py
+++ b/doc/python_api/examples/gpu.9.py
@@ -7,7 +7,6 @@ You could also make this independent of a specific camera,
but Blender does not expose good functions to create view and projection matrices yet.
"""
import bpy
-import bgl
import gpu
from gpu_extras.presets import draw_texture_2d
@@ -34,8 +33,8 @@ def draw():
view_matrix,
projection_matrix)
- bgl.glDisable(bgl.GL_DEPTH_TEST)
- draw_texture_2d(offscreen.color_texture, (10, 10), WIDTH, HEIGHT)
+ gpu.state.depth_mask_set(False)
+ draw_texture_2d(offscreen.texture_color, (10, 10), WIDTH, HEIGHT)
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
diff --git a/doc/python_api/examples/mathutils.Matrix.LocRotScale.py b/doc/python_api/examples/mathutils.Matrix.LocRotScale.py
new file mode 100644
index 00000000000..016a5002e82
--- /dev/null
+++ b/doc/python_api/examples/mathutils.Matrix.LocRotScale.py
@@ -0,0 +1,5 @@
+# Compute local object transformation matrix:
+if obj.rotation_mode == 'QUATERNION':
+ matrix = mathutils.Matrix.LocRotScale(obj.location, obj.rotation_quaternion, obj.scale)
+else:
+ matrix = mathutils.Matrix.LocRotScale(obj.location, obj.rotation_euler, obj.scale)
diff --git a/doc/python_api/examples/mathutils.Matrix.py b/doc/python_api/examples/mathutils.Matrix.py
index 26c7ccba27c..84e09b97c15 100644
--- a/doc/python_api/examples/mathutils.Matrix.py
+++ b/doc/python_api/examples/mathutils.Matrix.py
@@ -14,10 +14,14 @@ mat_rot = mathutils.Matrix.Rotation(math.radians(45.0), 4, 'X')
mat_out = mat_loc @ mat_rot @ mat_sca
print(mat_out)
-# extract components back out of the matrix
+# extract components back out of the matrix as two vectors and a quaternion
loc, rot, sca = mat_out.decompose()
print(loc, rot, sca)
+# recombine extracted components
+mat_out2 = mathutils.Matrix.LocRotScale(loc, rot, sca)
+print(mat_out2)
+
# it can also be useful to access components of a matrix directly
mat = mathutils.Matrix()
mat[0][0], mat[1][0], mat[2][0] = 0.0, 1.0, 2.0