0
$\begingroup$

I was wondering how to write a conditional in a script so that a list of commands runs only if an object IS NOT inside the blender scene currently.

For example if "Cube" is already in the scene I want the script to do nothing. If there is no object name "Cube" I want a list of commands to run. I feel this should be pretty simple but I'm not really good with syntax at the moment.

More Specifically(EDIT): I am making an operator button that is added to a tab in the tools section. When the operator button is pressed by the user I only want it to perform a set of commands IF there is not already a object named "Cube" inside the scene. Something like: if bpy.data.groups "Cube" = False: bpy.ops.mesh.primitive_cube_add. I know that is the wrong syntax so I am looking for the right way. Does that make sense?

Thanks in advance! Best, Brad

$\endgroup$
3
  • $\begingroup$ "I want a list of commands to run" Which "commands"? When? Upon deletion? How? Constantly looping? Once? As far as I know for an operator to run it has to be attached to some event, constantly checking a scene for objects would be a potentially huge resource hog. I don't think this question is very answerable or your intentions clear. $\endgroup$ Commented Oct 12, 2018 at 1:32
  • $\begingroup$ Ah I understand let me be more specific. I am making an operator button that is added to a tab in the tools section. When the operator button is pressed by the user I only want it to perform a set of commands IF there is not already a object named "Cube" inside the scene. Something like: if bpy.data.groups "Cube" = False: bpy.ops.mesh.primitive_cube_add. I know that is the wrong syntax so I am looking for the right way. Does that make sense? $\endgroup$ Commented Oct 12, 2018 at 3:59
  • 1
    $\begingroup$ You get the list of objects via bpy.data.objects. If you want to check whether an object with the name 'Cube' exists, you would have the condition bpy.data.objects.get('Cube') != None $\endgroup$ Commented Oct 12, 2018 at 4:54

1 Answer 1

2
$\begingroup$

Don't recommend addressing by name.

Using name as a sole variable to lookup objects in blender is fraught with danger.

If you really do only want to test for the original, and named "Cube" cube object there are two conditions to look at

cube_in_blend = bpy.data.objects.get("Cube") is not None
cube_in_scene = context.scene.objects.get("Cube") is not None

If cube not in blend then

if not cube_in_blend:
    bpy.ops.mesh.primitive_cube_add()
    cube = context.object

will result in both being true. If the cube is in file and not in scene

if cube_in_blend and not cube_in_scene:
    cube = bpy.data.objects.get("Cube")
    context.scene.objects.link(cube) 

links the object named "Cube" to that scene. There is no guarantee that it has 8 equal length edges, 4 equal area faces and other props expected of a cube.

Alternatively

Just like a scene has a camera, context.scene.camera can set it up to "have a cube"

bpy.types.Scene.cube = bpy.props.PointerProperty(type=bpy.types.Object)

And in scene with cube can be set via

scene.cube = scene.objects.get("Cube")

Using method outlined in answer to Limit "prop_search" to Specific Types of Objects and modified to associate a particular cube with a scene. The cube is chosen from file objects that have a name beginning with "Cube". The cube is linked to the scene if not already. For the case where there is no cube in the scene could add the add primitive operator in the markup.

import bpy    

class OBJECT_PT_HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        layout.prop(scene, "cube")

def scene_mychosenobject_poll(self, object):
    return object.name.startswith("Cube")

def link_to_scene(self, context):
    if self.cube and self.cube.name not in self.objects:
        self.objects.link(self.cube)

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.cube = bpy.props.PointerProperty(
        type=bpy.types.Object,
        poll=scene_mychosenobject_poll,
        update=link_to_scene
    )


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.cube


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .