Initial commit

This commit is contained in:
akaneyu 2024-03-05 21:02:32 +09:00
parent 66252e4234
commit 431a613ae2
7 changed files with 275 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/build*/
__pycache__/

11
CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.11)
set(LIB_NAME cpp_test)
project(${LIB_NAME})
add_library(${LIB_NAME} SHARED
src/cpp_test_processor.cpp
src/cpp_test_processor.h
)

35
addon/__init__.py Normal file
View File

@ -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()

93
addon/cpp_test.py Normal file
View File

@ -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()

21
addon/utils.py Normal file
View File

@ -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()

View File

@ -0,0 +1,78 @@
#include "cpp_test_processor.h"
#include <cmath>
#include <iostream>
using namespace std;
Processor *g_processor;
int CPPTEST_init()
{
g_processor = new Processor();
return 0;
}
void CPPTEST_free()
{
delete g_processor;
}
void CPPTEST_setImageSize(int width, int height)
{
g_processor->setImageSize(width, height);
}
void CPPTEST_setImagePixels(float *pixels)
{
g_processor->setImagePixels(pixels);
}
void CPPTEST_process()
{
g_processor->process();
}
Processor::Processor() :
m_imageWidth(0),
m_imageHeight(0),
m_imagePixels(NULL)
{
}
Processor::~Processor()
{
}
void Processor::setImageSize(int width, int height)
{
m_imageWidth = width;
m_imageHeight = height;
}
void Processor::process()
{
int x, y;
for (y = 0; y < m_imageHeight; y++) {
for (x = 0; x < m_imageWidth; x++) {
long offset = (m_imageWidth * y + x) * 4;
float *addr = m_imagePixels + offset;
float r = (float) cos(x * 0.01);
float g = (float) sin(y * 0.01);
float b = (float) cos(x * y * 0.01);
r = (r + 1.0f) / 2.0f;
g = (g + 1.0f) / 2.0f;
b = (b + 1.0f) / 2.0f;
*addr = r;
*(addr + 1) = g;
*(addr + 2) = b;
*(addr + 3) = 1.0f;
}
}
}

35
src/cpp_test_processor.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef CPP_TEST_PROCESSOR_H_
#define CPP_TEST_PROCESSOR_H_
#if defined(_MSC_VER)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
extern "C" {
EXPORT int CPPTEST_init();
EXPORT void CPPTEST_free();
EXPORT void CPPTEST_setImageSize(int width, int height);
EXPORT void CPPTEST_setImagePixels(float *pixels);
EXPORT void CPPTEST_process();
}
class Processor
{
public:
Processor();
virtual ~Processor();
void setImageSize(int width, int height);
void setImagePixels(float *pixels) { m_imagePixels = pixels; }
void process();
private:
int m_imageWidth;
int m_imageHeight;
float *m_imagePixels;
};
#endif // CPP_TEST_PROCESSOR_H_