Implement scene setup operator.

This commit is contained in:
Nathan Vegdahl 2022-04-05 15:02:43 -07:00
parent afcbeb1ff7
commit c8decb3608

View File

@ -28,6 +28,7 @@ bl_info = {
}
import re
import math
import bpy
@ -38,12 +39,18 @@ from .node_groups import \
MAIN_NODE_NAME = "Compify Footage"
BAKE_IMAGE_NODE_NAME = "Baked Lighting"
UV_LAYER_NAME = 'Compify Baked Lighting'
# Gets the Compify Material name for the active scene.
def compify_mat_name(context):
return "Compify Footage | " + context.scene.name
# Gets the Compify baked lighting image name for the active scene.
def compify_baked_texture_name(context):
return "Compify Bake | " + context.scene.name
#========================================================
@ -77,14 +84,14 @@ class CompifyPanel(bpy.types.Panel):
col.separator_spacer()
col.operator("material.compify_material_new")
col.operator("material.compify_prep_scene")
col.operator("material.compify_bake")
class CompifyCameraPanel(bpy.types.Panel):
"""Configure cameras for 3D compositing."""
bl_label = "Comp Tools"
bl_idname = "DATA_PT_comp_tools_camera"
bl_label = "Compify"
bl_idname = "DATA_PT_compify_camera"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
@ -105,20 +112,36 @@ class CompifyCameraPanel(bpy.types.Panel):
# Ensures that the Compify Footage material exists for this scene.
#
# It will create it if it doesn't exist, and returns the material.
def ensure_compify_material(context):
def ensure_compify_material(context, baking_res=(1024, 1024)):
name = compify_mat_name(context)
if name in bpy.data.materials:
return bpy.data.materials[name]
else:
bake_image_name = compify_baked_texture_name(context)
bake_image = None
if bake_image_name in bpy.data.images:
bake_image = bpy.data.images[bake_image_name]
else:
bake_image = bpy.data.images.new(
bake_image_name,
baking_res[0], baking_res[1],
alpha=False,
float_buffer=True,
stereo3d=False,
is_data=False,
tiled=False,
)
return create_compify_material(
name,
context.scene.compify_footage_camera,
context.scene.compify_footage,
bake_image,
)
# Creates a Compify Footage material.
def create_compify_material(name, camera, footage):
def create_compify_material(name, camera, footage, bake_image=None):
# Create a new completely empty node-based material.
mat = bpy.data.materials.new(name)
mat.use_nodes = True
@ -130,6 +153,7 @@ def create_compify_material(name, camera, footage):
# Create the nodes.
output = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial')
camera_project = mat.node_tree.nodes.new(type='ShaderNodeGroup')
baking_uv_map = mat.node_tree.nodes.new(type='ShaderNodeUVMap')
input_footage = mat.node_tree.nodes.new(type='ShaderNodeTexImage')
feathered_square = mat.node_tree.nodes.new(type='ShaderNodeGroup')
baked_lighting = mat.node_tree.nodes.new(type='ShaderNodeTexImage')
@ -137,12 +161,14 @@ def create_compify_material(name, camera, footage):
# Label and name the nodes.
camera_project.label = "Camera Project"
baking_uv_map.label = "Baking UV Map"
input_footage.label = "Input Footage"
feathered_square.label = "Feathered Square"
baked_lighting.label = BAKE_IMAGE_NODE_NAME
compify_footage.label = MAIN_NODE_NAME
camera_project.name = "Camera Project"
baking_uv_map.label = "Baking UV Map"
input_footage.name = "Input Footage"
feathered_square.name = "Feathered Square"
baked_lighting.name = BAKE_IMAGE_NODE_NAME
@ -153,6 +179,7 @@ def create_compify_material(name, camera, footage):
x = 0.0
camera_project.location = (x, 0.0)
baking_uv_map.location = (x, -200.0)
x += hs
input_footage.location = (x, 400.0)
feathered_square.location = (x, 0.0)
@ -166,6 +193,8 @@ def create_compify_material(name, camera, footage):
camera_project.node_tree = ensure_camera_project_group(camera)
camera_project.inputs['Aspect Ratio'].default_value = footage.size[0] / footage.size[1]
baking_uv_map.uv_map = UV_LAYER_NAME
input_footage.image = footage
input_footage.interpolation = 'Closest'
input_footage.projection = 'FLAT'
@ -177,30 +206,89 @@ def create_compify_material(name, camera, footage):
feathered_square.inputs['Feather'].default_value = 0.05
feathered_square.inputs['Dilate'].default_value = 0.0
baked_lighting.image = None # TODO
baked_lighting.image = bake_image
compify_footage.node_tree = ensure_footage_group()
# Hook up the nodes.
mat.node_tree.links.new(camera_project.outputs['Vector'], input_footage.inputs['Vector'])
mat.node_tree.links.new(camera_project.outputs['Vector'], feathered_square.inputs['Vector'])
mat.node_tree.links.new(baking_uv_map.outputs['UV'], baked_lighting.inputs['Vector'])
mat.node_tree.links.new(input_footage.outputs['Color'], compify_footage.inputs['Footage'])
mat.node_tree.links.new(feathered_square.outputs['Value'], compify_footage.inputs['Footage-Background Mask'])
mat.node_tree.links.new(baked_lighting.outputs['Color'], compify_footage.inputs['Baked Lighting'])
mat.node_tree.links.new(compify_footage.outputs['Shader'], output.inputs['Surface'])
return mat
class CompifyMaterialNew(bpy.types.Operator):
"""Creates a new Compify material"""
bl_idname = "material.compify_material_new"
bl_label = "New Compify material"
# class CompifyMaterialNew(bpy.types.Operator):
# """Creates a new Compify material."""
# bl_idname = "material.compify_material_new"
# bl_label = "New Compify material"
# bl_options = {'UNDO'}
# @classmethod
# def poll(cls, context):
# return True
# def execute(self, context):
# ensure_compify_material(context)
# return {'FINISHED'}
class CompifyPrepScene(bpy.types.Operator):
"""Prepares the scene for compification."""
bl_idname = "material.compify_prep_scene"
bl_label = "Prep Scene"
bl_options = {'UNDO'}
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT' \
and context.scene.compify_footage != None \
and context.scene.compify_footage_camera != None \
and context.scene.compify_proxy_collection != None \
and len(context.scene.compify_proxy_collection.all_objects) > 0
return True
def execute(self, context):
ensure_compify_material(context)
proxy_collection = context.scene.compify_proxy_collection
lights_collection = context.scene.compify_lights_collection
material = ensure_compify_material(context)
# Deselect all objects.
for obj in context.scene.objects:
obj.select_set(False)
# Set up proxy objects.
for obj in proxy_collection.all_objects:
if obj.type == 'MESH':
# Select it.
obj.select_set(True)
context.view_layer.objects.active = obj
# Ensure it has a compify UV layer and that
# it's selected.
if UV_LAYER_NAME not in obj.data.uv_layers:
obj.data.uv_layers.new(name=UV_LAYER_NAME)
obj.data.uv_layers.active = obj.data.uv_layers[UV_LAYER_NAME]
# Set it up with the footage material.
obj.data.materials.clear()
obj.data.materials.append(material)
# UV unwrap the proxy objects.
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.smart_project(
angle_limit=(math.pi/180)*60, # 60 degrees
island_margin=0.005,
area_weight=0.0,
correct_aspect=False,
scale_to_bounds=True,
)
bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'}
@ -350,19 +438,20 @@ class CompifyCameraProjectGroupNew(bpy.types.Operator):
def register():
bpy.utils.register_class(CompifyPanel)
bpy.utils.register_class(CompifyCameraPanel)
bpy.utils.register_class(CompifyMaterialNew)
# bpy.utils.register_class(CompifyMaterialNew)
bpy.utils.register_class(CompifyPrepScene)
bpy.utils.register_class(CompifyBake)
bpy.utils.register_class(CompifyCameraProjectGroupNew)
# Custom properties.
bpy.types.Scene.compify_footage_camera = bpy.props.PointerProperty(
type=bpy.types.Object,
name="Footage Camera",
)
bpy.types.Scene.compify_footage = bpy.props.PointerProperty(
type=bpy.types.Image,
name="Footage Texture",
)
bpy.types.Scene.compify_footage_camera = bpy.props.PointerProperty(
type=bpy.types.Object,
name="Footage Camera",
)
bpy.types.Scene.compify_proxy_collection = bpy.props.PointerProperty(
type=bpy.types.Collection,
name="Footage Proxy Collection",
@ -375,7 +464,8 @@ def register():
def unregister():
bpy.utils.unregister_class(CompifyPanel)
bpy.utils.unregister_class(CompifyCameraPanel)
bpy.utils.unregister_class(CompifyMaterialNew)
# bpy.utils.unregister_class(CompifyMaterialNew)
bpy.utils.unregister_class(CompifyPrepScene)
bpy.utils.unregister_class(CompifyBake)
bpy.utils.unregister_class(CompifyCameraProjectGroupNew)