Created
March 27, 2016 14:41
-
-
Save p2or/a6d9e757123f550255d5 to your computer and use it in GitHub Desktop.
UI - Custom Material #Blender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bl_info = { | |
"name": "", | |
"description": "", | |
"author": "", | |
"version": (0, 0, 1), | |
"blender": (2, 70, 0), | |
"location": "", | |
"warning": "", | |
"wiki_url": "", | |
"tracker_url": "", | |
"category": "" | |
} | |
import bpy | |
from bpy.props import (StringProperty, | |
BoolProperty, | |
IntProperty, | |
FloatProperty, | |
FloatVectorProperty, | |
EnumProperty, | |
PointerProperty, | |
) | |
from bpy.types import (Panel, | |
Operator, | |
AddonPreferences, | |
PropertyGroup, | |
) | |
def find_node(material, nodetype): | |
if material and material.node_tree: | |
ntree = material.node_tree | |
active_output_node = None | |
for node in ntree.nodes: | |
if getattr(node, "type", None) == nodetype: | |
if getattr(node, "is_active_output", True): | |
return node | |
if not active_output_node: | |
active_output_node = node | |
return active_output_node | |
return None | |
def find_node_input(node, name): | |
for input in node.inputs: | |
if input.name == name: | |
return input | |
return None | |
def panel_node_draw(layout, id_data, output_type, input_name): | |
if not id_data.use_nodes: | |
layout.operator("cycles.use_shading_nodes", icon='NODETREE') | |
return False | |
ntree = id_data.node_tree | |
node = find_node(id_data, output_type) | |
if not node: | |
layout.label(text="No output node") | |
else: | |
input = find_node_input(node, input_name) | |
layout.template_node_view(ntree, node, input) | |
return True | |
class UV_OT_my_panel(Panel): | |
bl_space_type = 'VIEW_3D' | |
bl_idname = "UV_OT_my_panel" | |
bl_label = "Custom" | |
bl_category = "Custom" | |
bl_region_type = 'TOOLS' | |
def draw(self, context): | |
layout = self.layout | |
mat = bpy.context.object.material_slots[0].material | |
if not panel_node_draw(layout, mat, 'OUTPUT_MATERIAL', 'Surface'): | |
layout.prop(mat, "diffuse_color") | |
def register(): | |
bpy.utils.register_module(__name__) | |
def unregister(): | |
bpy.utils.unregister_module(__name__) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment