Previous Thread  Next Thread

chat icon New to Scripting: How to create a pure conditional Addon

Goodfly

Posted: Sun Sep 09, 2012 1:16 am
Joined: 09 Sep 2012
Posts: 2
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..
Reply with quote


CoDEmanX

Posted: Sun Sep 09, 2012 9:35 am
Joined: 05 Apr 2009
Posts: 687
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/blender_python_api_2_63_17/bpy.app.handlers.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...
Reply with quote


Goodfly

Posted: Sun Sep 09, 2012 8:48 pm
Joined: 09 Sep 2012
Posts: 2
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
Reply with quote


CoDEmanX

Posted: Mon Sep 10, 2012 12:24 pm
Joined: 05 Apr 2009
Posts: 687
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...
Reply with quote


CoDEmanX

Posted: Mon Sep 10, 2012 12:26 pm
Joined: 05 Apr 2009
Posts: 687
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...
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group