Join Blender Studio for just €11.50/month and get instant access to all of our training and film assets!
In this video we see how, in Python, names refer to objects. In this context, an "object" refers to a thing in Python, and not necessarily a Blender object in 3D space. Knowing how names refer to things in memory is an essential part of scripting (and not wearing out your fingers from all the typing).
Slides are only visible to Blender Cloud susbcribers
Very new to Blender scripting. TAB is your Python auto-complete friend. Others new to Blender's way may benefit from knowing this.
I disagree that monkey is definitely not named 'Steve'. I think it's 'Suzanne' ....
Very helpful videos but It would be really better if we had at least English subtitles!!! In a few parts I can't really understand what Mr. Sybren says... Subtitles would be really useful!!!
Transcript please. (no subtitle)
Hi, thanks for the tutorial!
If I say:
steve = bpy.ops.mesh.primitive_circle_add()
steve will become the string {'FINISHED'}, which might be handy for error handling, but I expected it to be a reference to the created object.
If I want to immediately do something with steve, I can refer to the active object, but what if I want to just remember that specific object for later modifications, I could use these lines:
bpy.ops.mesh.primitive_circle_add()
steve = bpy.context.selected_objects[0]
However, I'd like to know if I could write that in just one line.
@Roland Gsell: the returned value of an operator will always be of this form, so you can't write what you want in one line. I would also suggest using steve = bpy.context.active_object
instead of using selected_objects[0]
. If later the active object changes, steve
will still point to the circle object you added.
@Dr. Sybren: Thanks! The strange thing is that python autocompleted activ... to active_objects - so I thought there's no single object to work with.
Maybe that's sort of a zsh autocompletion and I'm more the bash-guy ;-) I can't try it out at the moment, my Blender is rendering some stuff...
I love the new version by the way. 100 samples with denoise looks much better than 500 with 2.78..
Hi thanks you offer script tutoriall. it is really useful to learn blender specific operations. then I have one question about context. in blender manuall, it say "all context values are readonly, but may be modified through the data api or by running operators"
but when I try to re-name, by bpy.context.active_object.name = "newmonkey" it could work in blender console. is it exceptional case? or if I can only change location.x etc by context?
@fun2tax: I did it in 2 lines.
a = bpy.context.active_object
a.name = "newmonkey"
@sybren: thanks now I seems understand , bpy.context read only meaning better. and Yes stephan, it actually work for me too. then I though it can be changed by context too.
@fun2tax: That remark only relates to the names in bpy.context
themselves. For example, you cannot do bpy.context.active_object = some_ob
to make some_ob
the active object. Instead, you have to assign to bpy.context.scene.objects.active
, since every scene has an active object.
You can still change the name of an object by assigning a value to its name
attribute; the fact that that object happens to also be accessible through bpy.context.active_object
does not all of a sudden make its name
or location
attributes read-only.
@Stefan59: it doesn't matter whether you do something in one or two lines. In your case, a
is just another name for that particular object. It's the object itself that determines what you can do with it, not the name you assign to it.
The autoplay just repeats the first video over and over again. it doesn't go to the next one
@raj: never has
Wow Sybren! I can't thank you enough! You have special talent for explaining this kind of stuff! I'm a recovering "script avoiding addict" I'm almost sure this course will cure me :D
Join to leave a comment.