2

I'm working on my addon for Blender, which is a simple offline renderer. I'm stuck at the material system for two days. I would be much appreciated if anyone gives me a pointer.

The issue is that in order to implement my own material system, I have to store my material information in the context.material class, however I've no idea how to add a customized property for this class. I noticed that there are customized property for other external renderers, such as this one: http://www.mitsuba-renderer.org/ There is a property named "context.material.mitsuba_material". What I want is something like "context.material.my_material" so that I can store my material information there.

I also found this class in the render addon script:

@MitsubaAddon.addon_register_class
class mitsuba_material(declarative_property_group):
    '''
    Storage class for Mitsuba Material settings.
    '''

    ef_attach_to = ['Material']

I believe it has something to do with this customized property, however I got no luck by implementing myself.

Forgive me if it is a silly question and I did googled it, got nothing useful yet.

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125

1 Answers1

5

I believe all you need are bpy.props properties. The first example even demonstrates it for bpy.types.Material. Here's an extended example, that uses some additional options for a FloatProperty and adds it above the Material list in Material tab, so you can try it out:

import bpy

def draw_func(self, context):
    layout = self.layout
    ob = context.object
    layout.prop(ob.active_material, "my_material")

def register():
    bpy.types.Material.my_material = bpy.props.FloatProperty(
        name="My Material Property",
        min=0.0,
        max=10.0,
        default=5.0,
        subtype='FACTOR'
    )
    bpy.types.MATERIAL_PT_context_material.prepend(draw_func)

def unregister():
    del bpy.types.Material.my_material
    bpy.types.MATERIAL_PT_context_material.remove(draw_func)


if __name__ == "__main__":
    register()

You can add your custom properties to a full, custom panel too of course.

If you need to add plenty of properties, use a property group and a PointerProperty to organize them better (and avoid name conflicts with existing properties).

If you need a varying number of a group of properties, use a PropertyGroup to define the properties per occurrence and supply it to a CollectionProperty as type.

CodeManX
  • 29,298
  • 3
  • 89
  • 128