Skip to content

Commit

Permalink
Better handling of libraries installation, first object rotation version
Browse files Browse the repository at this point in the history
  • Loading branch information
FConstans committed May 12, 2022
1 parent 61c05b8 commit d452b20
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
52 changes: 22 additions & 30 deletions repositioning/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@


import bpy
import subprocess
import os
import sys
from pathlib import Path
import importlib


# Pillow, that we need to convert images into arrays, will be installed into Blender's folder dedicated to modules if it is not already accessible
# This installation requires to run Blender as administrator the first time the module is imported and activated

if not importlib.util.find_spec("PIL"):
py_exec = str(sys.executable)
dir = os.path.dirname(Path(importlib.util.find_spec("bpy").origin).parent)

subprocess.check_call(
[py_exec, '-m', 'pip', 'install', 'Pillow', '--target', dir])

else:
print("Pillow already installed : ", importlib.util.find_spec("PIL").origin)

from . import add_plane, main_panel, manage_textures, evaluate_shadow, rotate_target

# ------------------------------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------------------------

bl_info = {
"name": "Set a shadow baking environment",
"author": "Florence Constans",
"version": (0, 0, 3),
"version": (0, 0, 5),
"blender": (2, 80, 0),
"location": "View3D > Side Bar",
"description": "Adds a plane to bake on, or add material and textures on already existing objects to bake shadows on ",
Expand All @@ -19,38 +39,10 @@
# ------------------------------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------------------------

import subprocess
import sys
import os
import importlib
from pathlib import Path


# We will be using RMSE coming from the Sewar package, to evaluate the obtained renders
# Because the version of python that bpy use is "isolated" in Blender, we have to make sure the package is installed inside of Blender:


if not importlib.util.find_spec("sewar"):

py_exec = str(sys.executable)

# # Get lib directory
lib = os.path.join(Path(py_exec).parent.parent, "lib")
# # Ensure pip is installed
# #subprocess.check_call([py_exec, "-m", "ensurepip", "--user"])
# # Update pip (not mandatory)
# #subprocess.check_call([py_exec, "-m", "pip", "install", "--upgrade", "pip"])
# # Install packages
subprocess.check_call([py_exec, '-m', 'pip', 'install',f"--target={str(lib)}", 'sewar'])
else :
print("Sewar already installed")

# ------------------------------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------------------------

from . import add_plane, main_panel, manage_textures, evaluate_shadow, rotate_target



def register():

Expand Down
12 changes: 7 additions & 5 deletions repositioning/evaluate_shadow.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

import bpy


from sewar.full_ref import rmse
from numpy import asarray
import numpy as np
from PIL import Image

from . manage_textures import bake_shadow, save_image


def rmse(prediction, target):
return np.sqrt(((prediction.astype(np.float64)-target.astype(np.float64))**2).mean())


def compare_textures():
object = bpy.context.active_object
object_material = bpy.data.materials[object.name_full]
Expand All @@ -21,9 +23,9 @@ def compare_textures():
save_image(object_material.node_tree.nodes[ref_name])
save_image(bake_shadow())

ref_image = asarray(Image.open(
ref_image = np.asarray(Image.open(
bpy.app.tempdir + ref_name+".png"))
shad_image = asarray(Image.open(
shad_image = np.asarray(Image.open(
bpy.app.tempdir + shad_name+".png"))

result = rmse(ref_image, shad_image)
Expand Down
40 changes: 34 additions & 6 deletions repositioning/rotate_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import bpy

from math import radians
from sewar.full_ref import rmse
from numpy import asarray
import numpy as np
from PIL import Image

from . manage_textures import bake_shadow, save_image
from . evaluate_shadow import compare_textures
from . evaluate_shadow import compare_textures, rmse

global target
target = None
Expand All @@ -19,10 +18,39 @@
def select_target_object():
return (bpy.context.active_object)

# This version uses compare_textures(), which means everytime we evaluate, the reference is saved and opened again, even though it doesn't change.

def rotate_object(target):
target.rotation_euler[2] += radians(10)
return("Rotation of target")

def rotate_object(target, degree=360):
if abs(degree) > 1:
initial_diff = compare_textures()
degree_1 = degree/2
degree_2 = -degree_1
target.rotation_euler[2] += radians(degree_1)
diff_1 = compare_textures()

target.rotation_euler[2] -= radians(degree_1)

target.rotation_euler[2] += radians(degree_2)
diff_2 = compare_textures()

target.rotation_euler[2] -= radians(degree_2)

if initial_diff < diff_1 and initial_diff < diff_2:

if diff_1 < diff_2:
return(rotate_object(target, degree_1))
else:
return(rotate_object(target, degree_2))

elif diff_1 <= diff_2:
target.rotation_euler[2] += radians(degree_1)
return(rotate_object(target, degree_1))
else:
target.rotation_euler[2] += radians(degree_2)
return(rotate_object(target, degree_2))
else:
return(str(compare_textures()))


class OBJECT_OT_rotate_target(bpy.types.Operator):
Expand Down

0 comments on commit d452b20

Please sign in to comment.