Forum

Create B4W logic nodes by using Python

29 April 2016 23:43 #9177
Video https://www.youtube.com/watch?v=8tELJBf53Kc shows how to add and link Cycle nodes in Blender by using Python. Unfortunately I can't figure out how to do this for B4W logic nodes. Could somebody give me an example?
04 May 2016 11:45 #9223
Hello and welcome to our forum!

Sorry for delayed answer, we were really busy with b4w conference stuff.
Video https://www.youtube.com/watch?v=8tELJBf53Kc shows how to add and link Cycle nodes in Blender by using Python. Unfortunately I can't figure out how to do this for B4W logic nodes. Could somebody give me an example?
Of course you can add b4w logic nodes via Python scripting.
For example the next code snippet will add "move to" node to logic the tree named "B4W_LogicTree":

node = bpy.data.node_groups['B4W_LogicTree'].nodes.new(type='B4W_logic_node')
node.type = 'MOVE_TO'

I can't describe all aspects of the question in one answer. So if you are interested in this topic in general you should take a look at "logic_node_tree.py" to learn about our logic nodes inner structure. This file is a part of our Blender addon.
If you have certain task or question, I will answer more concrete
Blend4Web Team - developer
05 May 2016 15:55 #9265
Thank you Konstantin!

After searching I was able to create a small tree (as a start):

import bpy

scene = bpy.context.scene

scene.render.engine = 'BLEND4WEB'
scene.b4w_use_logic_editor = True
bpy.ops.node.new_node_tree(type='B4WLogicNodeTreeType', name="B4WLogicNodeTree")
scene.b4w_active_logic_node_tree = "B4WLogicNodeTree"

ntree = bpy.data.node_groups['B4WLogicNodeTree']
links=ntree.links

e_point = ntree.nodes.new(type='B4W_logic_node')
e_point.type = 'ENTRYPOINT'

regstore = ntree.nodes.new(type='B4W_logic_node')
regstore.type = 'REGSTORE'
regstore.floats['inp1'].float = 2 [s]regstore.param_number1 = 1[/s]
regstore.location = 160, 100

sw_select = ntree.nodes.new(type='B4W_logic_node')
sw_select.type = 'SWITCH_SELECT'
sw_select.location = 400, 100

move_to = ntree.nodes.new(type='B4W_logic_node')
move_to.type = 'MOVE_TO'
move_to.location = 650, 100

reroute1 = ntree.nodes.new("NodeReroute")
reroute1.location = 600, -100

reroute2 = ntree.nodes.new("NodeReroute")
reroute2.location = 380, -100

links.new(e_point.outputs[0], regstore.inputs[0])
links.new(regstore.outputs[0], sw_select.inputs[0])
links.new(sw_select.outputs[0], move_to.inputs[0])
links.new(sw_select.outputs[2], reroute1.inputs[0])
links.new(reroute1.outputs[0], reroute2.inputs[0])
links.new(reroute2.outputs[0], sw_select.inputs[0])

This is okay, I believe, but how do I select an object (the default cube) in Switch_Select and how do I add a new socket?

By the way, I watched the conference video. Congratulations!
05 May 2016 16:35 #9268

By the way, I watched the conference video. Congratulations!
Thank you! Really glad to hear!
We won't stop on it!

After searching I was able to create a small tree (as a start):
Great job!

This is okay, I believe, but how do I select an object (the default cube) in Switch_Select
In case of not dupli grouped objects it is simple:

node = … #your node
node.objects_paths['id0'].path = 'Cube' #name of the object


Adding a new socket may be tricky. I'll figure out the optimal way and give you an answer a little bit later
Blend4Web Team - developer
06 May 2016 16:46 #9308

and how do I add a new socket?
Hello again!

Adding a new socket to the "Switch Select" will be something like this:

bpy.ops.node.b4w_logic_add_dyn_jump_sock('INVOKE_DEFAULT', node_tree="B4WLogicNodeTree", node="B4WLogicNode.002", sock="id1")
#increment number in sock parameter
Blend4Web Team - developer
09 May 2016 23:03 #9373
Thank you Konstantin.
As far as I understand in my code it should be like
bpy.ops.node.b4w_logic_add_dyn_jump_sock('INVOKE_DEFAULT', node_tree="B4WLogicNodeTree", node="sw_select", sock="id1")
or something but it doesn't give the result I want.
An alternative way is starting with a switch_select node with many empty sockets (manually), but this is not really satisfying. What do you think?
10 May 2016 10:39 #9381

As far as I understand in my code it should be like
bpy.ops.node.b4w_logic_add_dyn_jump_sock('INVOKE_DEFAULT', node_tree="B4WLogicNodeTree", node="sw_select", sock="id1")
or something but it doesn't give the result I want.
The code snippet which I posted before should do the trick in your code example.

bpy.ops.node.b4w_logic_add_dyn_jump_sock('INVOKE_DEFAULT', node_tree="B4WLogicNodeTree", node="B4WLogicNode.002", sock="id1")
#increment number in sock parameter

I've tested it. You need to specify the "node" parameter as "B4WLogicNode.002". This parameter corresponds to the "Name" property of the node. If you don't specify a node name manually it is autogenerated as "B4WLogicNode.i".

This way of adding a new socket is better then adding empty sockets manually, because it reproduces exactly what happens, when you press the button to add a socket in the node.
Blend4Web Team - developer
12 May 2016 15:43 #9455
Hmm. What am I doing wrong? Even when the node is already added manually I can't add a new socket with Python. Could there be other factors then just the code?
12 May 2016 15:45 #9456

Hmm. What am I doing wrong? Even when the node is already added manually I can't add a new socket with Python. Could there be other factors then just the code?
Could you please attach a minimal .blend file saved after using this code snippet, so I can check the issue
Blend4Web Team - developer
12 May 2016 17:34 #9460
I have (#9455).
 
Please register or log in to leave a reply.