1
$\begingroup$

I'm a Blender beginner just trying to find my way around, with the end goal of fully automating a number of rather pedestrian 3D manipulation tasks. While experimenting with various Python commands I encountered the following situation:

  • I use the UI to change the scene unit system from the default Blender units to Metric/millimeters (I can also do this in Python). But now the grid units in the 3D view are too small, so they need to be adjusted as well.
  • I go to the Display section of the Properties screen (window?) and I set the Scale to 0.01, which would give me a decently visible grid.
  • The Python code shown in the Info area once I execute this command is: bpy.context.space_data.grid_scale = 0.01
  • But when trying to run this same command in the Python console I find out that the SpaceConsole object (.space_data) does not have any attribute named grid_scale. That is also confirmed by the auto-completion feature, and by the Python API documentation that comes with version 2.79 of Blender, which is what I'm running.

A couple of questions related to this case, and the general exercise of trying to familiarize myself with the Blender environment, which so far presents a unexpectedly steep learning curve when it comes to "getting both feet off the ground" (in this case maneuvering around the UI):

  • How far can I trust the Python commands shown in the Info area while I manipulate objects manually in the 3D view? (This particular one obviously can't be trusted, since it doesn't exist or it's been deprecated.)
  • Is there any other way to use Python to set the grid scale after I change the scene units (or any time I want to)?
  • If there is no other way (anymore), then what workarounds exist to achieve this modest goal? (Saving a file with all the correct scene and grid units and loading that instead, etc.)

I apologize in advance for wasting someone's time with newbie questions, but in this case this discrepancy seems to point to a shortcoming in the product, which I would like to confirm (and I am actually reading the _ manual :))

$\endgroup$

1 Answer 1

3
$\begingroup$

One reason context.space_data in the console isn't working is because of, well, the context. space_data is of the type Space(see here). When you're adjusting the grid scale, the space_data for the screen area 'VIEW_3D'. When you're typing in the console, the screen area is 'CONSOLE', so the 3D view properties aren't available because the context is wrong. Not to mention, getting the space_data that way is read-only.

One way to change the grid scale is this:

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        area.spaces.active.grid_scale = 0.01

Resources: Space, Area

It's also helpful to look at the tooltips when hovering over the button/property, as they provide information as well.

$\endgroup$
3
  • $\begingroup$ Thanks for explaining the issue with the active area, now I understand better what I'm doing (or what I should be doing). $\endgroup$
    – Dan
    Commented Dec 1, 2017 at 18:56
  • $\begingroup$ As far as the code sample is concerned, the "area" object (or the bpy.types.Area class) does not have a member named "active". Even if it did, when running the code from the Python console the active area seems to be CONSOLE, so it won't work either way. It almost looks like I should make the VIEW_3D area active first and then try to find a way to set the grid scale. If I figure out how to do it without going overboard I'll post the code back here, in case someone else might need to do this in the future. Thanks for pointing me in the right direction. $\endgroup$
    – Dan
    Commented Dec 1, 2017 at 19:03
  • $\begingroup$ No, sorry, I forgot to add 'spaces' when I copied it from my editor. Fixed now. $\endgroup$
    – cmomoney
    Commented Dec 1, 2017 at 21:35

You must log in to answer this question.

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