Created
January 29, 2023 13:01
-
-
Save demohero101/761de1a4c0f14271673ff120f476c963 to your computer and use it in GitHub Desktop.
Add Material Example
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
class AddMaterial(bpy.types.Operator): | |
"""Add Material""" | |
bl_idname = "object.addmat_operator" | |
bl_label = "Add Material" | |
def execute(self, context): | |
ob = bpy.context.active_object | |
bpy.ops.object.shade_smooth() | |
# Add modifier | |
mod_subsurf = ob.modifiers.new("My Modifier", 'SUBSURF') | |
mod_subsurf.levels = 3 | |
bpy.ops.object.modifier_apply(modifier="My Modifier") | |
# Add material | |
new_mat = bpy.data.materials.new("New Material") | |
new_mat.use_nodes = True | |
principled = new_mat.node_tree.nodes['Principled BSDF'] | |
principled.inputs['Metallic'].default_value = 1 | |
principled.inputs['Roughness'].default_value = 0 | |
principled.inputs['Base Color'].default_value = (1,0,0,0) | |
ob.data.materials.append(new_mat) | |
return {'FINISHED'} | |
def register(): | |
bpy.utils.register_class(AddMaterial) | |
def unregister(): | |
bpy.utils.unregister_class(AddMaterial) | |
class MyPanel(bpy.types.Panel): | |
bl_label = "Panel" | |
bl_idname = "MY_PT_Panel" | |
bl_space_type = 'VIEW_3D' | |
bl_region_type = 'UI' | |
bl_category = "Add Material" | |
def draw(self, context): | |
layout = self.layout | |
obj = context.object | |
row = layout.row() | |
row.operator("object.addmat_operator", text = "Add Material") | |
classes = (AddMaterial, MyPanel) | |
def register(): | |
for cls in classes: | |
bpy.utils.register_class(cls) | |
def unregister(): | |
for cls in classes: | |
bpy.utils.unregister_class(cls) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment