Join blocks programmatically

101 views
Skip to first unread message

aakarsh hajela

unread,
Sep 13, 2023, 1:24:25 AM9/13/23
to Blockly
var parentBlock = Blockly.Block.obtain(Blockly.getMainWorkspace(), 'text_print');
parentBlock.initSvg();
parentBlock.render();

In the above code, I am getting "Property 'obtain' does not exist on type 'typeof Block' "

I am trying to connect blocks programmatically one after the other by  using their previousConnection and nextConnection functions. 

I tried the below code:

renderNewBlock = (type) => {
    const newBlock = Blockly.serialization.blocks.append({
      'type': type,
    },Blockly.getMainWorkspace())
  }

This loads all the blocks I need but without the connections.

Neil Fraser

unread,
Sep 13, 2023, 7:19:07 AM9/13/23
to blo...@googlegroups.com
[Due to the group's spam filter mis-firing, this message may have been duplicated.]

Try instead:
var parentBlock = Blockly.getMainWorkspace().newBlock('parent_name');
parentBlock.initSvg();
parentBlock.render();


According to all the documentation I can find that should work, but rendering spits out an error in the console:

Uncaught TypeError: a[0].getBoundingRectangle is not a function
    getBlocksBoundingBox workspace_svg.ts:1748
    getContentMetrics metrics_manager.ts:207
    getMetrics metrics_manager.ts:405
    resize scrollbar_pair.ts:105
    resizeContents workspace_svg.ts:1012
    doRenders$$module$build$src$core$render_management render_management.ts:115
    triggerQueuedRenders$$module$build$src$core$render_management render_management.ts:76
    render block_svg.ts:1554
    <anonymous> debugger eval code:1


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/513725c2-c924-41db-aaa5-7709dbbcec8an%40googlegroups.com.


--
Neil Fraser, Switzerland
https://neil.fraser.name

Beka Westberg

unread,
Sep 13, 2023, 11:39:14 AM9/13/23
to blo...@googlegroups.com
Hello!

That should be correct, and I can't reproduce any errors with it. If anyone has further reproduction steps we can dig in and debug!

Best wishes,
--Beka

Neil Fraser

unread,
Sep 13, 2023, 12:09:14 PM9/13/23
to blo...@googlegroups.com
Aha, it took some debugging, but it turns out that if one issues this command:
  new Blockly.Block(Blockly.getMainWorkspace(), 'logic_negate');
Blockly becomes irrecoverably bricked.

Beka Westberg

unread,
Sep 13, 2023, 12:27:43 PM9/13/23
to blo...@googlegroups.com
Hello,

It looks like it might be because it's constructing a headless block in a rendered workspace. We recommend using `newBlock` or deserializing from JSON because it handles headless vs rendered.

Is it only breaking for the `logic_negate` block? or all blocks?

Best,
--Beka

aakarsh hajela

unread,
Sep 14, 2023, 12:18:48 AM9/14/23
to Blockly
For me the initSvg() and render() functions where not being recognized so I according to some sites I was using initModel() function. But it was still not working. Even the blocks were not being rendered with this.
Even after specifying the connections, the blocks would appear in the workspace but disconnected

So I used code below through which I was able to get the blocks.
var newblock = Blockly.serialization.blocks.append({
      'type': 'blocktype',
    },Blockly.getMainWorkspace());

I was able to make the connections using the following code:

var parentblock = Blockly.serialization.blocks.append({
      'type': 'parentblock',
    },Blockly.getMainWorkspace());

var childBlock = Blockly.serialization.blocks.append({
      'type': 'childblock',
},Blockly.getMainWorkspace());

parentblock.getInput("inputname").connection.connect(childBlock.previousConnection)


Beka Westberg

unread,
Sep 14, 2023, 11:34:12 AM9/14/23
to blo...@googlegroups.com
Yay glad you found a solution! Thanks for posting back with that =) It really helps future people find the answers to their questions!

Best wishes,
--Beka

Reply all
Reply to author
Forward
0 new messages