Garry's Mod

Garry's Mod

67 ratings
Add Beautiful Hair Physics to Ragdolls !!!
By 胡杨E
Add beautiful hair physics to ragdolls !!!
2
   
Award
Favorite
Favorited
Unfavorite
Introduction
Check this addon

If you have NPCs with long hair, you will find their hair being solid after they die and become ragdoll.

This tutorial will show you how to make their hair soft and realistic for any NPC/PM models you like!

A - Prepare the Files
There are many words in this tutorial, so be patient
不翻了,凑合看吧


You will need:
Crowbar[github.com]
Blender[www.blender.org]
Blender source tools[steamreview.org]

A1. Get the addon file
Open the page of the addon you want to modify, in this case, I use sharkry for an example, this model has not only a ponytail, but also a hair ribbon, and two robotic arms, great for demonstration.

-
Once you open the workshop page, in the address bar on top you will find a number "3029152157",
so in the SteamLibrary\steamapps\workshop\content\4000\3029152157
you will find this addon's file in the format of ".gma".

Drag the ".gma" file you found in the folder onto "gmad.exe" located in
"SteamLibrary\steamapps\common\GarrysMod\bin",
then the ".gma" file will be unzipped into a folder containing everything in this addon.
In this case, "gfl2_sharkry" folder

If you can't get the folder by dragging onto "gmad.exe", just google how to open .gma file. There are thousands of ways to achieve this.

-
Once you are done, go into the "gfl2_sharkry" folder and you will see stuff like this:

-
In the
models/npc (This stores NPC model),
or models/player (This stores PM model),
or anything like models/balabala (Creators may name the file anything they want),
you will find a bunch of files ends with ".mdl", ".phy", ".vtx", ......
Each ".mdl" file represents a model file, in this case, there are two model files in the folder.

-
The whole file path should look like this:

-
You now successfully extracted Gmod's addons



A2. Decompile the addon file

I recommend you to create four folders on your desktop, for convinience
they are "origin", "decompile", "modify", "compile"

-
Now copy everything in the folder "models/npc" (".mdl", ".phy", ".vtx", ......) into "desktop/origin".

-
Then open Crowbar, in "Set Up Games" tab, change everything like this:

-
Then in the "Decompile" tab, set the file path like below, and then press "Decompile"

-
In this case, you will get two folders in the "decompile" folder, representing two models from this addon, open one of them, and you will see a bunch of stuff (".smd", ".qc" file) like this:

-
Now you have every file you need.
B - Modify the model
B1. Import the model into blender
Open blender, install the Blender Source Tools addon.
Then go into File/Import/Source Engine, choose the ".qc" file in the "decompile" folder, then import.

-
Then you will get this:

-
Hide everthing except for "_physics" and "_skeleton", you will see the physics model look like this:

-
B2. Modifying the physics mesh
The purple mesh (you may see other color in your case) is the Physics Model for this NPC,
The many tiny white spheres around the model are the Bones (I call them Bones) for this NPC.

See? there's only physics model for basic body parts (arms, legs, etc), there's no physics model for the stuff like hair, hair ribbon, robotic arms, that's why they won't have physics after character dies and becomes ragdoll.

Now we need to add additional physics mesh to these stuff.

Use whatever method you know about modeling, add the missing mesh to the gap between tiny white spheres.


Be Aware !!!!!!!!!!!!!!

Cross Section of the mesh you added shouldn't be smaller than these tiny white spheres, otherwise this mesh will be considered too small for the game, and it will get deleted!

Also! Game only supports 32 physics meshes at most ! Each separated mesh in "_physics" is one, count your numbers when you are adding !!!!!!!!!!!!!!!!!!

-
B3. Assign bones to the mesh you just added
Once you are done with modeling, select "_skeleton", in the tab below, click "show names", this will show the names of all the bones this model has.

-
Select "_physics", in the tab below, in the "Vertex Groups", let's assign them with the newly added mesh.

Zoom in to the mesh you just added, select one mesh, see what bone it should be assigned to.
In the picture's case, the mesh should be assigned to "pt1" (Not "pt2", because "pt1" is closer to the body).

-
Same with other meshes you just added.


-
Then you are done!

B4. Export the "_physics.smd" file

Select the "_physics", in the tab below, In "Source Engine Export",
change "Export Format" to SMD,
change "Export Path" to desktop/modify folder,
then press "Export",
choose "_physics.smd" in the popping up tab

-
Then you will get the modified physics model file in your "modify" folder!

Use it to replace the old one in "decompile" file

C - Modify the qc file
You made changes to the physics model, now you need to let the game know what to do with your newly added changes.

Open the ".qc" file in the "decompile" folder, scroll to the bottom, find "$collisionjoints",
in its brace, add lines like below.

$jointconstrain "pt1" x limit -45 45 0
$jointconstrain "pt1" y limit -45 45 0
$jointconstrain "pt1" z limit -45 45 0

$jointconstrain "pt2" x limit -45 45 0
$jointconstrain "pt2" y limit -45 45 0
$jointconstrain "pt2" z limit -45 45 0

..................


-
How many new meshes you added, then how many new lines you will add to .qc file.
"$jointconstrain "pt1" x limit -45 45 0" means how much degrees this bone "pt1" can rotate around x axis (in this case, from -45° to 45°)
Axis see as below:

-
Basicly, for hair, I recommend:

x limit -45 45 0
y limit -45 45 0
z limit -45 45 0

for other stuff, just use

x limit -360 360 0
y limit -360 360 0
z limit -360 360 0

if you don't want to bother.

After you finish adding lines, save the file and you are done.
D - Compile everything back
After you are done with step B and C, (which means your "decompile" folder will now have the modified "_physics.smd" file and ".qc" file)

Go to Crowbar's "Compile" folder,
change the path like below,
change "Game that has the model compiler" to "Garry's Mod",
then press "Compile"

-
After finishing, the "compile" folder will now have the compiled files, in the format of ".mdl" ".phy" ".vtx" and so on.

If you can't see anything in the folder, then there must be an error in the ".qc" file, the text box in Crowbar will show you where the error occurs.

-
Use them to replace the original files in the addon folder, which is
SteamLibrary\steamapps\workshop\content\4000\3029152157\gfl2_sharkry\models\npc

-
Now you have the fully modified addon folder "gfl2_sharkry"!

Move the folder of "gfl2_sharkry" into
SteamLibrary\steamapps\common\GarrysMod\garrysmod\addons

Now you can disable or unsubscribe the original addon if you want. Because you now already have the files in "addons" folder.

The "addons" folder should look like this:

-
Then everthing is done!

Get in the game, murder somebody, you will see the change.

Final word
This is a demonstration for changing an NPC model, you can also change PM models, in this case,

SteamLibrary\steamapps\workshop\content\4000\3029152157\gfl2_sharkry\models\npc
is NPC model

SteamLibrary\steamapps\workshop\content\4000\3029152157\gfl2_sharkry\models\player
is PM model

(folder's name might not be "models\npc" or "models\player", it may be models\something else, but no matter what folder name it is, if it's really a NPC/PM addon, then in the "models" folder you will always find ".mdl" file which you need to modify.)

They all share the same file format, and steps are also the same.


If you find your model twitching on death:
It's due to improper physics collision between your newly added bones and the original bones.
You may try these two methods to fix:

Method 1:
In your .qc file, in the group of "$collisionjoints"
Add this new line in your bones:

$jointcollide "ValveBiped.Bip01_Spine4" "your_bone_name"

for each bone you will get something like:

$jointcollide "ValveBiped.Bip01_Spine4" "hair_back_right22"
$jointconstrain "hair_back_right22" x limit -360 360 0
$jointconstrain "hair_back_right22" y limit -360 360 0
$jointconstrain "hair_back_right22" z limit -90 90 0

Now this bone of "hair_back_right22" will Only collide with "ValveBiped.Bip01_Spine4", which is the main body, it won't collide with arms and head any more (This is a drawback, the hair will just phase through arms). This should fix a lot twitch.

You can also make collision meshes smaller in size in Blender, cause bigger size, bigger chance of clipping and twitching.( a big big mesh will certainly Destroy the physics! you can refer to my GFL2 addons, I consider the meshes inside are in a fine size. )

Method 2 (Only for Hair physics):
If you are adding "Hair" physics rather than "Tail" or anything else, then this one may work for you.
In my GFL2 addon, I made a script aiming to fix the collision between hair and body, you can download this addon and find the script in
lua\autorun\server\00_death_collision_optimized.lua
Put this into your
SteamLibrary\steamapps\common\GarrysMod\garrysmod\lua\autorun\server

I provided two console command in the script.
Type "gf_death_collision_optimized_enable 1" to enable this script.
Type "gf_death_collision_optimized_universal 1" to make this script work for your model.

What this script does is very simple:
When NPC dies, disable collision of some bones (because they are too close to the hair and may clip with hair), then apply a small force to hair, separating the hair from other body parts, so that there won't be any collision (hopefully) between hair and body at the moment of NPC dying. Then, after all these are done, re-enable the collision of bone back to normal.

You won't make any changes to your model for this script to work, it can recognize what bone is hair and what bone is not, as long as your newly added bone looks like hair.

This script is for NPC models! not PM models, because PM models have a total different and wired system, I still couldn't find a way to script with PM models.

For PM models, just use Method 1


It's hard to type this much, but will be worthy if you find this tutorial useful
Leave a likeeeeeee
14 Comments
ha7le1gh 14 Feb @ 12:00pm 
2025
乃ㄖ乃爪卂匚 Ü 8 Jan @ 1:12am 
Definitely going to try this in my next model ports..
ModdedFries 5 Jan @ 8:48pm 
Sweet! This actually worked! Going to experiment some more with this. Thank you for the tutorial, you will be rewarded!
zarawig 16 Dec, 2024 @ 2:47am 
太厉害了!求出一期眼镜、鞋子之类可以脱落的自定义布娃娃blender教程!期待!
JERRY 25 Nov, 2024 @ 2:50am 
感谢大佬分享:steamthumbsup:,但对于刚接触3D软件的人来说添加骨骼还是有些吃力,不知大佬能否出一期教程视频,让我们详细了解一下绑骨过程
Raptor_Killer 01 21 May, 2024 @ 10:30am 
Thank you for your advice
胡杨E  [author] 21 May, 2024 @ 10:12am 
Raptor_Killer 01

Hey I checked your addon and actually didn't see much twitching in game...
Still, I found something wired in model file.

In the "aeon_physics" mesh, there's a mesh assigned to "Tail 1", and there's another duplicated mesh assigned to "ValveBiped.Bip01_Pelvis", these two meshes are sharing same position and clipping with each other.

Although two same meshes are clipping, this actually isn't the problem, because "ValveBiped.Bip01_Pelvis" is the [Parent Bone] of "Tail1" , there won't be any collision between [Parent Bone] and [Child Bone] , so these two are actually fine with other.
胡杨E  [author] 21 May, 2024 @ 10:12am 
However, they won't be fine with Thigh mesh.

"ValveBiped.Bip01_Pelvis" is the [Parent Bone] of "ValveBiped.Bip01_L/R_Thigh", so Pelvis mesh won't have collision problem with Thighs, But "Tail 1" will. Due to its giant mesh which was duplicated from Pelvis mesh, it may collide fiercely with Thighs.
Just go to blender, select the "Tail 1" in pose mode, and rotate it and you will know the problem.

I recommend either delete the "Tail 1" mesh or make it as small as other tail bones.
Raptor_Killer 01 21 May, 2024 @ 7:47am 
@胡杨E

Thank you so much for your response.
I didn't even know that §jointcollide was even a thing, i will experiment with that...
About the size of the collision joints, yeah, i actually thought about that and made them a lot smaller soon after the modification because i knew that they would have caused problems.

Oh, one more thing... This is my fault since i forgot to specify it in my last comment, sorry for that...
I didn't made HAIR...
I followed the steps in your guide to make a Tail, which is obviously attached to the Pelvis (and that's why probably i'm having these problems)

Here's the playermodel, there's a gif in the addon page where you can see what i'm trying to achieve.
https://sp.zhabite.com/sharedfiles/filedetails/?id=2713075077
胡杨E  [author] 19 May, 2024 @ 7:36am 
@Raptor_Killer 01

There's a command I provided, " gf_death_collision_optimized_universal 1", type this and my script to optimize hair physics will now work on All NPCs, including yours.
The script file that achieves this is
lua\autorun\server\00_death_collision_optimized.lua

What this script does is very simple:
When NPC dies, disable collision of some bones (because they are too close to the hair and may clip with hair), then apply a small force to hair, separating the hair from other body parts, so that there won't be any collision (hopefully) between hair and body at the moment of NPC dying. Then, after all these are done, re-enable the collision of bone back to normal.

This is for NPC models! not PM models, because PM models have a total different and wired system, I still couldn't find a way to script with PM models.

Still, for PM models, there's another way to optimize hair physics, but it will cost you something: