# How can I get the dimensions of a newly-created material node via Python?

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


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

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.