-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
117 lines (85 loc) · 3.66 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
bl_info = {
"name" : "Quick AO Nodes - BETA",
"author" : "Matias Garate",
"description" : "Add an AO and Color ramp node all the materials of the active object (it preserves the Base Color of the object).",
"blender" : (2, 80, 0),
"version" : (1, 0, 0),
"location" : "View3D",
"warning" : "",
"category" : "Generic"
}
import bpy
from bpy.types import Operator
from bpy.props import FloatProperty, FloatVectorProperty
from mathutils import Vector
def Add_AO_Nodes(self, context, AO_distance, ramp_left_position, ramp_right_position, ramp_black_color):
active_object = context.active_object
for material_slot in active_object.material_slots:
# Read the Node Tree
try:
node_tree = material_slot.material.node_tree
except:
# If the maetrial slot is empty then mode on.
continue
# Obtain the node connected to the Material Output
# It should be a Principled Shader
main_node = node_tree.nodes['Material Output'].inputs[0].links[0].from_node
base_color = main_node.inputs[0].default_value
# Create the ColorRamp
ramp_node = node_tree.nodes.new('ShaderNodeValToRGB')
ramp_node.color_ramp.elements[0].position = ramp_left_position
ramp_node.color_ramp.elements[1].position = ramp_right_position
ramp_node.color_ramp.elements[0].color = (ramp_black_color[0], ramp_black_color[1],ramp_black_color[2], 1.0)
ramp_node.color_ramp.elements[1].color = base_color
# Create the AO Node
AO_node = node_tree.nodes.new('ShaderNodeAmbientOcclusion')
AO_node.inputs['Distance'].default_value = AO_distance
# Link the Color Ramp Color to the Principled BSDF
# Link the AO to the Color Ramp
node_tree.links.new(ramp_node.outputs[0], main_node.inputs[0])
node_tree.links.new(AO_node.outputs[0], ramp_node.inputs[0])
# Adjust the Color Ramp and AO positions
ramp_node.location = main_node.location + Vector((-400,0))
AO_node.location = ramp_node.location + Vector((-300,0))
class OBJECT_OT_add_quickAO(Operator):
"""Add an AO and color ramp node to all the materials of the active object"""
bl_idname = "object.add_quick_ambient_occlusion_nodes"
bl_label = "Quick AO Nodes"
bl_options = {'REGISTER', 'UNDO'}
AO_distance: FloatProperty(
name="AO distance",
default=0.1,
min = 0.0,
max = 1000.,
description="AO distance"
)
ramp_left_position: FloatProperty(
name="Ramp Left Position",
default=0.0,
min = 0.0,
max = 1.0,
description="Color Ramp left handle position"
)
ramp_right_position: FloatProperty(
name="Ramp Right Position",
default=1.0,
min = 0.0,
max = 1.0,
description="Color Ramp right handle position"
)
ramp_black_color: FloatVectorProperty(
name="AO Black Color",
subtype= 'COLOR',
default=(0.0, 0.0, 0.0),
min=0.0, max=1.0,
description="Replace the default black color of the color ramp"
)
def execute(self, context):
Add_AO_Nodes(self, context, self.AO_distance, self.ramp_left_position, self.ramp_right_position, self.ramp_black_color)
return {'FINISHED'}
def register():
bpy.utils.register_class(OBJECT_OT_add_quickAO)
def unregister():
bpy.utils.unregister_class(OBJECT_OT_add_quickAO)
if __name__ == "__main__":
register()