neverhood311 / Stop-motion-OBJ

A Blender add-on for importing a sequence of OBJ meshes as frames
GNU General Public License v3.0
687 stars 52 forks source link

Is that possible to create a mesh sequence from Python script, i.e. without loading from a file? #104

Open archibate opened 4 years ago

archibate commented 4 years ago

Is your feature request related to a problem? Please describe. Hi, I'm freshman to both Blender and Stop-motion-OBJ. Recently I'm working on integrating Taichi into Blender. So that people could make use their own physics engines based on Taichi in Blender. And it's usually the case that Taichi + Blender users don't want to import a mesh sequence from disk, but generate a mesh sequence via Taichi instead. So I'd like to add a possibility to create a mesh sequence from Python scripting module.

Describe the solution you'd like Add an API like this:

import bpy
object = bpy.context.object
for frame in range(250):
  x = ... (creating a new mesh)
  object.mesh_sequence_settings.append_mesh(x)

Describe alternatives you've considered We may do them in pure end-user Python script manipulating mesh_sequence_settings members.

Additional context For now I use an external script to generate a series of PLY file for creating empty meshes:

import taichi as ti
import numpy as np

n_frames = 250
output = '/tmp/particles.ply'

for frame in range(n_frames):
    print('generating frame', frame)
    writer = ti.PLYWriter(num_vertices=1)
    writer.add_vertex_pos(np.zeros(1), np.zeros(1), np.zeros(1))
    writer.export_frame(frame, output)

and import them into Blender for creating a mesh sequence of length 250.

neverhood311 commented 4 years ago

That's an interesting idea. I think it's entirely possible.

How would your mesh data be formatted? According to this stack exchange post, Blender can create a mesh from an array of vertex positions (formatted like this: [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)]) and an array of faces (formatted as arrays of vertex indices like this: [[0, 1, 2]]). If the mesh data is formatted like this, I think it would be fairly straightforward to add the desired API.

archibate commented 4 years ago

(formatted like this: [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)]) and an array of faces (formatted as arrays of vertex indices like this: [[0, 1, 2]])

Exactly! I will create the meshes first according to that post, and fed the meshes to the API here.

neverhood311 commented 4 years ago

Ok, I'll see what I can do. I can't promise a timeline, though.

archibate commented 4 years ago

And what's more, can we use the streaming mechanism, to only generate a frame when requested? Something like:

def on_request_frame(frame):
  mesh = ...  # generate a mesh via Taichi
  return mesh

object.mesh_sequence_settings.set_frame_callback(on_request_frame, mode='streaming')
neverhood311 commented 4 years ago

That sounds a little more tricky but still probably possible.

On Thu, Aug 20, 2020 at 8:44 PM 彭于斌 notifications@github.com wrote:

And what's more, can we use the streaming mechanism, to only generate a frame when requested? Something like:

def my_frame_generator(frame): mesh = ... return mesh object.mesh_sequence_settings.hook_streaming(my_frame_generator)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/neverhood311/Stop-motion-OBJ/issues/104#issuecomment-678006793, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDGYARMOSX3H4UIR3MZUALSBXNRXANCNFSM4QGA3BAA .

-- Justin

neverhood311 commented 3 years ago

@archibate Are you still working on TaiChi-Blender integration? I'm finally getting around to some Stop Motion OBJ development. I'm working on a feature that lets you create an empty mesh sequence then add new frames to the sequence on-the-fly. Did you still want to integrate with this addon or have you taken a different route?

archibate commented 3 years ago

@archibate Are you still working on TaiChi-Blender integration? I'm finally getting around to some Stop Motion OBJ development. I'm working on a feature that lets you create an empty mesh sequence then add new frames to the sequence on-the-fly. Did you still want to integrate with this addon or have you taken a different route?

Yes, thank for the great project! I studied your addon source code carefully and succeed to support mesh sequence on my own by hooking frame_change_pre. But I have some trouble when saving the mesh sequence (only the mesh at current frame is saved), though.