2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I'm totally new to python, just got my hands on it a week ago. I worked on this addon in my spare time so the progress is relatively slow. $\endgroup$ – JerryCao1985 Aug 28 '15 at 12:56
  • $\begingroup$ If it's a simple render engine, this answer could be a solid starting point. $\endgroup$ – p2or Aug 28 '15 at 13:03
4
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ That is exactly the right thing that I should do, thanks. $\endgroup$ – JerryCao1985 Aug 29 '15 at 8:31

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.