2
$\begingroup$

I am writing an addon,I am here defining variables based on a list to use it in "for in" statement.But when I run the addon it is not working properly or not working at all.

description of the addon

The addon is made to disable visablity, selection and rendering based on visablity,selection and rendering so for instance if I want to make the hidden object non renderable I can use the addon to do that, I can also make the unselectable hidden and so on, it is useful in some cases like Boolean which we hidded an want to make them non renderable and so on.

It works as following, there is 2 lists and 2 booleans when you run the addon,the 1st Boolean define what will be done (disable or enable) 1st list define which property will be edited (like renderable in the Boolean modifier example). 2nd Boolean define when should the addon be done(when the property is disabled or enabled) 2nd list define the property which the addon will be dobe based on, (like the visability in the Boolean mdofier example)

code

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Restrict Object",
    "author": "Omar Ahmed",
    "version": (1, 0),
    "blender": (2, 75, 0),
    "location": "View3D > Object > ",
    "description": "Enable/Disable viewport visability,selection and rendering based on restrict options",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Object"}


import bpy
from bpy.props import EnumProperty,BoolProperty

class RestrictObject(bpy.types.Operator):
    """Restrict Object visability"""
    bl_idname = "object.restrict_object"
    bl_label = "Restrict Object"
    bl_options = {'UNDO','REGISTER'}

    Operation_boolean =  BoolProperty(name="Disable", description="Disabl selected property based on the other selected property", default=True)
    Operation_list =     EnumProperty(items = [('Viewport visibility', 'Viewport visibility', 'viewport visibility'), 
                                      ('Viewport selection', 'Viewport selection', 'viewport selection'),
                                      ('Rendering','Rendering','Rendering')],
                             name="Property",
                             description="Select the property you want to change",
                             default="Rendering")   
    Original_boolean =  BoolProperty(name="Disabled", description="The original property", default= True)
    Property_list =     EnumProperty(items = [('Viewport visibility', 'Viewport visibility', 'viewport visibility'), 
                                      ('Viewport selection', 'Viewport selection', 'viewport selection'),
                                      ('Rendering','Rendering','Rendering')],
                             name="Original property",
                             description="Select the property in which the operation will be based on",
                             default="Viewport visibility")   


    def execute(self, context):

        if self.Operation_list == "Viewport visibility" :
            Restricted_property = "object.hide"
        elif self.Operation_list == "Viewport selection" :
            Restricted_property = "object.hide_select"
        else :
            Restricted_property = "object.hide_render"


        if self.Property_list == "Viewport visibility" :
            Original_property = "object.hide"
        elif self.Property_list == "Viewport selection" :
            Original_property = "object.hide_select"
        else :
            Original_property = "object.hide_render"


        for object in bpy.context.scene.objects:
            if Original_property == self.Original_boolean :
                Restricted_property = self.Operation_boolean

            return {'FINISHED'}


class RestrictObjectPanel(bpy.types.Panel):
    """Restrict Object Panel"""
    bl_idname = "SCENE_PT_Restrict_Object"
    bl_label = "Restrict Object Panel"

    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = 'scene'


    def draw(self, context):
        layout = self.layout
        layout.operator(RestrictObject.bl_idname, text = "Restrict object", icon = 'FILE_REFRESH')


def register():
    bpy.utils.register_class(RestrictObject)
    bpy.utils.register_class(RestrictObjectPanel)

def unregister():
    bpy.utils.unregister_class(RestrictObject)
    bpy.utils.unregister_class(RestrictObjectPanel)


if __name__ == "__main__":
    register()

I guess that defining variable portion is the wrong,but when I try to fix it it give me more errors,so I want to get a review here before doing anything else.

$\endgroup$
2
$\begingroup$

The execute function is indeed where the magic is failing :)

yours

def execute(self, context):

    if self.Operation_list == "Viewport visibility" :
        Restricted_property = "object.hide"
    elif self.Operation_list == "Viewport selection" :
        Restricted_property = "object.hide_select"
    else :
        Restricted_property = "object.hide_render"


    if self.Property_list == "Viewport visibility" :
        Original_property = "object.hide"
    elif self.Property_list == "Viewport selection" :
        Original_property = "object.hide_select"
    else :
        Original_property = "object.hide_render"


    for object in bpy.context.scene.objects:
        if Original_property == self.Original_boolean :
            Restricted_property = self.Operation_boolean

        return {'FINISHED'}

The most important thing here to realize is that you aren't executing code in your assignment to the Restricted_property variable, but you might want to dive deeper into Python to fully appreciate this.

my Pythonic rewrite

def execute(self, context):

    op_dict = {
        "Viewport visibility": "hide",
        "Viewport selection": "hide_select"
    }

    # if self.operation_list is not explicitely mentioned in op_dict above
    # using .get() will return 'hide_render' instead.
    Restricted_property = op_dict.get(self.Operation_list, 'hide_render')
    Original_property = op_dict.get(self.Property_list, 'hide_render')

    for object in bpy.context.scene.objects:
        if getattr(object, Original_property) == self.Original_boolean:
            setattr(object, Restricted_property, self.Operation_boolean)

    return {'FINISHED'}

I think that's what you want anyway. Notice the use of getattr and setattr they are very useful for dynamic programming.

Additionally I've placed the return statement after the loop, so the loop will iterate over all objects (but if that was intentional returning early, revert that change)

https://docs.python.org/3/library/functions.html#setattr https://docs.python.org/3/library/functions.html#getattr

$\endgroup$
  • $\begingroup$ I added the code above $\endgroup$ – Omar Emara Jul 8 '15 at 15:19
  • $\begingroup$ thanks for the answer,I am completely new to python so I will take time to get used to this things,But blender is giving me syntax error in "hide_select" in the line 5 in your rewrite,what is the problem? $\endgroup$ – Omar Emara Jul 9 '15 at 13:11
  • $\begingroup$ I forgot a comma in the op_dict . :) $\endgroup$ – zeffii Jul 9 '15 at 13:54
  • $\begingroup$ but an unrelated problem is that you haven't added some of your properties to the draw function.. $\endgroup$ – zeffii Jul 9 '15 at 13:59
  • $\begingroup$ Most of my properties are going to be custom properties after the excution of the script,and really thanks for your help. $\endgroup$ – Omar Emara Jul 9 '15 at 18:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.