Addon scripting in Python for Dummies

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
Pharion
Posts: 0
Joined: Thu Jul 26, 2007 12:01 pm

Addon scripting in Python for Dummies

Post by Pharion » Tue Oct 05, 2010 1:51 pm

Its most likely due to my lack of intelligence, but I'm finding that starting scripting in Blender is a real struggle.
I've started breaking the addon code to create a simple button in the hopes that it will not only help me but bee a good template for the wiki, but there are so many gaps in what I know that I have started to struggle.

I hoped to create a button to apply the location rotation and scale of an object, for example When you apply a location to an object you get the responce:

bpy.ops.object.location_apply()

ob = sc.objects[obj.name] I assume gives you the object

so why doesn't ob.location_apply() work?

Would anyone be able to fill in the blanks below, or explain why the above doesnt work, and the best place to look for an explanation on how it should be done?

Code: Select all


bl_addon_info = {
    'name': 'Experiment Two Buttons',                                        # Name that appears in the Addon List
    'author': 'insert your name here',                                     # your name if your doing the writing
    'version': 'this is my nth attempt',                                   # I've screwed this up, this many times
    'blender': (2, 5, 3),                                                  # no idea, doesnt seem to change anything that I can see
    'location': 'in the cube object list at the bottom',                   # where will you see the addon in the blender UI
    'description': 'create a button',                                      # say what it does
    'wiki_url': 'http://www.arcane3d.co.uk',                               # website to say how this addon works
    'tracker_url': 'http://www.arcane3d.co.uk',                            # where to say HELP I FOUND A BUG
    'category': 'Experiment'}                                              # will create a new addon category if it doesnt already exist


import bpy                                                                 # grab the stuff that other people have written to make this work

class OBJECT_PT_testaddon(bpy.types.Panel):# Create a panel?
 bl_label = "Test Panel Header"                                            # will appear as the label for your addon
 bl_space_type = "PROPERTIES"                                              # where to place the addon in the Blender UI?
 bl_region_type = "WINDOW"                                                 # where to place the addon in the Blender UI?
 bl_context = "object"                                                     # where to place the addon in the Blender UI?

 def draw_header(self, context):                                           # how to draw the information above in the header
  layout = self.layout                                                     # blender desides on how its shown?


 def draw(self, context):                                                  # the meat and two veg of the Addon UI/
  global obj                                                               # obj variable that can be accessed by all classes
  obj = context.object                                                     # assign the selection to obj
  layout = self.layout                                                     # get the layout information for the UI and assign it to layout
  layout.label(text="label text", icon='MONKEY')                           # text with icon to dscribe what a button or checkbox is.. moved from def draw_header
  row = layout.row()                                                       # grab the row information out of layout and assign it to row
  layout.operator("buttonMoveX", icon='FORWARD')      # edit the label on the button to say what state it is in
  layout.operator("buttonMove-X", icon='BACK') 


def moveTheBallF():                                                         # lets say what the cube does
    global state                                                           # setting state to be a global variable
    print('Button Clicked')                                               # lets shout out so we can see whats happening in the event window
    sc = bpy.context.scene                                                 # sc = the current scene?
    ob = sc.objects[obj.name]                                              # ob = the name of the object obj referes to
    ob.location_apply()
                                              # add one to the x location of the cube 
    
def moveTheBallB():                                                         # lets say what the cube does
    global state                                                           # setting state to be a global variable
    print('Button Clicked')                                               # lets shout out so we can see whats happening in the event window
    sc = bpy.context.scene                                                 # sc = the current scene?
    ob = sc.objects[obj.name]                                              # ob = the name of the object obj referes to
    ob.location.x -= 1.0                                                 # add one to the x location of the cube 
    
#---------------------------------------------------------------------

class VIEW3D_OT_CreateButtonOne(bpy.types.Operator):                       # create the class for your button
    '''Button One'''                                                       # say what the hell does this button do when you hover your mouse over it?
    bl_idname = "buttonMoveX"                                              # If this is set, the panel gets a custom ID, otherwise it takes the name of the class used to define the panel.
    bl_label = ""                                                              # The panel label, http://www.blender.org/documentation/250PythonDoc/bpy.types.Panel.html
    bl_options = {'REGISTER', 'UNDO'}                                        # Options for this panel type
    @classmethod                                                           # ?
    def poll(cls, context):                                                   # Boolean: If this method returns a non-null output, then the menu can be drawn.
        return context.active_object != None                               # as long as something is selected, return the active Object?
    def execute(self, context):                            #1 ?
        moveTheBallF()                                    #2 ?
        return {'FINISHED'}    
    
class VIEW3D_OT_CreateButtonTwo(bpy.types.Operator):                      # create the class for your button
    '''ButtonTwo'''                                                       # say what the hell does this button do when you hover your mouse over it?
    bl_idname = "buttonMove-X"
    bl_label = ""                                                              # The panel label, http://www.blender.org/documentation/250PythonDoc/bpy.types.Panel.html
    bl_options = {'REGISTER', 'UNDO'}                                        # Options for this panel type
    @classmethod                                                           # ?
    def poll(cls, context):                                                   # Boolean: If this method returns a non-null output, then the menu can be drawn.
        return context.active_object != None                               # as long as something is selected, return the active Object?
    def execute(self, context):                            #1 ?
        moveTheBallB()                                    #2 ?
        return {'FINISHED'}                                #3 ?

def register():                                            #4 ?
    pass                                                #5 ?
def unregister():                                        #6 ?
    pass                                                #7 ?
if __name__ == "__main__":                                #8 ?
    register()                                            #9 ?





ldo
Posts: 0
Joined: Sun Nov 07, 2010 11:30 pm
Location: Hamilton, New Zealand

Post by ldo » Tue Nov 09, 2010 4:00 am

When getting to grips with the Python API, it helps to start bit by bit. The Console is particularly useful for trying things out here.

For example, location_apply works on the current selected object. Start with a new default document (with the default cube in it), change the Timeline window at the bottom to show the Console instead, and perhaps make it a bit bigger. Move the default cube to some random position away from the origin; duplicate it, and move the copy to some other random position.

Select one of the copies, and in the Console, type

Code: Select all

 bpy.ops.object.location_apply()
Then try

Code: Select all

 bpy.ops.object.location_clear()
The object doesn’t move. But if you select the other copy (the one you didn’t do location_apply on) and re-execute the location_clear command, it will jump back to the origin.

See if you can get this far. Then we can look at getting your UI working.

Post Reply