Make changing the footage and camera properties update the material.

This commit is contained in:
Nathan Vegdahl 2022-04-08 15:51:38 -07:00
parent 898b5203ae
commit 32c746ac84
2 changed files with 76 additions and 4 deletions

View File

@ -122,15 +122,28 @@ class CompifyCameraPanel(bpy.types.Panel):
col = layout.column()
col.operator("material.compify_camera_project_new")
#========================================================
# Fetches the current scene's compify material if it exists.
#
# Returns the material if it exists and None if it doesn't.
def get_compify_material(context):
name = compify_mat_name(context)
if name in bpy.data.materials:
return bpy.data.materials[name]
else:
return None
# 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, baking_res=(1024, 1024)):
name = compify_mat_name(context)
if name in bpy.data.materials:
return bpy.data.materials[name]
mat = get_compify_material(context)
if mat != None:
return mat
else:
bake_image_name = compify_baked_texture_name(context)
bake_image = None
@ -148,7 +161,7 @@ def ensure_compify_material(context, baking_res=(1024, 1024)):
)
return create_compify_material(
name,
compify_mat_name(context),
context.scene.compify_footage_camera,
context.scene.compify_footage,
bake_image,
@ -236,6 +249,25 @@ def create_compify_material(name, camera, footage, bake_image=None):
return mat
def change_footage_material_clip(scene, context):
if scene.compify_footage == None:
return
mat = get_compify_material(context)
if mat != None:
footage_node = mat.node_tree.nodes["Input Footage"]
footage_node.image = scene.compify_footage
footage_node.image_user.frame_duration = scene.compify_footage.frame_duration
def change_footage_camera(scene, context):
if scene.compify_footage_camera == None or scene.compify_footage_camera.type != 'CAMERA':
return
mat = get_compify_material(context)
if mat != None:
group = ensure_camera_project_group(scene.compify_footage_camera)
mat.node_tree.nodes["Camera Project"].node_tree = group
class CompifyPrepScene(bpy.types.Operator):
"""Prepares the scene for compification."""
bl_idname = "material.compify_prep_scene"
@ -486,10 +518,13 @@ def register():
bpy.types.Scene.compify_footage = bpy.props.PointerProperty(
type=bpy.types.Image,
name="Footage Texture",
update=change_footage_material_clip,
)
bpy.types.Scene.compify_footage_camera = bpy.props.PointerProperty(
type=bpy.types.Object,
name="Footage Camera",
poll=lambda scene, obj : obj.type == 'CAMERA',
update=change_footage_camera,
)
bpy.types.Scene.compify_footage_geo_collection = bpy.props.PointerProperty(
type=bpy.types.Collection,

37
uv_island_utils.py Normal file
View File

@ -0,0 +1,37 @@
from math import inf
from bpy_extras.mesh_utils import mesh_linked_uv_islands
def uv_island_bbox_area_sum_multi_object(mesh_objects, uv_layer_name):
area_sum = 0.0
for obj in mesh_objects:
area_sum += uv_island_bbox_area_sum(obj.data, uv_layer_name)
return area_sum
def uv_island_bbox_area_sum(mesh, uv_layer_name):
""" Returns the sum of the area of the bounding boxes of all the UV
islands in the given mesh for the given uv layer.
"""
uvs = mesh.uv_layers[uv_layer_name].data
islands = mesh_linked_uv_islands(mesh)
bbox_area_sum = 0.0
for island in islands:
# Calculate the island's bounding box.
min_u = inf
max_u = -inf
min_v = inf
max_v = -inf
for face_idx in island:
for loop_idx in mesh.polygons[face_idx].loop_indices:
uv = uvs[mesh.loops[loop_idx].vertex_index].uv
min_u = min(min_u, uv[0])
max_u = max(max_u, uv[0])
min_v = min(min_v, uv[1])
max_v = max(max_v, uv[1])
# Add it's area to the sum.
bbox_area_sum += ((max_u - min_u) * (max_v - min_v))**0.5
return bbox_area_sum