Simple create object script in 2.5

Hey,
I know its not officially out yet but I’ve switched over to 2.5 and am trying to grasp some of the changes in how python is implemented. All of the modules and code that I was just getting familiar with is now gone:) I’m trying to get back to just writing basic code that creates objects within the scene but its saying modules aren’t valid and commands that are in the API don’t exist…

for example:
if I’d just like to create an empty within the scene and type–

newEmpty = OBJECT_OT_object_add(type=‘EMPTY’)

it comes back to me and says that “name OBJECT_OT_object_add is not defined”

i thought that meant I wasn’t including the proper module but it keeps saying that bpyoperator and rna modules don’t exist.

would someone mind posting a simple script that creates an object in the scene so I can see how the basics have changed and then move from there?

Thanks!! --Ryan

#empty
empty = bpy.ops.object.object_add()

#or
bpy.ops.object.object_add()


#plane
bpy.ops.object.mesh_add()

#cube
bpy.ops.object.mesh_add(type='CUBE')

etc…

sample scripts

if you doanload the lastest 2.5 at graphical.org

go into the UI folder and you will fins several sample scripts

showing how to add object in 2.5

it does not show everything but it’s a beginning

hope it helps

Thanks man that helped a lot. I’m not great at interpreting API’s without some initial help but now I think I’m on my way:) That initial OBJECT_OT at the beginning of the command was really throwing me off. Would you mind explaining what that part of the command refers to? (Some commands say OBJECT_OT, others say NODE_OT or POSE_OT etc…) I can probably figure out how to use each command through trial and error but I like to fully understand whats happening. And as to those scripts, I must not have the latest build because I don’t have that folder but I’ll definately download it shortly. Again, thanks a lot I really appreciate the help on both of the questions.

–Ryan

By looking at the UI files found in the ui folder of the dev release i got it seems “OBJECT_OT_” is in front of any panel in the OBJECT tab. For the scene tab, they would have SCENE_OT_…

One other thing I’ve determined is that, OT is for an operator class, and PT is for a panel class. So basically :

“SCENE_PT_dimensions” would be a class defining the GUI panel seen in the scene tab and “SCENE_OT_dimensions” would be an operator class called by the GUI.
does that make any sense?

also remember to register your panel & operator classes :slight_smile: for example that dimension panel i just mentioned (you can find it inside “buttons_scene.py”). In the file when it is created it starts like so:


class SCENE_PT_dimensions(RenderButtonsPanel):
       (insert code here)

and then at the end of the file you’ll find:
bpy.types.register(SCENE_PT_dimensions)

there are a few other threads around with examples in them so you can take a look at those if you get a bit lost :slight_smile: I’m quite new at this myself I’ve only really started playing around with this a few days ago.

Thanks that cleared it up a good bit.

I’m transferring over from rigging in Maya and am trying to learn to program at the same time (an essential skill that I lack). For many of the things you do in Blender, I now see that the console updates with the python scripting that is occuring–which is pretty cool. The one area that it doesn’t seem to display are when you change properties? If you hover over the name field it says that the python to access the name field is Object.name, but if you actually change it it doesn’t display in the console. Further, if you made an empty, and follow its advice to change the name:

kieth = bpy.ops.object.object_add()
kieth.name = “kieth”

it says : "AttributeError: ‘nonetype’ object has no attribute ‘name’

it does much the same stuff for the location and rotation properties etc…

So if thats true, how should I be using the mouse over advice? Is there extra syntax that I’m not seeing? Thanks for any help and to everyone who’s posted so far.

don’t forget the API is still under developement it’s a WIP !

so thinghs might not work as they should

2.5 first release wont be before october so

don’t count too much on 2.5 to do things with yet at least not before official release
and even then that’s gone be a beta real 1 not with bugs again !

its nice to see people getting involved but there is almost no tut and doc available yet
hope it changes soon!

hope it helps

Right. If its not working because its not active yet, thats fine. I knew stuff would be like that when I left off learning on 2.4x . That said, usually when things don’t work for me its because I’m new at stuff and screwing up rather than a bug or not current feature:) I didn’t see anything saying that properties of objects weren’t live so I just assumed it was more complicated to do than I thought. Do you think the way I was doing it is the correct method than?

Ryan

Properties of objects are live, but object_add() doesn’t return the object it created. So your variable ‘kieth’ doesn’t contain the object, but None.

I don’t know yet how you can get the object you create. You can of course do a comparison between lists that contain all the objects in the .blend ( bpy.data.objects.keys() ), but that isn’t vary practical on larger scenes.

By the way, to use object_add() you need to provide the object type. Right now it automatically creates an empty, but it is complaining in the console. So use this:
bpy.ops.object.object_add(type=‘EMPTY’)

Assuming you add the object within the execute method of an operator, then you can access the object using the active_object attribute of the passed in context:


import bpy

class MyOperator(bpy.types.Operator):
    __idname__ = "my.op"
    def execute(self, context):
        bpy.ops.object.object_add(type = "EMPTY")
        ob = context.active_object
        
        print(ob.name)
        
        return ("FINISHED", )
        
bpy.ops.add(MyOperator)

if __name__ == "__main__":
    bpy.ops.my.op()

Note that as of revision 22542 (not the latest), it wasn’t possible to add an arbitrary object to the scene, i.e. there’s no equivalent to scene.objects.new method from the 2.4x API. It is possible to create a mesh using a script, but adding it to the scene currently needs to be done manually.

Thanks for the example forTe. I’ve still got to get used to things being dependent on the context.
I seem to remember that when the concept was introduced they said we could also change the context with Python. Would you know anything about this?

Edit:
To answer my own question: it actually seems simpler than I had expected. It can be defined like idname
context = “mesh_edit”

Well I can’t take credit for this, but JesterKing on the IRC channels gave some great advice. He said to create the object:

bpy.ops.object.object_add(type='EMPTY')

and then to iterate through the objects in the scene to see which one is selected:

for ob in bpy.data.objects:
     if ob.selected:
     newEmpty = ob

this leaves you free to adjust properties etc…

newEmpty.name = "marker1"

I have a new question about armatures but I think I’ll start a new thread to make it easier for people to find this data when others are looking for this stuff when 2.5 is live:)

one thing here

how can you create an object then find if it is selected

i mean you try to find it before it is selected ?

Thanks

when you create a new item it is automatically selected. This makes the .selected property ideal for accessing items that have just been created because you are guaranteed to be accessing the correct object. you could also probably do it other ways (like checking the type of each object in the scene and pulling only the empties) but at the moment that seems to be the simplest brute force method:)

but you cold i guess also with the given name re select this object then do whatever modif you want i think

hope it’s possible to select but not certain about that one ?

and we learn new things about taht 2.5 a little everyday !
may be will be fine when it comes out to work scripts i mean

Thanks

once you “get” it the first time (like when I assigned it the variable name newEmpty) you can access it any time to change any of the properties or constrain it or whatever. Getting it the first time is a bit of a challenge.

why is it a challenge to select it the first time ?

Thanks

Well i originally didn’t understand that the create function didn’t return an object of the type that was called (which still doesn’t quite make sense but I’m beginning to understand why). So unless I could predict what the name of the object that was created would always be, I couldn’t figure out how to access it. Hence, the .selected tag (which isn’t elegant but it seems to be the simplest way)?

you mean that when you create an object and it is eauomatically selected

you cannot acces directly this selected object

so the why the need to make a for loop to get the selected object ?
then find it and give it a new name!

i tough we could do what we did in 2.49 like in the scene get the selected object ?

Happy blendering

mp3man4: what is wrong with forTe’s method?

ob = context.active_object

You won’t have to do any iterations then.