#Change coordinate system origin to the center of the grid?

380 messages · Page 1 of 1 (latest)

spark zodiac
#

I am creating a plugin to use the new snapshot feature for assembly entity models. However, I would like to change the coordinate system to have the center of the grid as the origin, rather than the lower left corner.

ripe vector
spark zodiac
#

Neat, that wasn't on the wiki

ripe vector
#

Yeah the types are more up-to-date than the wiki

spark zodiac
#

Is there a guide how to set up a typescript project with these types? JS and web environments have never been my strong suit

ripe vector
#

Yeah you just need to have a NPM project, and then use the command to install the types. Typescript is not even required for this to work if you are using VSCode

#

If you have forked the blockbench-plugins repository and you're working in that, you just need to run npm install to install the types

spark zodiac
#

Let me try

#

No, that doesn't seem to work

sinful flume
sinful flume
spark zodiac
#

Oh, that's new

sinful flume
#

Plugin is a built-in libDOM class that is overwritten by BB, but typescript doesn't like it

spark zodiac
#

Also, how do I reference a different types branch?

#

I have no idea how npm works

#

(Also, do you have a typescript example project, maybe?)

sinful flume
#

Well, you'd need to clone the repo locally, and then reference it in a d.ts file:

/// <reference path="path/to/types/repo">
#

I do actually

spark zodiac
#

A d.ts file?

#

Oh, intermediaries

sinful flume
#

It's a little out of date

sinful flume
spark zodiac
#

Gotcha

sinful flume
#

If you want my branch of bbtypes using that template:

  • Remove the bbtypes dependency npm uninstall blockbench-types.
  • Clone my bbtypes branch somewhere on your file system (NOT in your plugin's repo)
  • Make a file at types/blockbench.d.ts in your plugin repo with the content:
/// <reference path="<path_to_bbtypes_repo>/types/index.d.ts">
  • In the tsconfig.json replace "blockbench-types" with a reference to the above file in the types list:
"types": ["node", "./types/blockbench.d.ts"],
spark zodiac
#

Well then

#

@sinful flume Mind helping me out with a couple more general questions while I feel my way through this?

sinful flume
#

sure

spark zodiac
#

I made this project a long time ago, so most stuff is outdated

#

For instance, why is this not valid anymore

#
const namespace_dialog = new Dialog("export_namespace", {
    title: "SAM Export",
    id: "export_namespace",
    form: { namespace: { label: "Namespace", type: "text" } },
    onConfirm(form) { export_sam(form.namespace); }
})```
#

namespace is apperantly undefined

sinful flume
#

Dialogs have changed completely

spark zodiac
#

Oh great

#

Yeah, the lack of docs is a bit of a problem

#

Right, so here is the idea

#

I need this plugin to take every element group and export it as a item model seperately

#

While also offsetting the groups pivot to the origin

#

How would I approach that?

ripe vector
sinful flume
#

Oh, was that not a change made recently?
I recall it being mentioned

spark zodiac
#

Yeah, dialogues seem to still be the same

#

At least from what I can tell

sinful flume
#

Oh, maybe it was the addition of a new blank slate dialog?

spark zodiac
#

So then how do I circumvent this?

#

I assume this is because the form data is just a object

sinful flume
#

That's a bb type issue

spark zodiac
#

And doesn't explicitly define the form

sinful flume
#

Yeah, object doesn't allow anything except an empty object

#

Make an issue in the bb types and I'll fix it on my branch and Jannis will get to it eventually

spark zodiac
#

A minor issue for now

#

Getting stuff to work is more important now

sinful flume
spark zodiac
#

Kinda

sinful flume
#

Using the new tech in the snapshot

spark zodiac
#

It's for the new snapshots display entities

sinful flume
#

Yeah me to

spark zodiac
#

But I also have a server sided plugin that controls animations and AI

#

So I need to also export certain metadata to generate the resourcepack and be able to load and process it

sinful flume
#

Take a look at the Outliner in bb types

spark zodiac
#

The idea was to just export the individual pieces, name them properly and also export a manifest that serves as the model and animation data

#

I did use to have something before

#

It just seems very broken

#

Like I said, I tried this before very long ago

#
function export_sam(namespace: string) {
    const geoName = Project.geometry_name;
    Blockbench.export({
        name: geoName,
        type: "SAM Model Definition",
        extensions: ["sam"],
        content: autoStringify(sam_codec.compile()),
        savetype: "text"
    }, (p) => {
        var save = PathModule.resolve(PathModule.dirname(p), "parts");
        fs.mkdirSync(save, {recursive: true});
        Project.groups.forEach((group) => {
            var path = PathModule.resolve(save, group.name + ".json");
            Blockbench.writeFile(path, {
                content: autoStringify(serialize_bone_to_item(group, namespace)),
                savetype: "text"
            }, null);
        });
    });
}```
sinful flume
#

Again, check out the Outliner

#

It contains elements and groups

#

In a tree

spark zodiac
#

Does Project.groups not work anymore?

sinful flume
#

I wouldn't bet on it

#

But it might

#

Groups.all

#

Would work better

spark zodiac
#

Ah, sweet

#

So then, is there a way to get and reuse the existing item serializer codec

#
const ITEM_CODEC: Codec = Codecs["java_block"];``` This should give me the item codec, correct?
sinful flume
#

If you open Blockbench and press CTRL + SHIFT + I it will open up the dev tools. Then you can open the console and type Codecs, press ENTER and see for yourself what it contains

spark zodiac
#

Yeah, that's how I got that

#

Just making sure

#

Better question, I noticed that a group doesn't keep its position

#

I need to offset the whole group to the origin, anchored around their pivot

#

So do I have to iterate through each element and subtrack the pivot?

sinful flume
#

Idk it's been a year since I messed with it

#

It's a case of F around and find out

spark zodiac
#

Fair enough

spark zodiac
#

Where is the autoStringify function actually kept?

#

Wait, autoStringify doesn't exist anymore?

spark zodiac
#

ALRIGHT, SO

#

I think I got everything covered

#

@sinful flume Mind looking it over real quick?

sinful flume
#

Global function, takes into account formatting rules that blockbench lets you configure

spark zodiac
#

Oh, is that what it's called now

#

I think this should cover everything

sinful flume
#

It appears to be in order from a quick scan over it

#

It's quite small actually

spark zodiac
#

It is pretty simple

#

Since unlike java models I don't have to generate commands or anything

#

It looks like it didn't add the export option

#

It also looks like it doesn't like the compile function

sinful flume
#

sam_codec.compile!

#

! after a value tells TypeScript that the value will never be undefined during runtime

spark zodiac
#

It still complains about a incompatible type

sinful flume
#

Oh, looks like that property is invalidly typed

spark zodiac
#

excellent

sinful flume
#

This has been my experience with the bbtypes

#

I spend more time fixing them than I do developing my plugin

spark zodiac
#

Well, it compiles without error, so I guess it's fine

#

Even if it's marked

#

Still though, why does the export function not get added to the menu?

#

Ok got it

#

That weird event thing was making issues

#

Aaaand it doesn't do anything

#

Excellent

sinful flume
#

I've pushed a fix on my branch of the types

spark zodiac
#

I haven't actually cloned the local types yet

#

Let me do that right now

#

Hm

sinful flume
#

does that file exist in your project?

spark zodiac
#

Yes, I've created it

#

Do I need to run some sort of re-setup?

sinful flume
#

What's the rest of your tsconfig look like?

spark zodiac
#
    "compilerOptions": {
        "outDir": "./dist/",
        "noImplicitAny": true,
        "module": "es2020",
        "target": "ES2021",
        "jsx": "react",
        "allowJs": true,
        "moduleResolution": "node",
        "strict": true,
        "types": ["node", "./types/blockbench.d.ts"],
        "resolveJsonModule": true
        // "declaration": true,
        // "emitDeclarationOnly": true
    }
}```
sinful flume
#

oh, sorry remove the .d.ts extension

spark zodiac
#

That got rid of it

sinful flume
#

This is what mine looks like

{
    "compilerOptions": {
        "outDir": "./dist/",
        "noImplicitAny": true,
        "module": "es2022",
        "target": "ES2022",
        "moduleResolution": "node",
        "strict": true,
        "types": ["node", "svelte", "three", "./types/blockbench-types"],
        "resolveJsonModule": true,
        "allowSyntheticDefaultImports": true
    }
}
spark zodiac
#

Though it seems like the project didn't get the memo

sinful flume
#

Try saving the files?

#

Sometimes it doesn't update types until you save

spark zodiac
#

Nope

sinful flume
#

I'm curious why your reference path didn't get highlighted

#

OH

#
/// <reference path="D:/github-repos/blockbench-types/types/index.d.ts"/>

I think I forgot a / at the end

spark zodiac
#

Ah, we love xml

sinful flume
spark zodiac
#

Yeah, there we go

sinful flume
#

Excellent

spark zodiac
#

Well, it still doesn't like the export content

#

With or without the !

sinful flume
#

CTRL + click the content property

#

and show me the code it brings up

spark zodiac
sinful flume
#

Ok, do the same with Codec and find the compile method's def

spark zodiac
sinful flume
spark zodiac
#

huh

sinful flume
#

This is what my branch looks like ^

spark zodiac
#

Huh, I just cloned it though

sinful flume
#

From my branch or Jannis'?

spark zodiac
sinful flume
#

How did you initially clone it?

spark zodiac
#

Just git clone

sinful flume
#

Weird

spark zodiac
#

It got the string and array

#

But it didn't remove the any

spark zodiac
#

Well, I can't tell you what's going on

sinful flume
#

Maybe see if deleting the repo and re-cloning it works?

spark zodiac
#

I've just removed it manually for now

sinful flume
#

¯_(ツ)_/¯

spark zodiac
#

It's getting late and I want to get this at least somewhat working

sinful flume
#

oh wait

#

That's codec options

#

Try pulling now

spark zodiac
#

Yeah, there we go

#

Finally, no more errors

sinful flume
spark zodiac
#

Alright, next issue

#

Why is the export button not doing anything

sinful flume
#

We hate red squiggly lines here

#

which export button

spark zodiac
#
    onload() {
        console.log("Loaded!")
        var export_action_sam = new Action("export_sam", {
            name: "Export SAM",
            icon: "archive",
            description: "Export as model archive and definition json.",
            category: "file",
            condition: () => Format.id === "sam",
            click: () => sam_codec.export
        });
    
        MenuBar.addAction(export_action_sam, "file.export");
    }``` The one I add here
#

Huh

#

I was wondering why there is a overload for action

sinful flume
#

I get that with AJ but it doesn't seem to effect it

#

But we're also just directly injecting our actions into the menus

#

very ugly but it works

spark zodiac
#

Well, either way

#

The logic looks to be right

#

Oh wait, I am missing the brackets again

#

It's a runnable, not a function field

#

Yeah, there it goes

#

Though the action is still not doing it

#

It opens the file select dialogue, but that's about it

#

For context, here is the latest state

spark zodiac
#
function export_action() {
    console.log("Exporting pieces.")
    Blockbench.export({
        type: "Server Animatable Model",
        extensions: [ "sam.json", "json" ],
        content: sam_codec.compile!(),
        savetype: "text",
        custom_writer: (a, b) => {}
    }, (p) => {
        console.log("Exporting pieces.");
        export_parts("minecraft", path.dirname(p));
    });
}```
#

So apperantly it calls the function

#

But the export function fails

#

And the consumer for after writing never gets called

#

It also calls the compile function

#

Could it be that the custom writer overwrites it and thus doesn't do anything?`

#

@sinful flume

sinful flume
#

I mean

#

Custom writer supposed to write stuff

spark zodiac
#

I have stubbed it, since I assumed it was a override

#

But it's a required field and doesn't let me leave it empty

sinful flume
#

Oh, whoops

#

wait

spark zodiac
#

Wait no

sinful flume
#

That was fixed in my types

spark zodiac
#

Now I can do it without complaining

#

Yeah

#

lol

sinful flume
#

Yeah bb's default types force it

spark zodiac
#

Yeah, that solved it

#

OK, uh

#

Not exactly pointing towards an error

sinful flume
#

Codec.G is probably your codec. Do you have code minification enabled?

spark zodiac
#

I don't think so, but where would it be configured?

sinful flume
#

What are you using to bundle your ts project

spark zodiac
#

Just yarn build

sinful flume
#

oh

#

use yarn dev

#

You're using my template then?

spark zodiac
#

Indeed

sinful flume
#

FYI It's using ESBuild as it's typescript/javascript bundler

#

yarn dev won't minify code and includes a source map for better dev tool debugging
yarn build is for prod

spark zodiac
#

It claims to be unable to load the map

#

And is also just not finishing

sinful flume
#

yarn dev builds once, then automatically builds on every file change

spark zodiac
sinful flume
#

Yep, that's working as intended

spark zodiac
#

Ah, right

sinful flume
#

You can stop it manually using CTRL + C when you're done deving

sinful flume
#

What's the sam.js.map file look like?

spark zodiac
#

Too long to not be a file

#

But nothing that strikes me as invalid json

sinful flume
#

yeah seems fine

spark zodiac
#

Yeah, idk

#

The issue seems to be in a foreach though

#

And I only have one of those

#
function export_parts(namespace: string, path: string) {
    Group.all.forEach((group) => {
        var pivotOffset: ArrayVector3 = [group.origin[0] * -1, group.origin[1] * -1, group.origin[2] * -1];
        group.forEachChild((element) => {
            var cube = element as Cube;
            cube.moveVector(pivotOffset, 0, false);
        }, Outliner.elements, false);
        Blockbench.writeFile(path + `/${Project?.name}_${group.name}.json`, {
            content: JSON.stringify(group.compile(false)),
            savetype: "text"
        });
    });
}```
#

Two, actually

spark zodiac
#

It's some binary file

#

app.asar

sinful flume
#

sounds about right

#

some dev tools shenanigans

spark zodiac
#

Right

#

Well, still

#

So what is this about

#

Also, it has to be something in here

#
        group.forEachChild((element) => {
            var cube = element as Cube;
            cube.moveVector(pivotOffset, 0, false);
        }, Outliner.elements, false);```
#

Is this not how you define the foreach child?

sinful flume
#

group can have groups as children

spark zodiac
#

But does the second arg not act as a filter of the child type

sinful flume
#
function recurse(el: OutlinerNode) {
  if (el instanceof Group)
    recurse(el)
  else
    cube.moveVector...
}
recurse(group)
sinful flume
spark zodiac
#

Ah.

#

No, that did not solve it

#

Also, the casting doesn't seem to work, since it's still just a object

sinful flume
#

You need to filter the element type. Not case the element to a new type

spark zodiac
#

Yes, I have

#
        group.forEachChild((element) => {
            if(element instanceof Cube) {
                var cube = element as Cube;
                cube.moveVector(pivotOffset, 0, false);
            }
        }, Outliner.elements, false);```
sinful flume
#

Why are you casting it still

spark zodiac
#

Because it has a stroke about moveVector otherwise

sinful flume
#

You shouldn't be, TS should automatically understand that it's now a cube since you're now checking it's class in an if statement

spark zodiac
#

Ah, hm

#

Still no

#

I think this may be a different issue

#

Sure would be handy if I could see the mappings

#

lmao

sinful flume
#

If you remove that code entirely does it still error

spark zodiac
#

Yes, actually

sinful flume
#

Then it's probably not that code

spark zodiac
#

I think it may be what I am passing to it

sinful flume
#

oh

spark zodiac
#

I don't think Outliner.elements is a valid type

sinful flume
#

Outliner.elements is definitely not a class that extends OutlinerNode

#

You'd want to pass Cube in this case

spark zodiac
#

Yep, that was it

#

Excellent

#

Now, the next question

sinful flume
#

I pushed an update to the types that should make that clearer

spark zodiac
#

Compiling a group as a item model

#

I have the item codec already

sinful flume
#

Blockbench dose does this internally, simply figure out how it does that

spark zodiac
#

I would assume I'd need to call compile

#

On the codec

#

Giving the group as an option

#

But the options arg does not actually have a layout

sinful flume
#

compile's options is unique to every codec

#

So it's an any

#

You'll have to check the bb source to find out what java_block's options are

spark zodiac
#

I am not sure

#

I am looking at it right now

#

But the options don't seem to define the input elements or groups at all

#

So what does

sinful flume
#

Probably the global Project

spark zodiac
#

Oh, that's not good

sinful flume
#

Welcome to Blockbench

spark zodiac
#

What, so do I need to reimplement the entire java item codec now

sinful flume
#

yep

spark zodiac
#

Great.

sinful flume
#

That's why I was surprised your code was so small

spark zodiac
#

I guess I can just copy the existing codec

#

And replace all the Project calls with the options

#

The root of a project is a group as well, yes?

sinful flume
#

Outliner.root

#

Kinda

spark zodiac
#

Just type-wise

sinful flume
#

Looks like an array to me

spark zodiac
#

Ah

#

Right, well I can work around that

#

OK, I am going to bed now

#

I have to do this tomorrow

#

Don't have the energy to fix this today anymore

sinful flume
#

I love me some JS to TS conversion

#

.;,,,;.

spark zodiac
#

Well, good night

spark zodiac
#

@sinful flume I think I found another issue

#

I don't believe rotationAxis should not return, should it?

sinful flume
#

Void is an indication of no return value

spark zodiac
#

Indeed

#

but it should return the axis

#

Not nothing

sinful flume
#

Oh, I miss read

#

💀

#

Sorry just waking up

spark zodiac
#

Also, Textures don't include their texture properties like name, id etc

sinful flume
#

If you make issues on the git repo I can more effectively track and fix issues

spark zodiac
#

I'll make some later

#

I am pretty close to finally getting this to work

#

Item exporting and everything is done

sinful flume
#

Nice

spark zodiac
#

But I am running into some strange issues while exporting the elements to the items

#

Exporting this element:

#

Has the following properties, according to the outliner

#

Which seems accurate

#

(it's rotated)

#

However, when trying to access the to and from coordinates from the plugin, they are completely skewed

#

Name: handle | Pivot: 5.000000000000002,11.999999999999996,-1.7763568394002505e-15 | Offset: -5.000000000000002,-11.999999999999996,1.7763568394002505e-15 | From: -14.999999999999996,-35.5,-0.49999999999999867 | To: 10.00000000000002,-34.5,0.5000000000000011

#

Going from from[-6, 11.5, -0.5] to from[-15, -35.5, -0.5]

#

And from to[19, 12.5, 0.5] to to[10, -34.5, 0.5]

#

i.e. the entire element gets offset by [-9, -46, 0]

#

And I am at a loss where that is coming from

#

It's not the pivot point either, that would be [5, 12, 0]

#

So what is happening here?

#

The printout is also happening before any transformation to the element happens, for context

sinful flume
#

No clue

spark zodiac
#

This is kinda breaking everything

#

@ripe vector Do you maybe have any idea what‘s going on here

ripe vector
#

Is it intentional that you are moving the position of the cubes? Best practice for an export function would be to make it read-only and not change anything about the model.. But at least you would need to reset it afterwards.

spark zodiac
#

Yes it is, I am moving all bones to the origin of the grid based on their pivot

ripe vector
#

Do you only handle level one groups, no nesting?

spark zodiac
#

No, I go through every group recursively

#

Very group is a bone that needs to be exported as its own item

#

Oh hang on

#

Is forEachChild recursive?

ripe vector
#

Yeah

spark zodiac
#

Well damn

ripe vector
#

You could use group.chidren.forEach() if you don't want to handle soem cubes twice

spark zodiac
#

Let me try

spark zodiac
#

There seems to be a different issue now

#

Coordinates seem correct now

spark zodiac
#

!close