This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/release/scripts/startup/bl_operators/freestyle.py

231 lines
9.0 KiB
Python
Raw Normal View History

# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
import bpy
2015-01-27 17:46:07 +11:00
from bpy.props import (
BoolProperty,
EnumProperty,
StringProperty,
)
class SCENE_OT_freestyle_fill_range_by_selection(bpy.types.Operator):
2013-04-07 10:21:22 +00: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)"""
bl_idname = "scene.freestyle_fill_range_by_selection"
bl_label = "Fill Range by Selection"
bl_options = {'INTERNAL'}
2015-01-27 17:46:07 +11:00
type = EnumProperty(
name="Type", description="Type of the modifier to work on",
items=(("COLOR", "Color", "Color modifier type"),
("ALPHA", "Alpha", "Alpha modifier type"),
("THICKNESS", "Thickness", "Thickness modifier type")),
)
name = StringProperty(
name="Name",
description="Name of the modifier to work on",
)
@classmethod
def poll(cls, context):
view_layer = context.scene.view_layers.active
return view_layer and view_layer.freestyle_settings.linesets.active
def execute(self, context):
2015-01-27 17:46:07 +11:00
import sys
scene = context.scene
view_layer = scene.view_layers.active
lineset = view_layer.freestyle_settings.linesets.active
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]
# Find the reference object
if m.type == 'DISTANCE_FROM_CAMERA':
ref = scene.camera
matrix_to_camera = ref.matrix_world.inverted()
elif m.type == 'DISTANCE_FROM_OBJECT':
if m.target is None:
self.report({'ERROR'}, "Target object not specified")
return {'CANCELLED'}
ref = m.target
target_location = ref.location
else:
self.report({'ERROR'}, "Unexpected modifier type: " + m.type)
return {'CANCELLED'}
# Find selected vertices in editmesh
ob = context.active_object
if ob.type == 'MESH' and ob.mode == 'EDIT' and ob.name != ref.name:
bpy.ops.object.mode_set(mode='OBJECT')
selected_verts = [v for v in ob.data.vertices if v.select]
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':
ob_to_cam = matrix_to_camera * ob.matrix_world
for vert in selected_verts:
# dist in the camera space
dist = (ob_to_cam * vert.co).length
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
dist = (ob.matrix_world * vert.co - target_location).length
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'}
# Find selected mesh objects
Render Layers and Collections (merge from render-layers) Design Documents ---------------- * https://wiki.blender.org/index.php/Dev:2.8/Source/Layers * https://wiki.blender.org/index.php/Dev:2.8/Source/DataDesignRevised User Commit Log --------------- * New Layer and Collection system to replace render layers and viewport layers. * A layer is a set of collections of objects (and their drawing options) required for specific tasks. * A collection is a set of objects, equivalent of the old layers in Blender. A collection can be shared across multiple layers. * All Scenes have a master collection that all other collections are children of. * New collection "context" tab (in Properties Editor) * New temporary viewport "collections" panel to control per-collection visibility Missing User Features --------------------- * Collection "Filter" Option to add objects based on their names * Collection Manager operators The existing buttons are placeholders * Collection Manager drawing The editor main region is empty * Collection Override * Per-Collection engine settings This will come as a separate commit, as part of the clay-engine branch Dev Commit Log -------------- * New DNA file (DNA_layer_types.h) with the new structs We are replacing Base by a new extended Base while keeping it backward compatible with some legacy settings (i.e., lay, flag_legacy). Renamed all Base to BaseLegacy to make it clear the areas of code that still need to be converted Note: manual changes were required on - deg_builder_nodes.h, rna_object.c, KX_Light.cpp * Unittesting for main syncronization requirements - read, write, add/copy/remove objects, copy scene, collection link/unlinking, context) * New Editor: Collection Manager Based on patch by Julian Eisel This is extracted from the layer-manager branch. With the following changes: - Renamed references of layer manager to collections manager - I doesn't include the editors/space_collections/ draw and util files - The drawing code itself will be implemented separately by Julian * Base / Object: A little note about them. Original Blender code would try to keep them in sync through the code, juggling flags back and forth. This will now be handled by Depsgraph, keeping Object and Bases more separated throughout the non-rendering code. Scene.base is being cleared in doversion, and the old viewport drawing code was poorly converted to use the new bases while the new viewport code doesn't get merged and replace the old one. Python API Changes ------------------ ``` - scene.layers + # no longer exists - scene.objects + scene.scene_layers.active.objects - scene.objects.active + scene.render_layers.active.objects.active - bpy.context.scene.objects.link() + bpy.context.scene_collection.objects.link() - bpy_extras.object_utils.object_data_add(context, obdata, operator=None, use_active_layer=True, name=None) + bpy_extras.object_utils.object_data_add(context, obdata, operator=None, name=None) - bpy.context.object.select + bpy.context.object.select = True + bpy.context.object.select = False + bpy.context.object.select_get() + bpy.context.object.select_set(action='SELECT') + bpy.context.object.select_set(action='DESELECT') -AddObjectHelper.layers + # no longer exists ```
2017-02-07 10:18:38 +01:00
selection = [ob for ob in scene.objects if ob.select_get() and ob.type == 'MESH' and ob.name != source.name]
if selection:
# 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':
for ob in selection:
ob_to_cam = matrix_to_camera * ob.matrix_world
for vert in ob.data.vertices:
# dist in the camera space
dist = (ob_to_cam * vert.co).length
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
dist = (ob.matrix_world * vert.co - target_location).length
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'}
class SCENE_OT_freestyle_add_edge_marks_to_keying_set(bpy.types.Operator):
'''Add the data paths to the Freestyle Edge Mark property of selected edges to the active keying set'''
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
ob_mode = ob.mode
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:
path = 'edges[%d].use_freestyle_mark' % i
ks.paths.add(mesh, path, index=0)
bpy.ops.object.mode_set(mode=ob_mode, toggle=False)
return {'FINISHED'}
class SCENE_OT_freestyle_add_face_marks_to_keying_set(bpy.types.Operator):
'''Add the data paths to the Freestyle Face Mark property of selected polygons to the active keying set'''
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
ob_mode = ob.mode
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:
path = 'polygons[%d].use_freestyle_mark' % i
ks.paths.add(mesh, path, index=0)
bpy.ops.object.mode_set(mode=ob_mode, toggle=False)
return {'FINISHED'}
class SCENE_OT_freestyle_module_open(bpy.types.Operator):
"""Open a style module file"""
bl_idname = "scene.freestyle_module_open"
bl_label = "Open Style Module File"
bl_options = {'INTERNAL'}
filepath = StringProperty(subtype='FILE_PATH')
make_internal = BoolProperty(
name="Make internal",
description="Make module file internal after loading",
default=True)
@classmethod
def poll(cls, context):
view_layer = context.scene.view_layers.active
return view_layer and view_layer.freestyle_settings.mode == 'SCRIPT'
def invoke(self, context, event):
self.freestyle_module = context.freestyle_module
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
text = bpy.data.texts.load(self.filepath, self.make_internal)
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,
)