Page MenuHome

Adding Node Sockets to Node Group from Python crashes Blender
Closed, ArchivedPublic

Description

System Information
Operating system and graphics card

Blender Version
Broken: (example: 2.69.7 4b206af, see splash screen)
Worked: (optional)

Short description of error

Exact steps for others to reproduce the error
Based on a (as simple as possible) attached .blend file with minimum amount of steps

Details

Type
Bug

Event Timeline

Rainer Trummer (aliasguru) raised the priority of this task from to Needs Triage by Developer.
Rainer Trummer (aliasguru) updated the task description. (Show Details)

I'm working on a script that will add some additional input sockets to a node group based on output socket names from another node (input image) in the Compositor. This is the script so far:

def execute(self, context):
    tree = context.scene.node_tree

    _nodes = [x for x in tree.nodes if x.select]

    _group, _source = _nodes[1], _nodes[0]
    _groupInputs = [x.name for x in _group.inputs]

    for _socket in _source.outputs:
        #    check and create the socket
        if not _socket.name in _groupInputs:
            print('socket %s is missing, type is %s' % (_socket.name, _socket.type))
            _group.inputs.new('NodeSocketColor', _socket.name)
            _group.update()

        #    connect the sockets
        tree.links.new(_source.outputs[_socket.name], _group.inputs[_socket.name])

    print('all sockets added')


    return{'FINISHED'}

Now, when I run this script from the Pyhton Interactive Console in Blender, everything seems to work, I can see the resulting node group plus the connections fine. But as soon as I click on ANY node in the Composite node tree, Blender crashes with an 'ACCESS_VIOLATION'. Same thing happens immediately if I add a button to the Node Editor Panel and run my operator from there. BAM!

I guess I miss something, I tried adding the _group.update() function in the loop, but it does not change a thing, same with context.scene.update(). I know I kinda edit stuff in bpy.data here which is never hassle-free, but why the crash happens still isn't clear to me.

I reported this also on Blender StackExchange, but was pointed back to the BugTracker.

Hope the description was understandable, any ideas appreciated!

found this dirty hacky workaround here:

def execute(self, context):

		tree = context.scene.node_tree

		_nodes = [x for x in tree.nodes if x.select]

		_group, _source = _nodes[1], _nodes[0]
		_nodeGroupInputs = [x for x in _group.node_tree.nodes if x.type == 'GROUP_INPUT']
		_groupInputs = [x.name for x in _group.inputs]

		if _nodeGroupInputs:
			for _socket in _source.outputs:
				#    check and create the socket
				if not _socket.name in _groupInputs:

					#    Try to use a reroute as a workaround instead
					_reroute = _group.node_tree.nodes.new('CompositorNodeBrightContrast')
					_reroute.inputs[0].name = _socket.name
					ref = _nodeGroupInputs[0].outputs[-1]
					x = _group.node_tree.links.new(ref, _reroute.inputs[0])

					_group.node_tree.nodes.remove(_reroute)

					_group.update()

				#    connect the sockets
				tree.links.new(_source.outputs[_socket.name], _group.inputs[_socket.name])

		print('all sockets added and group updated')


		return{'FINISHED'}
Bastien Montagne (mont29) lowered the priority of this task from Needs Triage by Developer to Needs Information from User.Jun 11 2016, 4:09 PM

Please attach a whole usable py script, chuncks of operators are painful for us if we have to reput them ourselves in usuable op…

Hi Bastien,

Sorry forgot to do that, I just copied my Question over from Blender.Stackexchange.com, hoping I would have missed some obvious "don't do this!" thing. Anyways, attatched to this message you'll find a demo Blend file. On the left you'll see the Compositor, bottom right I put the script (just run it, it registers the operator), and on the top a console with the command to execute. The nodes in the Compositor are already selected with the NodeGroup last, which is required for the operator to run "correctly".

When you run it, the connections are made and sockets created. If you then click on a node, BOOM. Happens also on Ubuntu 16.04 by the way, Blender 2.77.1

Thanks for looking into it,
Rainer{F316829}

Bastien Montagne (mont29) raised the priority of this task from Needs Information from User to Confirmed, Medium.Jun 12 2016, 5:28 PM

Can confirm the issue, happens in writefile.c, in USE_NODE_COMPAT_CUSTOMNODES part of code. @Lukas Toenne (lukastoenne) should know better I think?

On a side note, running that compat code for undo/redo blendfile writing seems rather useless, maybe this should be handled the same way as mesh compat?

archiving this task, no activity nearly 1 year, tested file in 2.78c buildbot & cannot reproduce the error.
feel free to reopen if I'm wrong here.

@Brendon Murphy (meta-androcto) Indeed this has been silently fixed somewhen, that's good news! Not sure when it has been adressed, but no crashes any longer