Initial commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
bl_info = {
|
||||
"name": "Cpp Test",
|
||||
"author": "akaneyu",
|
||||
"version": (1, 1, 1),
|
||||
"blender": (2, 93, 0),
|
||||
"location": "Image",
|
||||
"warning": "",
|
||||
"description": "",
|
||||
"wiki_url": "",
|
||||
"tracker_url": "",
|
||||
"category": "Paint"}
|
||||
|
||||
if "bpy" in locals():
|
||||
import importlib
|
||||
importlib.reload(cpp_test)
|
||||
importlib.reload(utils)
|
||||
|
||||
import bpy
|
||||
from . import cpp_test
|
||||
|
||||
classes = [
|
||||
cpp_test.CPP_TEST_OT_generate_image,
|
||||
cpp_test.CPP_TEST_PT_panel
|
||||
]
|
||||
|
||||
def register():
|
||||
for cls in classes:
|
||||
bpy.utils.register_class(cls)
|
||||
|
||||
def unregister():
|
||||
for cls in classes:
|
||||
bpy.utils.unregister_class(cls)
|
||||
|
||||
if __name__ == "__main__":
|
||||
register()
|
||||
@@ -0,0 +1,93 @@
|
||||
import sys
|
||||
import os
|
||||
import ctypes
|
||||
import bpy
|
||||
import numpy as np
|
||||
from . import utils
|
||||
|
||||
class Session:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def get_session():
|
||||
global session
|
||||
|
||||
return session
|
||||
|
||||
def load_native_library():
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
if os.name == 'nt':
|
||||
lib_file_name = 'cpp_test.dll'
|
||||
else:
|
||||
lib_file_name = 'libcpp_test.so'
|
||||
|
||||
lib = ctypes.CDLL(os.path.join(script_dir, lib_file_name))
|
||||
|
||||
return lib
|
||||
|
||||
def unload_native_library(lib):
|
||||
if os.name == 'nt':
|
||||
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
kernel32.FreeLibrary.argtypes = [ctypes.c_void_p]
|
||||
kernel32.FreeLibrary(lib._handle)
|
||||
else:
|
||||
stdlib = ctypes.CDLL("")
|
||||
stdlib.dlclose.argtypes = [ctypes.c_void_p]
|
||||
stdlib.dlclose(lib._handle)
|
||||
|
||||
class CPP_TEST_OT_generate_image(bpy.types.Operator):
|
||||
"""Generate a image"""
|
||||
bl_idname = "cpp_test.generate_image"
|
||||
bl_label = "Generate Image"
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
def execute(self, context):
|
||||
global session
|
||||
|
||||
wm = context.window_manager
|
||||
|
||||
native_lib = load_native_library()
|
||||
|
||||
native_lib.CPPTEST_init()
|
||||
|
||||
img = bpy.data.images.new('cpp_test', 1024, 1024, alpha=True)
|
||||
|
||||
context.tool_settings.image_paint.canvas = img
|
||||
context.tool_settings.image_paint.mode = 'IMAGE'
|
||||
|
||||
img_width, img_height = img.size
|
||||
|
||||
pixels = utils.read_pixels_from_image(img)
|
||||
pixels_ctypes = pixels.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
|
||||
|
||||
native_lib.CPPTEST_setImageSize(img_width, img_height)
|
||||
native_lib.CPPTEST_setImagePixels(pixels_ctypes)
|
||||
|
||||
native_lib.CPPTEST_process()
|
||||
|
||||
utils.write_pixels_to_image(img, pixels)
|
||||
|
||||
native_lib.CPPTEST_free()
|
||||
|
||||
unload_native_library(native_lib)
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
class CPP_TEST_PT_panel(bpy.types.Panel):
|
||||
bl_label = "Cpp Test"
|
||||
bl_space_type = "IMAGE_EDITOR"
|
||||
bl_region_type = "UI"
|
||||
bl_category = "Cpp Test"
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
return True
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
|
||||
row = layout.row()
|
||||
op = row.operator(CPP_TEST_OT_generate_image.bl_idname, text='Generate Image')
|
||||
|
||||
session = Session()
|
||||
@@ -0,0 +1,21 @@
|
||||
import bpy
|
||||
import numpy as np
|
||||
|
||||
def read_pixels_from_image(img):
|
||||
width, height = img.size[0], img.size[1]
|
||||
|
||||
if bpy.app.version >= (2, 83, 0):
|
||||
pixels = np.empty(len(img.pixels), dtype=np.float32);
|
||||
img.pixels.foreach_get(pixels)
|
||||
return np.reshape(pixels, (height, width, 4))
|
||||
else:
|
||||
return np.reshape(img.pixels[:], (height, width, 4))
|
||||
|
||||
def write_pixels_to_image(img, pixels):
|
||||
if bpy.app.version >= (2, 83, 0):
|
||||
img.pixels.foreach_set(np.reshape(pixels, -1))
|
||||
else:
|
||||
img.pixels = np.reshape(pixels, -1)
|
||||
|
||||
if img.preview:
|
||||
img.preview.reload()
|
||||
Reference in New Issue
Block a user