My first addon (problems)

I have this code in file: algo.py


import bpy
bpy.ops.object.select_by_type(extend=False, type='MESH') # select all mesh
selmesh =  bpy.context.selected_objects # list all objects selected

cnt = 0
while cnt < len(selmesh[:]):
    namem = selmesh[cnt].name
    #ob = bpy.context.active_object  #<-- get object active
    bpy.ops.object.select_name(name=namem, extend=False) #<-- select by name
    bpy.context.scene.objects.active = bpy.data.objects[namem] #<-- set object active    
    ob = bpy.context.active_object  #<-- geting active object
    bpy.ops.object.modifier_add(type='UV_PROJECT') #<--  apply the modifier
    cnt += 1

and execute this, with this code


bl_info = {
    "name": "My first addon test",
    "description": "blablabla",
    "author": "my name blabla",
    "version": (0, 1),
    "blender": (2, 61, 0),
    "api": 31236,
    "location": "View3D > Add > Mesh",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"
                "Scripts/My_Script",
    "tracker_url": "http://projects.blender.org/tracker/index.php?"
                   "func=detail&aid=<number>",
    "category": "Add Mesh"}


import bpy


class ObjectPanel(bpy.types.Panel):
    bl_label = "mio"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "object"

    def draw(self, context):
        self.layout.operator("script.python_file_run", text="My button execute pyfile").filepath='/home/user/.blender/2.61/scripts/addons/algo.py'

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()

and dont work!!, but copy & paste in the text editor and run (in console too), works it well :open_mouth: :S why?
I can group as many tasks into one button? Thank you.

Solved…

This code:

class ObjectPanel(bpy.types.Panel):
    bl_label = "Hello from Object context"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "object"

Dont work well.

This:

bl_info = {
    "name": "My first addon test",
    "description": "blablabla",
    "author": "my name blabla",
    "version": (0, 1),
    "blender": (2, 61, 0),
    "api": 31236,
    "location": "View3D > Add > Mesh",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"
                "Scripts/My_Script",
    "tracker_url": "http://projects.blender.org/tracker/index.php?"
                   "func=detail&aid=<number>",
    "category": "Add Mesh"}

import bpy

#
#    Menu in toolprops region
#
class ToolPropsPanel(bpy.types.Panel):
    bl_label = "test"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOL_PROPS"
 
    def draw(self, context):
        self.layout.operator("zebus.zebus", text='Good button') 
 
#
#    The Hello button prints a message in the console
#
class OBJECT_OT_HelloButton(bpy.types.Operator):
    bl_idname = "zebus.zebus"
    bl_label = "aplicar modificador"
    #country = bpy.props.StringProperty()
    
    def execute(self, context):
         C = context
         layout = self.layout
         obj = context.object
         selmesh = C.selectable_objects
         cnt = 0
         while cnt < len(selmesh[:]):
             namem = selmesh[cnt].name
             bpy.ops.object.select_name(name=namem, extend=False) #<-- select by name
             bpy.context.scene.objects.active = bpy.data.objects[namem] #<-- set object active    
             ob = bpy.context.active_object  #<-- geting active object
             bpy.ops.object.modifier_add(type='UV_PROJECT') #<--  apply the modifier
             cnt += 1
         return{'FINISHED'}

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()

Work fine!! :slight_smile:

You can simplify your code a lot if you don’t use bpy.ops:


modifiers = [ob.modifiers.new(name='UV_PROJECT', type='UV_PROJECT') for ob in bpy.data.objects if ob.type == 'MESH']

Might you to explain to me the syntax, please? Why that order?

This syntax i understand it better:

import bpy
for ob in bpy.data.objects:
    if ob.type == 'MESH':
        ob.modifiers.new(name='UV_PROJECT', type='UV_PROJECT')

:smiley: thank you very much!! and Merry Christmas!!