New to Scripting: How to create a pure conditional Addon

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Goodfly
Posts: 2
Joined: Sun Sep 09, 2012 1:13 am

New to Scripting: How to create a pure conditional Addon

Postby Goodfly » Sun Sep 09, 2012 2:16 am

Hello everyone, I am a new member and this is my first post. I am also new to scripting in Blender, so I hope that you give me a push to make a good start with that.
I wrote a script to check the mode of the objects in the scene so that if it was 'EDIT', it turns it into 'OBJECT', and here is the code :

import bpy
for ob in bpy.data.objects:
if ob.mode == 'EDIT':
bpy.ops.object.editmode_toggle()

It works fine from Text Editor, but how to convert it to an add-on to let it be executed automatically once I open Blender, without a panel or a menu or a shortcut.
In other words : Is it possible that the add-on have a pure For loop or even a pure If statement ?

and thank you in advance..

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Sun Sep 09, 2012 10:35 am

in text editor you got a Template menu, have a look at e.g. UI panel simple or the like to see what is required for an addon (you basically need a bl_info struct, register and unregister functions)

I would use a persistent handler to execute code whenever a .blend is loaded (should also occur on startup with default scene), load_post seems appropriate for this.
http://www.blender.org/documentation/bl ... dlers.html

Note that you need to enable your addon and save as default (so that it is always activated and creates the persistent handler on launch)
I'm sitting, waiting, wishing, building Blender in superstition...

Goodfly
Posts: 2
Joined: Sun Sep 09, 2012 1:13 am

Postby Goodfly » Sun Sep 09, 2012 9:48 pm

Thank you CoDEmanX for your reply..

So, generally speaking, to make an add-on containing a condition depending on an operator execution or a mouse movement or a certain event, I should go for Handlers..

Thank you again CoDEmanX...you lit my way

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Mon Sep 10, 2012 1:24 pm

well, it's more difficult than that.

Generally, you can wrap your entire code with an if-statement to execute on certain condition only. Of course not around a whole addon code, but inside the main function.

If you want to react on certain events like frame change, .blend loading, rendering etc. you can go with app handlers, which are basically callback function.

In other cases you may not want to wait for such an event, but trigger an addon's operator manually. You can define a poll() function for the operator, which needs to return True or False. True if all required conditions are met for the operator, False if not. A condition could be "must be in edit mode, object must be type MESH".

Panels got a bl_context setting, if you want it to show in object mode only, you simply set bl_context = "object".
I'm sitting, waiting, wishing, building Blender in superstition...

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Mon Sep 10, 2012 1:26 pm

what is missing in Blender, is a possibility to register callback functions, which get called on certain data changes, like a vertex you defined. I hope such a feature will be added in future (callback + custom data for mesh geometry elements)
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests