mgooding / itwin-render-timeline-unity

Simple example showing 4D animation from an iModel inside Unity
2 stars 0 forks source link

Unclear #1

Open VescanAlexandru opened 1 year ago

VescanAlexandru commented 1 year ago

Hi mgooding, Fantastic example and thank you very much. Can you please offer a guide on how you have extracted the animation.js as you have mentioned : The RenderSchedule.Script controlling the example's 4D animation extracted to JSON.

MichaelBelousov commented 1 year ago

Hi @VescanAlexandru

Here is some example iTwin.js@2.x code that covers both the old and new persistence formats to get the schedule script JSON in iTwin.js:

for iTwin.js@3.x it should be mostly the same, perhaps some minor tweaks

const scheduleScript: RenderSchedule.ModelTimelineProps[] | undefined = await (async () => {
  try {
    for await (const { script } of iModel.query(
      "SELECT ECInstanceId, Script FROM Bis.RenderTimeline LIMIT 1"
    )) {
      if (script) return JSON.parse(script);
    }
  } catch {
    // some iModels' bis schemas won't be up to date, so just ignore errors
  }
  for await (const { id } of iModel.query("SELECT ECInstanceId FROM Bis.DisplayStyle")) {
    const props = iModel.elements.getElementProps<DisplayStyle3dProps>(id);
    if (props?.jsonProperties?.styles?.scheduleScript) {
      return props.jsonProperties.styles.scheduleScript;
    }
  }
  return undefined;
})();

You can of course remove the immediate-invoked function and just use a let variable to assign to instead or returning early from the immediately-invoked-function

Let me know if this doesn't work and I can try to put this code in an iTwin.js@3.x sandbox

VescanAlexandru commented 1 year ago

Thank you very much for your reply. I will give it a try. I will close this thread as there is no issue but there was no way in contacting you.

Thank you very much again

VescanAlexandru commented 1 year ago

Hi Michael,

I will be amazing if you can put the code in a sandbox given the latest's updates in the code. If you can add a download button for the JSON file the same like you have for the MeshExport that will be great. I think this sandbox will be of greater help for other people in the future ,if it will be created. Thank you very much for the brilliant code and the amazing things that iTwin brings to everyone.

MichaelBelousov commented 1 year ago

@VescanAlexandru I will look into converting this into a sandbox, and reply here once done

MichaelBelousov commented 1 year ago

@VescanAlexandru sandboxes do not look like you can load them with your own iModel. So I don't think that's what you want. Do you want a script? You would have to register your own app id and paste it into such a script to run it

VescanAlexandru commented 1 year ago

Providing the script will be lovely if you can provide it. Will create a new new app that will not be a problem. Thank you very much for the quick reply.

MichaelBelousov commented 1 year ago

The script is really just what I posted above, but first authenticating your app and opening the iModel, and then doing:

const text = JSON.stringify(scheduleScript);
const fs = require("fs");
fs.writeFile("animation.json", text);

You can see for example an app here with auth that loads an iModel and does something with it. In this case you'd just be doing that and then doing both code snippets I provided in order. https://github.com/iTwin/itwinjs-core/tree/master/test-apps/export-gltf

I still intend on putting all three together at some point and replying here with a complete sample app, but in case it takes me some time, I am providing this more in-depth explanation if in the meanwhile you have the time to do it.