1
$\begingroup$

I am curently creating a simple texture baker addon. I am not a coder, just picking thing from many sources and try to make it work together. My AO baker works fine but I am not able to choose image size nor destination folder from the UI. any help please?

bl_info = {
    "name": "AO_BAKER",
    "description": "Tools",
    "author": "Loranozor",
    "version": (0.1, 0),
    "blender": (2, 79, 0),
    "location": "View3D, panel",
    "category": "3D View"}

import bpy
from bpy.types import Panel

class AOBAKER(Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_label = 'AO_BAKER'
    bl_context ='objectmode'
    bl_category ='AO_BAKER'

    def draw(self, context):
        layout = self.layout
        scn = context.scene
        row = layout.row(align=True)

        row = layout.row(align=True)
        row.operator("siz1.button").number=1
        row.operator("siz2.button").number=2
        row.operator("siz3.button").number=3
        col = layout.column()
        col.prop(context.scene, 'exp_path') 
        layout.label("!!Bake that shit now!!", icon='SCENE')
        layout.operator("bao.button")

#    The buttons       
class Button102(bpy.types.Operator):
    bl_idname = "siz1.button"
    bl_label = "1024"
    bl_description ="Ok I know. This is not the conveniant button but this is just for the idea"
    number = bpy.props.IntProperty()
    row = bpy.props.IntProperty()
    loc = bpy.props.StringProperty()
    def execute(self, context): 
        print(bpy.props.StringProperty)      
        return{'FINISHED'} 
class Button103(bpy.types.Operator):
    bl_idname = "siz2.button"
    bl_label = "2048"
    number = bpy.props.IntProperty()
    row = bpy.props.IntProperty()
    loc = bpy.props.StringProperty()
    def execute(self, context): 
        print(bpy.props.StringProperty)      
        return{'FINISHED'} 
class Button104(bpy.types.Operator):
    bl_idname = "siz3.button"
    bl_label = "4096"
    number = bpy.props.IntProperty()
    row = bpy.props.IntProperty()
    loc = bpy.props.StringProperty()
    def execute(self, context): 
        print(bpy.props.StringProperty)      
        return{'FINISHED'}   
class Button105(bpy.types.Operator):
    bl_idname = "bao.button"
    bl_label = "BAKE AO!"
    bl_description ="Bake Ambient Occlusion texture for the selected object. It should have UV and Cycle node material applied. Texture is saved in the blend file folder by default."
    number = bpy.props.IntProperty()
    row = bpy.props.IntProperty()
    loc = bpy.props.StringProperty()
    def execute(self, context):
        #active selection
        cube = bpy.context.active_object
        nm = bpy.context.object.name
        #active existing material
        material2 =cube.active_material
        nodes = material2.node_tree.nodes
        #create image
        siz = 512
        image = (bpy.data.images.get(nm +"-AObake")) or (bpy.data.images.new(nm +"-AObake", width=siz, height=siz))
        imag = bpy.data.images[nm + '-AObake']
        #test if node exist
        if 'AOtexture' in  [node.name for node in nodes]:
            print('Allready there!')
            imgnode = nodes ["AOtexture"]
            imgnode.image = imag
            imgnode.select = True
        else:
            print('not here! Building it')
            imgnode2 = nodes.new('ShaderNodeTexImage')
            imgnode2.name = "AOtexture"
            imgnode2.image = imag
            imgnode2.select = True    
        #Bake image
        bpy.data.worlds["World"].light_settings.use_ambient_occlusion = True
        bpy.data.worlds["World"].light_settings.samples = 10    
        bpy.data.worlds["World"].light_settings.distance = 10
        bpy.ops.object.bake(type='AO')
        #save image
        imag.filepath_raw = '//' + nm + '-AObake' + '.png'
        imag.file_format = "PNG"
        imag.save()
        print("image saved!")
        #nodes.remove(imgnode)
        #print("img node suppressed")
        return{'FINISHED'} 

classes = [AOBAKER, Button102, Button103, Button104, Button105]

def register():
    for c in classes:
        bpy.utils.register_class(c)
#path        
    bpy.utils.register_module(__name__)
    bpy.types.Scene.exp_path = bpy.props.StringProperty \
      (
      name = "Baked Textures To",
      default = "//",
      description = "Define the root path of baked texture",
      subtype = 'DIR_PATH'
      )

def unregister():
    for c in classes:
        bpy.utils.unregister_class(c)
#path         
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.conf_path

if __name__ == "__main__":
    register()

$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.