5
$\begingroup$

I am using a Python startup script to add repetitious details to a scene. Arranging the nodes of the network for a new Cycles material is cleaner if I know the size of each node as it is created. The height and width attributes are not correct, but the dimensions attribute is accurate — once it is set up.

What I am looking for is a way to get the dimensions attribute updated right after the node is created. (My current workaround uses a table of node sizes.)

If I run the equivalent of

    import bpy
    mats = bpy.data.materials
    mat = mats['GlossyGreenMaterial']
    nods = mat.node_tree.nodes
    newNode = nods.new('ShaderNodeBsdfGlossy')
    newNode.name = 'Glossy BSDF added'

to set up the node and then immediately execute

    print('newNode.dimensions: ', newNode.dimensions)

it will display

    newNode.dimensions:  <Vector(0.0000, 0.0000)>

If I bring up the Node Editor so the node is drawn and then execute the print statement again it provides the expected

    newNode.dimensions:  <Vector(150.0000, 142.0000)>

Is there some update routine I could call as each node is added that would cause the dimensions attribute to be set?

Thanks,
Roy Earle

$\endgroup$
2
$\begingroup$

There seems to be a solution to this (see below). Keep in mind, though, that while in general this approach is not recommended, in some cases it may work just fine.

Add this call to your script to force a re-draw, which will update the node’s dimensions properly.

bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

Tip: if you add multiple nodes to the hierarchy (via your script) it may be smarter to add all nodes first then call this only once to minimize messing with blender’s drawing loop.

You can read more about this here:

https://docs.blender.org/api/2.79/info_gotcha.html?highlight=redraw#can-i-redraw-during-the-script

To be honest, I don’t understand exactly why blender doesn’t update the node’s dimensions right after it is added to the node hierarchy. It may be just an oversight, or perhaps blender really needs to re-draw everything once before it can figure out the dimensions.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.