1
$\begingroup$

I have a basic panel script with 3 buttons made with an enum. Those buttons are meant to change two materials of an object e.g. the 'silver' button would change the materials to the silver materials etc. *Update* I put one of those lines into the console directly and it worked just fine, so I guess it's a matter of where I put those codes into, does anyone know where it should be?

This is what I tried:

D = bpy.data

if colors == 'Gold':
    D.objects['Macbook'].material_slots[0].material = D.material['Gold']
    D.objects['Macbook'].material_slots[9].material = D.material['Gold Apple Logo']
elif colors == 'Silver':
    D.objects['Macbook'].material_slots[0].material = D.material['Silver']
    D.objects['Macbook'].material_slots[9].material = D.material['Silver']
elif colors == 'Space Grey':
    D.objects['Macbook'].material_slots[0].material = D.material['Space Grey']
    D.objects['Macbook'].material_slots[9].material = D.material['Space Grey Apple Logo']

There were no errors, but it just doesn't do anything...

Here is my full code:

import bpy
from bpy.types import (Menu, PropertyGroup)
from bpy.props import (BoolProperty, FloatProperty, EnumProperty)

def update_after_enum(self, context):
    D = bpy.data

    if colors == 'Gold':
    D.objects['Macbook'].material_slots[0].material = D.materials['Gold']
    D.objects['Macbook'].material_slots[9].material = D.materials['Gold Apple Logo']
elif colors == 'Silver':
    D.objects['Macbook'].material_slots[0].material = D.materials['Silver']
    D.objects['Macbook'].material_slots[9].material = D.materials['Silver']
elif colors == 'Space Grey':
    D.objects['Macbook'].material_slots[0].material = D.materials['Space Grey']
    D.objects['Macbook'].material_slots[9].material = D.materials['Space Grey Apple Logo']


def common_update(self, context, origin):
    obj = bpy.data.objects.get("Macbook")
    if not obj:
        return

    if origin == 'isight_camera_indicator':
        obj["00_iSight Camera Indicator"] = getattr(self, origin)

        node_tree = bpy.data.materials['iSight Camera Indicator'].node_tree
        node = node_tree.nodes["Emission"]

        if getattr(self, origin):
            emission_value = 5.0
        else:
            emission_value = 0.0

        nodes["Emission"].inputs[1].default_value = emission_value

    elif origin == 'dirt_and_dust':
        obj["01_Dirt and Dust"] = getattr(self, origin)
    elif origin == 'menubar_and_dock':
        obj["02_Menubar and Dock"] = getattr(self, origin)
    elif origin == 'screen_rotation':
        obj["03_Screen Rotation"] = getattr(self, origin)


class IgnitProperties(bpy.types.PropertyGroup):

    isight_camera_indicator = BoolProperty(
        name = "iSight Camera Indicator",
        description = "Toggles the iSight Camera Indicator ON and OFF",
        default = False,
        update = lambda self, context: common_update(self, context, 'isight_camera_indicator')
    )

    dirt_and_dust = BoolProperty(
        name = "Dirt and Dust",
        description = "Toggles the Dirt and Dust ON and OFF",
        default = False,
        update = lambda self, context: common_update(self, context, 'dirt_and_dust')
    )

    menubar_and_dock = BoolProperty(
        name = "Menubar and Dock",
        description = "Toggles the Menubar and Dock ON and OFF",
        default = False,
        update = lambda self, context: common_update(self, context, 'menubar_and_dock')
    )

    screen_rotation = FloatProperty(
        name = "Screen Rotation",
        description = "Rotates the screen",
        default = 0.0,
        min = 0.0,
        max = 1.0,
        update = lambda self, context: common_update(self, context, 'screen_rotation')
    )

    #MacBook Colors
    colors = EnumProperty(
        name = "MacBook Colors",
        description = "Changes the color of the MacBook",
        items = [
            ("Silver", "Silver", "Silver"),
            ("Gold", "Gold", "Gold"),
            ("Space Grey", "Space Grey", "Space Grey")
        ],
        update = update_after_enum
    )

class IGLayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "MacBook Controller"
    bl_idname = "object_mode.macbook_controller"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout

        layout.label(text = "Main Features:")

        scene = context.scene
        split = layout.split()
        scene = context.scene
        camera = scene.camera.data

        # First column
        col = split.column()
        col.prop(scene.ignit_panel, "isight_camera_indicator")
        col.prop(scene.ignit_panel, "dirt_and_dust")

        # Second Column
        col = split.column()
        col.prop(scene.ignit_panel, "menubar_and_dock")
        col.prop(scene.ignit_panel, "screen_rotation")

        # MacBook Colors
        layout.label(text = "MacBook Color:")

        row = layout.row(align=True)

        row.prop(scene.ignit_panel, "colors", expand = True)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.ignit_panel = bpy.props.PointerProperty(type=IgnitProperties)


def unregister():
    bpy.utils.unregister__module(__name__)
    del bpy.types.Scene.ignit_panel


if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ The indenting was just from putting the code into here, its fine in the actual script in blender, and as for the materials, I tested it in the console directly and it worked fine... $\endgroup$ – mr-matt Dec 28 '15 at 9:20
  • $\begingroup$ Ok, I put the self.colors in and it worked fine! Gee I really wish coding for blender were like coding in unity! $\endgroup$ – mr-matt Dec 28 '15 at 9:24
  • $\begingroup$ Oh no I was referring to C#, in fact more so to the IDE, monodevelop, it works so much better than a text editor! $\endgroup$ – mr-matt Dec 28 '15 at 10:00
  • 1
    $\begingroup$ Yeah, Blender would benefit from a built in IDE, a mix between a REPL (console) and a TextEditor. No argument there :) I tend to use SublimeText3 with snippets of templates / and a custom set of templates $\endgroup$ – zeffii Dec 28 '15 at 10:12
  • $\begingroup$ Hmm, that's a pretty good resource! I do you know if Monodevelop can code with python, does it know the python syntaxing? $\endgroup$ – mr-matt Dec 28 '15 at 10:19
2
$\begingroup$
  1. Your first snippet references bpy.data.material, but it should be bpy.data.materials. It is plural; think of it as a collection of all materials in the .blend.
  2. Secondly the colors variable isn't global, but part of the PropertyGroup and must be referenced using self.colors.

your update function most likely would be written less verbose this way:

def update_after_enum(self, context):

    objects = bpy.data.objects
    materials = bpy.data.materials

    macbook = objects.get('Macbook')
    if not macbook:
        return

    slots = macbook.material_slots
    colors = self.colors

    if colors == 'Gold':
        slots[0].material = materials['Gold']
        slots[9].material = materials['Gold Apple Logo']
    elif colors == 'Silver':
        slots[0].material = materials['Silver']
        slots[9].material = materials['Silver']
    elif colors == 'Space Grey':
        slots[0].material = materials['Space Grey']
        slots[9].material = materials['Space Grey Apple Logo']

Then a bit of nitpicking. The last section I would go even further and write it without the if-elif-elif and use the colors property directly.

Assuming you have the following materials in the .blend

- 'Gold'
- 'Gold Apple Logo'
- 'Silver'
- 'Silver Apple Logo'
- 'Space Grey'
- 'Space Grey Apple Logo'

code:

    if self.colors in {'Gold', 'Silver', 'Space Grey'}:
        slots[0].material = materials[self.colors]
        slots[9].material = materials[self.colors + ' Apple Logo']
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.