2024年4月30日火曜日

UnityでつけたポーズをBlenderに取り込む方法(もっと簡単な方法あるかも)

Unityのエディタでシーンビュー内でつけたポーズをBlenderにもっていく方法
1. Unity側のスクリプトをポーズを付けたFBXにアタッチしてレストポーズにしたFBXを
referenceModelとして設定する。
ポーズを付けたFBXのrootBoneと出力先のパスを設定する。
2. インスペクター上で右クリックでExportBoneData
3. Blender側のスクリプトでアーマチュアの名前と読み込むポーズのファイルのパスを設定して RunScript


↓Unity側

using System.Collections.Generic;

using UnityEngine;

using System.IO;


[System.Serializable]

public class BoneData

{

    public string name;

    public Vector3 position;

    public Vector3 scale;

    public Quaternion rotation; // This will be Q_relative

}


[System.Serializable]

public class BoneDataList

{

    public List<BoneData> bones = new List<BoneData>();

}


public class BoneDataExporter : MonoBehaviour

{

    public Transform rootBone;

    public GameObject referenceModel; // Add a field for the reference model

    public string boneDataPath = "Assets/boneData.json";



    [ContextMenu("Export Bone Data")]

    void ExportBoneData()

    {

        BoneDataList boneDataList = new BoneDataList();

        CollectBoneData(rootBone, boneDataList.bones, referenceModel.transform);

        string json = JsonUtility.ToJson(boneDataList, true);

        File.WriteAllText(boneDataPath, json);

        Debug.Log("Bone data exported!");

    }


    void CollectBoneData(Transform bone, List<BoneData> boneData, Transform reference)

    {


        // Find the same bone in the reference model by name

        Transform referenceBone = reference.FindChildRecursive(bone.name);

        Quaternion Q0 = referenceBone != null ? referenceBone.localRotation : Quaternion.identity;

        Quaternion Q = bone.localRotation;

        Quaternion Q0_inv= Quaternion.Inverse(Q0);

        Quaternion Q_relative =Q0_inv*Q;

        Quaternion Q_final = new Quaternion(Q_relative.x, -Q_relative.y, -Q_relative.z, Q_relative.w);


        boneData.Add(new BoneData

        {

            name = bone.name,

            position = bone.localPosition,

            scale = bone.localScale,

            rotation = Q_final // Store the relative rotation

        });


        foreach (Transform child in bone)

        {

            CollectBoneData(child, boneData, reference);

        }

    }

}


↓Blender側

import bpy

import json


def apply_pose(armature_name, json_path):

    with open(json_path, 'r') as f:

        data = json.load(f)


    armature = bpy.data.objects[armature_name]

    bpy.context.view_layer.objects.active = armature

    bpy.ops.object.mode_set(mode='POSE')


    for bone_data in data['bones']:

        bone = armature.pose.bones.get(bone_data['name'])

        if bone:

            bone.location = (bone_data['position']['x'], bone_data['position']['y'], bone_data['position']['z'])

            bone.scale = (bone_data['scale']['x'], bone_data['scale']['y'], bone_data['scale']['z'])

            # 注意:四元数は (w, x, y, z) の順で設定

            bone.rotation_quaternion = (bone_data['rotation']['w'], bone_data['rotation']['x'], bone_data['rotation']['y'], bone_data['rotation']['z'])


    # Save the pose

    bpy.ops.object.posemode_toggle()

    bpy.ops.wm.save_mainfile()


# ファイルパスは適宜設定してください


apply_pose('RobinArmature', 'C:boneData.json')

    





2024年4月22日月曜日

Efficient Use of Blender Assets

-How to bake actions in Blender

http://eizouasobi.blogspot.com/2024/04/how-to-bake-actions-in-blender.html


-How to split actions in Blender

http://eizouasobi.blogspot.com/2024/04/how-to-split-actions-in-blender.html


-How to use in game engine

https://eizouasobi.blogspot.com/2019/01/how-to-use-in-game-engine.html

-How to change the scale
http://eizouasobi.blogspot.com/2019/01/how-to-change-scale.html

Formatting Animation Name Files for Use with ActionMaking.py

If the animation number files attached to your assets are not in the correct format for use with the ActionMaking.py script, please use the FormatAnimationNamesAnimColonNum.py script to adjust them.


This script performs the following tasks:

-Removes empty lines and trailing colons: This step cleans up unnecessary formatting that may interfere with processing.

-Replaces spaces with colons: This adjustment is made for lines where the frame range and animation name are separated by spaces instead of colons, ensuring consistency in data format.

-Checks and adjusts the position of name and frame data: This part verifies whether the line format is 'frames:name' or 'name:frames' and adjusts accordingly.

-Processes frame data: Depending on the number of listed frame ranges, the script selects the appropriate frames (the middle ones if there are four frames, the first two if there are three).

For example, a file with lines like:

50-80-110-140:WalkForward:

IdleToTrot:3850-3880-3910

will be changed to:

WalkForward:80-110

IdleToTrot:3850-3880

If there are any extraneous comments or notes that cannot be adjusted by this script, please remove them before running the script.

2024年4月20日土曜日

How to split actions in Blender

One of the ways to further utilize animation assets in Blender is to split actions. Here is a step-by-step guide on how to split actions.


Step 1: Baking the Animation

First, bake the animation in Blender. This involves solidifying the animation data and making any necessary adjustments such as removing root motion. For detailed instructions, please refer to this link.


Step 2: Preparing a Text File for Action Splitting

Prepare a text file in the following format to split actions:

WalkForward:10-40

WalkRight:50-80

If the format of the animation names in the text file differs (e.g., "10-40 WalkForward"), please adjust the format using the script provided at this link.


Step 3: Using the ActionMakingV5.py Script

Open the  script from Blender’s text editor. Set the animation_data to text prepared in Step 2, and set the strip.name to the name of the strip you want to split.

Step 4: Running the Script

Select the target armature and run the Run Script. This will appropriately split the specified action based on the text, making each action available as a separate item.

ActionMakingV5.py

import bpy


def split_action_strip_manual_steps(animation_data):

    """

    Splits an NLA strip named "Action" into multiple strips by following the exact manual steps:

    1. Select frame range

    2. Bake with VisualKeying=True and OnlySelectedBones=False

    3. Push Down Action to create a new strip

    4. Rename the strip

    

    Makes sure to prioritize the Action strip by moving its track to the top each iteration.

    

    Args:

        animation_data: A string of animation segments in format "Name:start-end"

                        with each segment on a new line

    """

    # Parse the animation data

    animations = []

    for line in animation_data.strip().split('\n'):

        if line.strip():

            name, frame_range = line.split(':')

            start, end = map(int, frame_range.split('-'))

            animations.append((name, start, end))

    

    # Sort animations by start frame

    animations.sort(key=lambda x: x[1])

    

    # Get the active object (should be an armature)

    obj = bpy.context.active_object

    if not obj or obj.type != 'ARMATURE':

        raise Exception("Please select an armature object")

    

    # Make sure we have animation data

    if not obj.animation_data:

        raise Exception("The selected armature has no animation data")

    

    # Find the action strip in the NLA editor

    action_strip = None

    action_track = None

    

    for track in obj.animation_data.nla_tracks:

        for strip in track.strips:

            if strip.name == "Action":

                action_strip = strip

                action_track = track

                break

        if action_strip:

            break

    

    if not action_strip:

        raise Exception("Could not find a strip named 'Action' in the NLA editor")

    

    # Store the original action

    original_action = action_strip.action

    if not original_action:

        raise Exception("The Action strip does not have an action assigned")

    

    # Store original frame settings

    original_frame = bpy.context.scene.frame_current

    

    # Clear any active action

    stored_action = obj.animation_data.action

    obj.animation_data.action = None

    

    # Store the original strip settings to restore later

    original_strip_start = action_strip.frame_start

    original_strip_end = action_strip.frame_end

    original_action_start = action_strip.action_frame_start

    original_action_end = action_strip.action_frame_end

    

    # Find the NLA Editor area (we'll need this for several operations)

    nla_editor = None

    for area in bpy.context.screen.areas:

        if area.type == 'NLA_EDITOR':

            nla_editor = area

            break

    

    if not nla_editor:

        raise Exception("Please open an NLA Editor window")

    

    # Create a context override to operate in the NLA editor

    override = {'area': nla_editor, 'region': nla_editor.regions[-1]}

    

    # Create a list to store the created strips for cleanup

    created_strips = []

    

    # Process each animation segment

    for i, (name, start, end) in enumerate(animations):

        print(f"\nProcessing {name} ({start}-{end})...")

        

        # IMPORTANT: Move the original action track to the top of the stack

        # This ensures it has priority in the animation evaluation

        if i > 0:  # We don't need to do this for the first iteration

            # Get the current index of the action track

            action_track_index = list(obj.animation_data.nla_tracks).index(action_track)

            

            # Move the action track to the top

            for _ in range(action_track_index):

                with bpy.context.temp_override(**override):

                    action_track.select = True

                    bpy.ops.nla.tracks_move(direction='UP')

        

        # STEP 1: Select frame range by modifying the action strip

        action_strip.frame_start = start

        action_strip.frame_end = end

        action_strip.action_frame_start = start

        action_strip.action_frame_end = end

        

        # Make sure we're in pose mode

        if obj.mode != 'POSE':

            bpy.ops.object.mode_set(mode='POSE')

        

        # Set the current frame to the start of the range

        bpy.context.scene.frame_set(start)

        

        # STEP 2: Bake the current action strip

        

        # Make sure only the action strip is mutable, mute all other strips

        # This ensures only our target animation is evaluated during baking

        for track in obj.animation_data.nla_tracks:

            for strip in track.strips:

                if strip != action_strip:

                    strip.mute = True

        

        # Deselect all strips

        with bpy.context.temp_override(**override):

            bpy.ops.nla.select_all(action='DESELECT')

        

        # Select our action strip

        action_strip.select = True

        

        # Make sure the NLA is enabled for evaluation

        obj.animation_data.use_nla = True

        

        # Bake the animation with the specified settings

        with bpy.context.temp_override(**override):

            bpy.ops.nla.bake(

                frame_start=start,

                frame_end=end,

                step=1,

                only_selected=False,

                visual_keying=True,

                clear_constraints=False,

                clear_parents=False,

                use_current_action=True,

                bake_types={'POSE'}

            )

        

        # STEP 3: Push Down Action to create a strip

        # At this point, the bake operation would have created a new active action

        baked_action = obj.animation_data.action

        if not baked_action:

            raise Exception(f"Baking failed for {name}, no action was created")

        

        # Rename the baked action to match our segment name

        baked_action.name = name

        

        # Push down the action to create a strip

        with bpy.context.temp_override(**override):

            bpy.ops.nla.actionclip_add(action=baked_action.name)

        

        # Find the newly created strip (should be the latest one added)

        new_track = None

        new_strip = None

        

        # Look for the new strip in all tracks

        for track in obj.animation_data.nla_tracks:

            for strip in track.strips:

                if strip.action == baked_action and strip != action_strip:

                    new_track = track

                    new_strip = strip

                    break

            if new_strip:

                break

        

        if not new_strip:

            print(f"Warning: Could not find the new strip for {name}, creating it manually")

            # Create a new track and strip manually as a fallback

            new_track = obj.animation_data.nla_tracks.new()

            new_track.name = name

            new_strip = new_track.strips.new(name=name, start=start, action=baked_action)

            new_strip.frame_start = start

            new_strip.frame_end = end

        

        # STEP 4: Rename the strip

        new_strip.name = name

        if new_track:

            new_track.name = "SplitActions"

        

        # Add to our list of created strips

        created_strips.append((new_track, new_strip))

        

        print(f"Created strip: {new_strip.name} with action: {baked_action.name}")

        

        # Reset for the next iteration

        obj.animation_data.action = None

        

        # Restore the original action strip settings for the next iteration

        action_strip.frame_start = original_strip_start

        action_strip.frame_end = original_strip_end

        action_strip.action_frame_start = original_action_start

        action_strip.action_frame_end = original_action_end

        

        # Unmute the strip we just created

        new_strip.mute = False

        

        # Mute all other strips except the action strip for the next iteration

        action_strip.mute = False

    

    # Cleanup - unmute all strips

    for track in obj.animation_data.nla_tracks:

        for strip in track.strips:

            strip.mute = False

    

    # Restore the original frame

    bpy.context.scene.frame_set(original_frame)

    

    # Restore original active action if there was one

    if stored_action:

        obj.animation_data.action = stored_action

    

    print("\nAnimation split complete!")

    print("Created action strips:")

    for anim in animations:

        print(f"- {anim[0]} (frames {anim[1]}-{anim[2]})")

    

    return True


# Example usage

animation_data = """Idle1:10-30

IdleToSoar:40-75

FlapForward:100-130

GlideForward:140-170

GlideRight:180-210

GlideLeft:220-250"""


# Run the function

try:

    split_action_strip_manual_steps(animation_data)

except Exception as e:

    print(f"Error: {str(e)}")

How to bake actions in Blender

1.Select all bones in Pose Mode.

2.Go to the menu in the 3D Viewport and select Pose -> Animation -> Bake Action.

3.Ensure these five options are set before clicking OK:

-Set the Start Frame.

-Set the End Frame.

-Check the box for Visual Keying.

-Check the box for Clear Constraints.

-For Bake Data, select Pose.

This should consolidate everything into a single action.






2024年4月1日月曜日

Avian Adventure MR

Avian Adventure MR is an MR action game consisting of four stages.  You can control a bird flying around a real room to collect fruits and coins.

Overview:

 In the entrance scene, you can select a stage and start each stage by clicking the start button. The game begins when you press the grip and trigger buttons at the same time, and a bird comes out of the cage. You can take off with the left controller's Y button and control it with the left and right thumbsticks. Scores are added when you collect lemons growing from the walls of the real room or coins in the air. Touching enemy that appear along the way will cause damage and reduce HP, and the game is over when HP reaches zero. The game clears after one minute, and the score you earned is recorded. The top ten rankings are displayed in the entrance.


Detailed Instructions and Precautions:

If the logo matching the shape of the room does not appear in the entrance scene, the room model setup is incomplete. Please close the app and run the room scan in the settings. If you do not know how to set it up, you can also scan the room model by running the Quest 3's First Encounter app.

-In the entrance, you can reset the position of the menu panel using the start button on the left controller.

-During the game, pressing the start button on the left controller will switch the display of the hand menu.

-In the entrance menu and hand menu, you can toggle on and off the music and controller vibration.

-The content of the cube displayed in the entrance changes with each stage clear. This cube can be moved by gripping the corner with the controller and its size can be changed.

Avian Adventure MR privacy policy

 1. Data Collection Explanation:

Our app does not collect any personal data from users. We respect your privacy and ensure that no personal information is gathered.


2. Data Usage Explanation:

Since our app does not collect any personal data, we do not use any user data for any purpose.


3. Data Deletion Policy:

As no personal data is collected or stored by our app, there is no need for a data deletion request.


4. Data Protection Compliance:

Our organization and app comply with data protection regulations. We do not collect or store any user data, thereby eliminating the risk of security vulnerabilities related to data privacy.