Blender 2.5 - need a little Python Help

Hey Guys

I have just started to teach myself Python in Blender 2.5

So hopefully this is an easy question to answer

I am using the Pantin Rig and trying to Hide different body parts with the “restrict_view”
and add this to a button that lives on the Properties Tab in the 3D view (n)

I have got this working but I can only make one object disappear with one button
this is fine for some sections of the rig but other parts are made up of more than one mesh object (eg. the Head, Neck, eyes, eylids etc)

The Way I have done it is by creating a varibale
eg:


HeadMesh = bpy.context.scene.objects['Head']

this grabs the object called “Head” and then I call it later and add it to a column button


Face.prop(HeadMesh, "restrict_view", toggle=True, text="HIDE")

This works, as I said, but I cannot figure out how to get the variable HeadMesh to include more that one object
This also may be a silly way of doing it - but hey, I’ve only been learning Python for a day

Can somebody help me out with calling multiple objects and changing a value on all of them?

thanks in advance

Try exploring the “bpy.data” object

for instance in the array:

bpy.data.objects

you have a list of all the objects in the scene

So you can do something like:


for ob in bpy.data.objects:
    ob.restrict_view = True

The “bpy.data” object is a very quick way to reach properties.

thanks for the reply
I see it’s a little easier to get to the data with “bpy.data.objects”


for ob in bpy.data.objects:
     ob.restrict_view = True

So this gets all the objects and restricts the view - nice one

how to I get specific objects (by name)?
Say If I only want the Head & Neck Object

A better example for your case would be…

If you have a list with the names of the objects:


list_of_objects_names = ["myobj_1", "myobj_2", "myobj_3"]

for ob_name in list_of_objects_names:
    bpy.data.objects[ob_name].restrict_view = True

making progress :slight_smile:

So now I know how to restrict the view of all the objects in the list
(lists will be very helpful)

But is it possible to toggle this with a button?

I don’t seem to be able to conduct a for statement inside the button

is there somewhere I can read about hot to get buttons to do what you want?

You have to create an operator for that and a button that triggers that operator.

There is a template for operators inculded in the Text editor. Check the menu Text->Script Templates.

Check for info here:
http://www.blender.org/documentation/250PythonDoc/contents.html

There is a link with sample code for operators and panels, where it says “An introduction to blender and python can be found at”.

See the titles “Example operator” and “Example panel”

Also there are nice code examples at Crouch’s thread:
http://blenderartists.org/forum/showthread.php?t=164765

now I know what I need to learn
thanks for the help

I have already pulled apart some of the scripts on crouchs thread to align my buttons

now to pull apart some more code…

i found the first link for Text

but cannot find the other one for panels

“An introduction to blender and python can be found at”.

can you give the link please

2 - @ Waylow

i hope you an come back with a small script showing how to do this
miagh be usefull for other peoples later on
keep up the good work

Thanks

I’m pretty sure you would have already visited this page but this is the link he means
http://wiki.blender.org/index.php/Dev:2.5/Py/API/Intro

I am still struggling with the problem but I have managed to figure some stuff out

I can Create an operator that toggles the restrict_view and add this to a button
but I still can’t figure out how to get this operator to change more than one object at a time

yes i rememeber seeing it

it’s the beginning of intro to new API 2.5

but hope they can add more examples soon

i did a small test with the exampl;e for hiding ob

and seem to work nicely

it would be nice to see a little example script with a class and button to show how for instance
to hide or to show selected objects

and may be add it to the Crouch thread as another example

one thing here you don’t have to explicitly set the scene or the context
almost like if this was already predefined!

any idea on this

cause i’m trying to do that for adding custom objects and having a prolbem with this
and would like to understand more about it

Thanks

Try making a list with the names of the objects you want to restrict its view.

For instance you could put the names in a list called list_of_objects_names. and then restrict their view all at “once” (in one operator).

For adding names to a list, first initialize the list:


list_of_objects_names = []

To add a name to the list


list_of_objects_names.append("my_obj_name_1")

repeat that for each object name

and then to restrict view of all the objects named in the list:


for ob_name in list_of_objects_names:
    bpy.data.objects[ob_name].restrict_view = True

Thanks Ecectiel

I did manage to solve changing a list of items all at ones - but I am working on being able to toggle the Retrict_view (the new learning point)

ie. make the operator change the value -
If it’s True then make it False
If it’s False then make it True

But would I need to make and if statement for each ob_name in the list
or is it possible to create one if statement that toggles all the objects in the list

This is an example of one if statement for each object (I don’t know if it would work, haven’t tested it yet - and it seems quite cumbersome if there are several more objects in the list)


list_of_objects_names = []

list_of_objects_names.append("Obj_1")
list_of_objects_names.append("Obj_2")

for ob_name in list_of_object_names:
	if bpy.data.objects["Obj_1"].restrict_view == True
		bpy.data.objects["Obj_1"].restrict_view = False
	elsif bpy.data.objects["Obj_1"].restrict_view == False
		bpy.data.objects["Obj_1"].restrict_view = True

	if bpy.data.objects["Obj_2"].restrict_view == True
		bpy.data.objects["Obj_2"].restrict_view = False
	elsif bpy.data.objects["Obj_2"].restrict_view == False
		bpy.data.objects["Obj_2"].restrict_view = True

The must be a better way than this

I will adapt this simple code to my situation and see if it actually works in an operator

Once you have all the names in the list you don’t need to write their names explicitely inside the FOR loop. Use the “ob_name” variable:

list_of_objects_names = []

list_of_objects_names.append("Obj_1")
list_of_objects_names.append("Obj_2")

for ob_name in list_of_object_names:
    if bpy.data.objects[ob_name].restrict_view == True
        bpy.data.objects[ob_name].restrict_view = False
    else
        bpy.data.objects[ob_name].restrict_view = True

Check the Python language manual for the FOR loop, you’ll understand how it assigns the names to the “ob_name” variable on each iteration.

Solved

Well, I didn’t do it but Kiopaa did

here is a simple example version of the code


import bpy
import re

#This class draws the Panel in the 3D view (the n properties)
class VisibilityPanel(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_label = "Visibility"

    def poll(self, context):
        try:
           ob = context.active_object
           return (ob.name == "the_name_of_your_Object")
        except AttributeError:
           return 0

#this draws the layout and puts the operator in it
    def draw(self, context):
        layout = self.layout
        col = layout.column()
        row = col.row(align=True)
        row.operator("vsb_Parts", text="Head")
        


def is_selected(context, names):
    try:
        for name in names:
            if context.active_pose_bone.name == name:
                return True
        for bone in context.selected_pose_bones:
            for name in names:
                if bone.name == name:
                    return True
    except AttributeError:
        pass
    return False


#This operator toggles the visibility property of all the Meshes in the list of objects
class OBJECT_OT_Visib_Parts(bpy.types.Operator):
    bl_label = "Parts"
    bl_idname = "vsb_Parts"

    def invoke(self, context, event):
        for mesh in ['obj_1', 'obj_2','obj_3']:
           bpy.data.objects[mesh].restrict_view = not(bpy.data.objects[mesh].restrict_view)
           bpy.data.objects[mesh].restrict_render = not(bpy.data.objects[mesh].restrict_render)
        return{'FINISHED'}


bpy.types.register(OBJECT_OT_Visib_Parts)
bpy.types.register(VisibilityPanel)


What is it good for you ask?

well if you have a character rig that you want the hide different body parts you can
(just as long as the body parts are separate objects you can easily toggle them on and off out of the way

It could also be adapted to toggle/change or display other properties in the side panel

thanks for the help Kiopaa