2023-08-16 00:20:26 +10:00
|
|
|
# SPDX-FileCopyrightText: 2011-2023 Blender Authors
|
2023-06-15 13:09:04 +10:00
|
|
|
#
|
2022-02-11 09:07:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2011-08-05 00:51:44 +00:00
|
|
|
|
|
|
|
import bpy
|
|
|
|
|
2021-03-23 16:08:02 +11:00
|
|
|
from bpy.types import (
|
|
|
|
Operator,
|
|
|
|
)
|
2015-01-27 17:46:07 +11:00
|
|
|
from bpy.props import (
|
2017-03-25 11:07:05 +11:00
|
|
|
BoolProperty,
|
|
|
|
EnumProperty,
|
|
|
|
StringProperty,
|
|
|
|
)
|
2011-08-05 00:51:44 +00:00
|
|
|
|
|
|
|
|
2021-03-23 16:08:02 +11:00
|
|
|
class SCENE_OT_freestyle_fill_range_by_selection(Operator):
|
2023-03-06 14:09:44 +01:00
|
|
|
"""Fill the Range Min/Max entries by the min/max distance between selected mesh objects and the source object """ \
|
|
|
|
"""(either a user-specified object or the active camera)"""
|
2011-08-05 00:51:44 +00:00
|
|
|
bl_idname = "scene.freestyle_fill_range_by_selection"
|
|
|
|
bl_label = "Fill Range by Selection"
|
2013-04-26 02:44:21 +00:00
|
|
|
bl_options = {'INTERNAL'}
|
2011-08-05 00:51:44 +00:00
|
|
|
|
2018-07-11 22:18:09 +02:00
|
|
|
type: EnumProperty(
|
2018-06-26 19:41:37 +02:00
|
|
|
name="Type", description="Type of the modifier to work on",
|
2018-09-03 14:15:18 +10:00
|
|
|
items=(
|
|
|
|
('COLOR', "Color", "Color modifier type"),
|
|
|
|
('ALPHA', "Alpha", "Alpha modifier type"),
|
|
|
|
('THICKNESS', "Thickness", "Thickness modifier type"),
|
|
|
|
),
|
2018-06-26 19:41:37 +02:00
|
|
|
)
|
2018-07-11 22:18:09 +02:00
|
|
|
name: StringProperty(
|
2018-06-26 19:41:37 +02:00
|
|
|
name="Name",
|
|
|
|
description="Name of the modifier to work on",
|
|
|
|
)
|
2011-08-05 00:51:44 +00:00
|
|
|
|
2013-04-26 02:29:31 +00:00
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
2018-04-24 15:20:17 +02:00
|
|
|
view_layer = context.view_layer
|
2017-11-22 10:52:39 -02:00
|
|
|
return view_layer and view_layer.freestyle_settings.linesets.active
|
2013-04-26 02:29:31 +00:00
|
|
|
|
2011-08-05 00:51:44 +00:00
|
|
|
def execute(self, context):
|
2015-01-27 17:46:07 +11:00
|
|
|
import sys
|
|
|
|
|
2014-01-18 09:13:51 +11:00
|
|
|
scene = context.scene
|
2018-04-24 15:20:17 +02:00
|
|
|
view_layer = context.view_layer
|
2017-11-22 10:52:39 -02:00
|
|
|
lineset = view_layer.freestyle_settings.linesets.active
|
2011-08-05 00:51:44 +00:00
|
|
|
linestyle = lineset.linestyle
|
|
|
|
# Find the modifier to work on
|
|
|
|
if self.type == 'COLOR':
|
|
|
|
m = linestyle.color_modifiers[self.name]
|
|
|
|
elif self.type == 'ALPHA':
|
|
|
|
m = linestyle.alpha_modifiers[self.name]
|
|
|
|
else:
|
|
|
|
m = linestyle.thickness_modifiers[self.name]
|
2017-01-30 12:18:39 +09:00
|
|
|
# Find the reference object
|
2011-08-24 15:47:05 +00:00
|
|
|
if m.type == 'DISTANCE_FROM_CAMERA':
|
2017-01-30 12:18:39 +09:00
|
|
|
ref = scene.camera
|
2023-03-08 01:46:51 +01:00
|
|
|
if ref is None:
|
|
|
|
self.report({'ERROR'}, "No active camera in the scene")
|
|
|
|
return {'CANCELLED'}
|
2017-01-30 12:18:39 +09:00
|
|
|
matrix_to_camera = ref.matrix_world.inverted()
|
2011-08-24 15:47:05 +00:00
|
|
|
elif m.type == 'DISTANCE_FROM_OBJECT':
|
|
|
|
if m.target is None:
|
|
|
|
self.report({'ERROR'}, "Target object not specified")
|
|
|
|
return {'CANCELLED'}
|
2017-01-30 12:18:39 +09:00
|
|
|
ref = m.target
|
|
|
|
target_location = ref.location
|
2011-08-24 15:47:05 +00:00
|
|
|
else:
|
|
|
|
self.report({'ERROR'}, "Unexpected modifier type: " + m.type)
|
|
|
|
return {'CANCELLED'}
|
2023-09-03 21:35:03 +10:00
|
|
|
# Find selected vertices in edit-mesh.
|
2018-02-07 15:47:54 +11:00
|
|
|
ob = context.active_object
|
2018-04-05 18:20:27 +02:00
|
|
|
if ob.type == 'MESH' and ob.mode == 'EDIT' and ob.name != ref.name:
|
2017-01-31 09:04:05 +09:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
2018-02-07 15:47:54 +11:00
|
|
|
selected_verts = [v for v in ob.data.vertices if v.select]
|
2017-01-31 09:04:05 +09:00
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
# Compute the min/max distance from the reference to mesh vertices
|
|
|
|
min_dist = sys.float_info.max
|
|
|
|
max_dist = -min_dist
|
|
|
|
if m.type == 'DISTANCE_FROM_CAMERA':
|
2019-12-23 23:38:24 -03:00
|
|
|
ob_to_cam = matrix_to_camera @ ob.matrix_world
|
2017-01-31 09:04:05 +09:00
|
|
|
for vert in selected_verts:
|
|
|
|
# dist in the camera space
|
2019-12-23 23:38:24 -03:00
|
|
|
dist = (ob_to_cam @ vert.co).length
|
2017-01-31 09:04:05 +09:00
|
|
|
min_dist = min(dist, min_dist)
|
|
|
|
max_dist = max(dist, max_dist)
|
|
|
|
elif m.type == 'DISTANCE_FROM_OBJECT':
|
|
|
|
for vert in selected_verts:
|
|
|
|
# dist in the world space
|
2019-12-23 23:38:24 -03:00
|
|
|
dist = (ob.matrix_world @ vert.co - target_location).length
|
2017-01-31 09:04:05 +09:00
|
|
|
min_dist = min(dist, min_dist)
|
|
|
|
max_dist = max(dist, max_dist)
|
|
|
|
# Fill the Range Min/Max entries with the computed distances
|
|
|
|
m.range_min = min_dist
|
|
|
|
m.range_max = max_dist
|
|
|
|
return {'FINISHED'}
|
2011-08-05 00:51:44 +00:00
|
|
|
# Find selected mesh objects
|
2019-09-09 10:45:24 +02:00
|
|
|
selection = [ob for ob in scene.objects if ob.select_get() and ob.type == 'MESH' and ob.name != ref.name]
|
2014-01-18 09:13:51 +11:00
|
|
|
if selection:
|
2017-01-30 12:18:39 +09:00
|
|
|
# Compute the min/max distance from the reference to mesh vertices
|
2013-03-02 18:27:50 +00:00
|
|
|
min_dist = sys.float_info.max
|
2011-08-05 00:51:44 +00:00
|
|
|
max_dist = -min_dist
|
2017-01-30 12:18:39 +09:00
|
|
|
if m.type == 'DISTANCE_FROM_CAMERA':
|
|
|
|
for ob in selection:
|
2019-09-09 10:46:25 +02:00
|
|
|
ob_to_cam = matrix_to_camera @ ob.matrix_world
|
2017-01-30 12:18:39 +09:00
|
|
|
for vert in ob.data.vertices:
|
|
|
|
# dist in the camera space
|
2019-09-09 10:46:25 +02:00
|
|
|
dist = (ob_to_cam @ vert.co).length
|
2017-01-30 12:18:39 +09:00
|
|
|
min_dist = min(dist, min_dist)
|
|
|
|
max_dist = max(dist, max_dist)
|
|
|
|
elif m.type == 'DISTANCE_FROM_OBJECT':
|
|
|
|
for ob in selection:
|
|
|
|
for vert in ob.data.vertices:
|
|
|
|
# dist in the world space
|
2019-09-09 10:46:25 +02:00
|
|
|
dist = (ob.matrix_world @ vert.co - target_location).length
|
2017-01-30 12:18:39 +09:00
|
|
|
min_dist = min(dist, min_dist)
|
|
|
|
max_dist = max(dist, max_dist)
|
2011-08-05 00:51:44 +00:00
|
|
|
# Fill the Range Min/Max entries with the computed distances
|
|
|
|
m.range_min = min_dist
|
|
|
|
m.range_max = max_dist
|
|
|
|
return {'FINISHED'}
|
2012-04-22 00:59:27 +00:00
|
|
|
|
|
|
|
|
2021-03-23 16:08:02 +11:00
|
|
|
class SCENE_OT_freestyle_add_edge_marks_to_keying_set(Operator):
|
2023-04-18 10:42:00 +10:00
|
|
|
"""Add the data paths to the Freestyle Edge Mark property of selected edges to the active keying set"""
|
2012-04-22 00:59:27 +00:00
|
|
|
bl_idname = "scene.freestyle_add_edge_marks_to_keying_set"
|
|
|
|
bl_label = "Add Edge Marks to Keying Set"
|
|
|
|
bl_options = {'UNDO'}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
ob = context.active_object
|
|
|
|
return (ob and ob.type == 'MESH')
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
# active keying set
|
|
|
|
scene = context.scene
|
|
|
|
ks = scene.keying_sets.active
|
|
|
|
if ks is None:
|
|
|
|
ks = scene.keying_sets.new(idname="FreestyleEdgeMarkKeyingSet", name="Freestyle Edge Mark Keying Set")
|
|
|
|
ks.bl_description = ""
|
|
|
|
# add data paths to the keying set
|
|
|
|
ob = context.active_object
|
2018-04-05 18:20:27 +02:00
|
|
|
ob_mode = ob.mode
|
2012-04-22 00:59:27 +00:00
|
|
|
mesh = ob.data
|
|
|
|
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
|
|
|
|
for i, edge in enumerate(mesh.edges):
|
|
|
|
if not edge.hide and edge.select:
|
2024-04-27 16:02:36 +10:00
|
|
|
path = "edges[{:d}].use_freestyle_mark".format(i)
|
2012-04-22 00:59:27 +00:00
|
|
|
ks.paths.add(mesh, path, index=0)
|
|
|
|
bpy.ops.object.mode_set(mode=ob_mode, toggle=False)
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
2021-03-23 16:08:02 +11:00
|
|
|
class SCENE_OT_freestyle_add_face_marks_to_keying_set(Operator):
|
2023-04-18 10:42:00 +10:00
|
|
|
"""Add the data paths to the Freestyle Face Mark property of selected polygons to the active keying set"""
|
2012-04-22 00:59:27 +00:00
|
|
|
bl_idname = "scene.freestyle_add_face_marks_to_keying_set"
|
|
|
|
bl_label = "Add Face Marks to Keying Set"
|
|
|
|
bl_options = {'UNDO'}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
ob = context.active_object
|
|
|
|
return (ob and ob.type == 'MESH')
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
# active keying set
|
|
|
|
scene = context.scene
|
|
|
|
ks = scene.keying_sets.active
|
|
|
|
if ks is None:
|
|
|
|
ks = scene.keying_sets.new(idname="FreestyleFaceMarkKeyingSet", name="Freestyle Face Mark Keying Set")
|
|
|
|
ks.bl_description = ""
|
|
|
|
# add data paths to the keying set
|
|
|
|
ob = context.active_object
|
2018-04-05 18:20:27 +02:00
|
|
|
ob_mode = ob.mode
|
2012-04-22 00:59:27 +00:00
|
|
|
mesh = ob.data
|
|
|
|
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
|
|
|
|
for i, polygon in enumerate(mesh.polygons):
|
|
|
|
if not polygon.hide and polygon.select:
|
2024-04-27 16:02:36 +10:00
|
|
|
path = "polygons[{:d}].use_freestyle_mark".format(i)
|
2012-04-22 00:59:27 +00:00
|
|
|
ks.paths.add(mesh, path, index=0)
|
|
|
|
bpy.ops.object.mode_set(mode=ob_mode, toggle=False)
|
|
|
|
return {'FINISHED'}
|
2013-06-01 22:37:15 +00:00
|
|
|
|
|
|
|
|
2021-03-23 16:08:02 +11:00
|
|
|
class SCENE_OT_freestyle_module_open(Operator):
|
2013-06-01 22:37:15 +00:00
|
|
|
"""Open a style module file"""
|
|
|
|
bl_idname = "scene.freestyle_module_open"
|
|
|
|
bl_label = "Open Style Module File"
|
|
|
|
bl_options = {'INTERNAL'}
|
|
|
|
|
2018-07-11 22:18:09 +02:00
|
|
|
filepath: StringProperty(subtype='FILE_PATH')
|
2013-06-01 22:37:15 +00:00
|
|
|
|
2018-07-11 22:18:09 +02:00
|
|
|
make_internal: BoolProperty(
|
2013-06-01 22:37:15 +00:00
|
|
|
name="Make internal",
|
|
|
|
description="Make module file internal after loading",
|
2025-01-14 12:46:40 +11:00
|
|
|
default=True,
|
|
|
|
)
|
2013-06-01 22:37:15 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
2018-04-24 15:20:17 +02:00
|
|
|
view_layer = context.view_layer
|
2017-11-22 10:52:39 -02:00
|
|
|
return view_layer and view_layer.freestyle_settings.mode == 'SCRIPT'
|
2013-06-01 22:37:15 +00:00
|
|
|
|
2019-05-09 09:15:01 +10:00
|
|
|
def invoke(self, context, _event):
|
2013-06-01 22:37:15 +00:00
|
|
|
self.freestyle_module = context.freestyle_module
|
|
|
|
wm = context.window_manager
|
|
|
|
wm.fileselect_add(self)
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
2019-05-09 09:15:01 +10:00
|
|
|
def execute(self, _context):
|
2019-07-01 09:44:08 +02:00
|
|
|
text = bpy.data.texts.load(self.filepath, internal=self.make_internal)
|
2013-06-01 22:37:15 +00:00
|
|
|
self.freestyle_module.script = text
|
|
|
|
return {'FINISHED'}
|
2017-03-20 02:07:24 +11:00
|
|
|
|
|
|
|
|
|
|
|
classes = (
|
|
|
|
SCENE_OT_freestyle_add_edge_marks_to_keying_set,
|
|
|
|
SCENE_OT_freestyle_add_face_marks_to_keying_set,
|
|
|
|
SCENE_OT_freestyle_fill_range_by_selection,
|
|
|
|
SCENE_OT_freestyle_module_open,
|
|
|
|
)
|