94 lines
2.2 KiB
Python
94 lines
2.2 KiB
Python
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()
|