-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomize_color.py
88 lines (73 loc) · 3.39 KB
/
randomize_color.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
'''
Copyright (C) 2020-2023 Orange Turbine
https://orangeturbine.com
This file is part of Scattershot, created by Jonathan Lampel.
All code distributed with this add-on is open source as described below.
Scattershot 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 3
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, see <https://www.gnu.org/licenses/>.
'''
import bpy
from bpy.types import (Operator)
from .utilities.utilities import append_node, mode_toggle, is_shader
from .defaults import node_tree_names
def connect_vector(links, nodes, from_node, to_node):
has_coordinates = False
for input in from_node.inputs:
if (input.name == 'Vector' or input.name == 'UV Map') and input.links:
links.new(input.links[0].from_socket, to_node.inputs['Vector'])
has_coordinates == True
break
def create_randomize_node(self, context):
nodes = context.selected_nodes[0].id_data.nodes
links = context.selected_nodes[0].id_data.links
for node in context.selected_nodes:
randomize_node = append_node(self, nodes, node_tree_names['randomize_noise_hsv'])
randomize_node.label = 'HSV Noise'
connect_vector(links, nodes, node, randomize_node)
randomize_node.width = 200
if is_shader(node):
randomize_node.location = [node.location[0] - randomize_node.width - 50, node.location[1]]
if hasattr(node.inputs, 'Base Color'):
to_socket = node.inputs['Base Color']
elif hasattr(node.inputs, 'Color'):
to_socket = node.inputs['Color']
else:
to_socket = node.inputs[0]
if to_socket.links:
links.new(to_socket.links[0].from_socket, randomize_node.inputs[0])
links.new(randomize_node.outputs[0], to_socket)
else:
randomize_node.location = [node.location[0] + node.width + 50, node.location[1]]
if node.outputs[0].links:
for link in node.outputs[0].links:
links.new(randomize_node.outputs[0], node.outputs[0].links[0].to_socket)
links.new(node.outputs[0], randomize_node.inputs[0])
class NODE_OT_randomize_col(Operator):
bl_label = "Add HSV Noise"
bl_idname = "node.randomizecol"
bl_description = "Randomizes the output of a node using a noise texture"
bl_space_type = "NODE_EDITOR"
bl_region_type = "UI"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.area.ui_type == 'ShaderNodeTree' and context.selected_nodes
def execute(self, context):
# switching modes prevents context errors
prev_mode = mode_toggle(context, 'OBJECT')
create_randomize_node(self, context)
mode_toggle(context, prev_mode)
return {'FINISHED'}
def register():
bpy.utils.register_class(NODE_OT_randomize_col)
def unregister():
bpy.utils.unregister_class(NODE_OT_randomize_col)