using BoolProperty

i got my MakeSurfacePlane script, and have now got the align to view code. now how do i use the boolproperty to execute my align function?

Not sure what you are asking, but a simple if should do, no?

Hi, a boolproperty to draw ui for your script?

may be you could get the code here.

http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Properties

Hello, may be you could check the oficial wiki, there is a good example for draw panels with all type of properties like boolean by Thomas Larsson.

i hope it’s useful for you

enjoy code.

#----------------------------------------------------------

File panel_props.py

#----------------------------------------------------------
import bpy
from bpy.props import *

Clean the scene and create some objects

bpy.ops.object.select_by_type(type=‘MESH’)
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add(location=(-3,0,0))
cube = bpy.context.object
bpy.ops.mesh.primitive_cylinder_add(location=(0,0,0))
cyl = bpy.context.object
bpy.ops.mesh.primitive_uv_sphere_add(location=(3,0,0))
sphere = bpy.context.object

Define an RNA prop for every object

bpy.types.Object.myRnaInt = IntProperty(
name=“RNA int”,
min = -100, max = 100,
default = 33)

Define an RNA prop for every mesh

bpy.types.Mesh.myRnaFloat = FloatProperty(
name=“RNA float”,
default = 12.345)

Set the cube’s RNA props

cube.myRnaInt = -99
cube.data.myRnaFloat = -1

Create ID props by setting them.

cube[“MyIdString”] = “I am an ID prop”
cube.data[“MyIdBool”] = True

Property panel

class MyPropPanel(bpy.types.Panel):
bl_label = “My properties”
bl_space_type = “VIEW_3D”
bl_region_type = “UI”

def draw(self, context):
    ob = context.object
    if not ob:
        return
    layout = self.layout
    layout.prop(ob, 'myRnaInt')
    try:
        ob["MyIdString"]
        layout.prop(ob, '["MyIdString"]')
    except:
        pass
    if ob.type == 'MESH':
        me = ob.data
        layout.prop(me, 'myRnaFloat')
        try:
            me["MyIdBool"]
            layout.prop(me, '["MyIdBool"]')
        except:
            pass

Registration

bpy.utils.register_class(MyPropPanel)

Hello, may be you could check the oficial wiki, there is a good example for draw panels with all type of properties like boolean by Thomas Larsson.

i hope it’s useful for you

enjoy code.



#----------------------------------------------------------
# File panel_props.py
#----------------------------------------------------------
import bpy
from bpy.props import *
 
# Clean the scene and create some objects
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_cube_add(location=(-3,0,0))
cube = bpy.context.object
bpy.ops.mesh.primitive_cylinder_add(location=(0,0,0))
cyl = bpy.context.object
bpy.ops.mesh.primitive_uv_sphere_add(location=(3,0,0))
sphere = bpy.context.object
 
# Define an RNA prop for every object
bpy.types.Object.myRnaInt = IntProperty(
    name="RNA int", 
    min = -100, max = 100,
    default = 33)
 
# Define an RNA prop for every mesh
bpy.types.Mesh.myRnaFloat = FloatProperty(
    name="RNA float", 
    default = 12.345)
 
# Set the cube's RNA props
cube.myRnaInt = -99
cube.data.myRnaFloat = -1
 
# Create ID props by setting them.
cube["MyIdString"] = "I am an ID prop"
cube.data["MyIdBool"] = True
 
#    Property panel
class MyPropPanel(bpy.types.Panel):
    bl_label = "My properties"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
 
    def draw(self, context):
        ob = context.object
        if not ob:
            return
        layout = self.layout
        layout.prop(ob, 'myRnaInt')
        try:
            ob["MyIdString"]
            layout.prop(ob, '["MyIdString"]')
        except:
            pass
        if ob.type == 'MESH':
            me = ob.data
            layout.prop(me, 'myRnaFloat')
            try:
                me["MyIdBool"]
                layout.prop(me, '["MyIdBool"]')
            except:
                pass
 
# Registration
bpy.utils.register_class(MyPropPanel)



well, i get all the props, but not boolproperty, simpely because it returns nothing. not false nor true or an int/float…

align = BoolProperty(default = False , name = ‘Align to View’ , desctiption
= ‘Align the newmade object to the view’
)

print(align)

code above results in this in the console:
<<built-in function Boolproperty>,{default’:False…

simpely the settings of the prop, not true , false or anything…

well, i guess my problem has to become a little clearer. i have a align to view class and function, and i want a BoolProperty, that while False does nothing, and when it changes to True, call the align to view operator i made.

the problem is what BoolProperty returns:

&gt;&gt;&gt; a = bpy.props.BoolProperty(name='align to view' , default = False)
&gt;&gt;&gt; a
(&lt;built-in function BoolProperty&gt;, {'default': False, 'name': 'align to view'})

a if statement won’t work this way, how do i let this work?

extra frustrating is that it seams to work on for instance the twisted torus, where a if statement does work…

just make a test on the bool name
it will be true or false

can you give a more complete example with the if you did !

hmm retyping and removing indents did the job, weird, because blender said there was nothing wrong, while doing nothing…
next problem: the execution of my aligntoview class is removing the input things you get when adding an object, and when an if statement is doing this, the function doesn’t work properly…

for who wants to see the code:
link
my test is the add_surface_wedge class. run this script in blender and add a surface wedge, to check if my/your code is working :slight_smile:

RickyBlender is right. You just test the bool for its value:

a = bpy.props.BoolProperty(name='align to view' , default = False)
if a == True:
    alignToView() #value is True, so do your function

seems to get an error on the wege
like out of context for the extrude

line 81, in execute
bpy.ops.mesh.extrude_region_move(‘INVOKE_REGION_WIN’)
File " bpy\ops.py"
, line 177, in call
ret = op_call(self.idname_py(), C_dict, kw, C_exec)
RuntimeError: Operator bpy.ops.mesh.extrude_region_move.poll() failed, context i
s incorrect

or getting this other error

v3d = bpy.context.space_data
#current transform orientation:
CurrentTransform = v3d.transform_orientation
#set the transform orientation
v3d.transform_orientation = ‘VIEW’

\wegeclass1.py", line 62, in atv
AttributeError: ‘SpaceInfo’ object has no attribute ‘transform_orientation’

not certain this has anything to do with the boolean !

will try to see if i can find it !

how can you use a mesh operator on a nurbs surface?

the second error is strange, because im sure it has a transform orientation, will look into it.

edit:
got something, transform_orientation is in the base class bpy.types.SpaceView3D(Space)

but what is space, and how do i use this?

The internal add mesh scripts use this to do the align to view:

def add_object_align_init(context, operator):
    '''Initialize loc, rot of operator
    context: Blender Context
    operator: the active Operator (self)
    Initializes the Operators location and rotation variables
    according to user preferences (align to view)
    See AddObjectHelper class
    Returns Matrix
    '''
    if (operator
        and operator.properties.is_property_set("location")
        and operator.properties.is_property_set("rotation")):
        location = mathutils.Matrix.Translation(mathutils.Vector(operator.properties.location))
        rotation = mathutils.Euler(operator.properties.rotation).to_matrix().to_4x4()
    else:
        # TODO, local view cursor!
        location = mathutils.Matrix.Translation(context.scene.cursor_location)

        if context.user_preferences.edit.object_align == 'VIEW' and context.space_data.type == 'VIEW_3D':
            rotation = context.space_data.region_3d.view_matrix.to_3x3().inverted().to_4x4()
        else:
            rotation = mathutils.Matrix()

        # set the operator properties
        if operator:
            operator.properties.location = location.to_translation()
            operator.properties.rotation = rotation.to_euler()

    return location * rotation

I don’t know if that helps any, but maybe looking at it will give you some insight?

i don’t see any operator, so this is just how the object should be rotated to match the view, right?

in that case, the .mesh.translate operator will make the add object panel dissappear, atleast that’s what i make of it. in that case its unusable anyway.

question is can i rotate something without letting the add object panel dissappear?

actually your conclusion is incomplete because you never tested boolproperty with an if statement, if you did that you will never have came on the conclusion that boolproperty does not return True or False. You have to understand that blender types are not like python types when you use the print statement it perfectly normal that it returns the whole object , because its an object , but if you use it inside an if then it will return a True and False.

I use BoolProperty alot (and when I say alot well I mean ALOT) in my Random Texture Generator , it works always as expected take a look here —> https://github.com/kilon/Gyes/blob/master/gyes/random_texture_generator.py

on your above example if you did

if a:
print(“true”)

it would have returned True

by the way if a: is the same as if a==True:

it works now, i ended the boolproperty with ,) and it didn’t work, but didn’t get an error either, so i thought boolproperty sucked, it works now. letting the script work is a different story though…