short Python code on Blender

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

naninuneno
Posts: 26
Joined: Wed Apr 18, 2012 3:31 pm

short Python code on Blender

Postby naninuneno » Tue May 29, 2012 1:18 pm

Hi everyone :)
If we move the cursor above a button or something on Blender, we can see a short and simple code in python. For example :
Python : Scene.frame_current()
so what's the meaning of that? does it mean bpy.ops.Scene?
But, i see on API Doc for Blender 2.62.3 there is no attribute/methode 'frame_current' in bpy.ops.Scene.
I hope some explanations and maybe another examples soi can understand it :)
huge thanks before!

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue May 29, 2012 7:58 pm

Scene.frame_current means, all Scene objects got a property called frame_current (it's not a function!)

first scene in current file:
bpy.data.scenes[0]

currently active scene:
bpy.context.scene

accessing frame_current of active scene:
bpy.context.scene.frame_current


But if you see RenderSettins.filepath, it doesn't mean that there is
bpy.context.rendersettings.filepath

You rather need to have a RenderSettings object, thus you need to know where they are stored, e.g.

bpy.context.scene.render.filepath
I'm sitting, waiting, wishing, building Blender in superstition...

naninuneno
Posts: 26
Joined: Wed Apr 18, 2012 3:31 pm

Postby naninuneno » Wed May 30, 2012 11:19 am

I get it, but i have another question ^^
so, how could you know it will be part of 'context'? i mean there are also bpy.data.scenes or bpy.ops.scene.
and how about SpaceView3D.cursor_location? will it be bpy.context also? or bpy.ops.view3d?
and what i read on some sources in internet, that i should in 3D view with active camera if i want to use bpy.ops.view3d. is that right? what does it mean?
sorry, i have a lot of questions. :oops:
again, thanks a loooooooooooooooooot! :)

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Wed May 30, 2012 5:22 pm

properties are located in bpy.data or bpy.context

bpy.ops allows you to access operators (derived classes from bpy.types.Operator), basically Blender-Python functions.

The problem with operators is, that some require a certain context. If you run a script from text editor or type into pyconsole, the context is SpaceConsole or TextEditor or something. But view3d ops require View3D, the most simple way to run in this context is to hit Spacebar in 3d viewport and type the operator name and run it from there.

Example:
if you wrote and registered your own operator with bl_idname "view3d.do_something", it becomes available as bpy.ops.view3d.do_something()

in Spacebar menu, displayed name equals bl_label
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: No registered users and 2 guests