Page 1 of 1

Changing View Model

Posted: Tue Feb 13, 2018 5:40 pm
by Kami
I am wondering if there is a good way of changing the view model of a players weapon to something else.
I'm currently testing and I can change the viewmodel, but it flickers a lot and the animations do not match up with the actual shooting and reloading.

I'm using this for testing:

Syntax: Select all

from listeners import OnPlayerRunCommand
from players.entity import Player
from filters.players import PlayerIter
from entities.entity import Entity
from filters.entities import BaseEntityIter
from events import Event
from players.helpers import inthandle_from_userid
from entities.helpers import inthandle_from_index
from listeners import OnTick
from listeners import OnPlayerRunCommand
from engines.precache import Model

model = Model("models/weapons/v_rif_aug.mdl")

player_weapons = {}


@Event('player_say')
def _say(ev):
player = Player.from_userid(int(ev['userid']))
if player.userid not in player_weapons:
player_weapons[player.userid] = {}
weapon = player.active_weapon
player_weapons[player.userid][weapon.classname] = 1
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
if inthandle_from_userid(int(ev['userid'])) == ent.owner_handle:
ent.set_property_short('m_nModelIndex',model.index)

@OnPlayerRunCommand
def _use_command_hook(player,command):
if command.weaponselect:
if Entity(command.weaponselect).classname in player_weapons[player.userid]:
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
weapon_handle = inthandle_from_index(command.weaponselect)
if inthandle_from_userid(player.userid) == ent.owner_handle:
if ent.weapon_handle == weapon_handle:
ent.set_property_short('m_nModelIndex',model.index)



@OnTick
def _on_tick():
for player in PlayerIter('all'):
if player.steamid != 'BOT':
if player.userid not in player_weapons:
player_weapons[player.userid] = {}
if player.active_weapon:
if player.active_weapon.classname in player_weapons[player.userid]:
if player_weapons[player.userid][player.active_weapon.classname] == 1:
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
weapon_handle = inthandle_from_index(player.active_weapon.index)
if inthandle_from_userid(player.userid) == ent.owner_handle:
if ent.weapon_handle == weapon_handle:
ent.set_property_short('m_nModelIndex',model.index)


The "goal" is to be able to download just any skin that changes viewmodel for any weapon and be able to use this on your server without having players to install it.

Re: Changing View Model

Posted: Tue Feb 13, 2018 5:45 pm
by L'In20Cible
Do it in a post_think hook.

Re: Changing View Model

Posted: Tue Feb 13, 2018 5:46 pm
by Kami
Will do, thank you!

Edit:

I tried using EntityPreHook but it seems to give me the same results. A sourcemod plugin I found that works well uses a post hook on post_think.

When I try creating an EntityPostHook for post_think I cannot seem to get the player from the memory Pointer.


Syntax: Select all

@EntityPostHook(EntityCondition.is_player, 'post_think')
def pre_post_think(stack,arg):
player = make_object(Player, stack[0])
if player.steamid != 'BOT':
if player.userid not in player_weapons:
player_weapons[player.userid] = {}
if player.active_weapon:
if player.active_weapon.classname in player_weapons[player.userid]:
if player_weapons[player.userid][player.active_weapon.classname] == 1:
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
weapon_handle = inthandle_from_index(player.active_weapon.index)
if inthandle_from_userid(player.userid) == ent.owner_handle:
if ent.weapon_handle == weapon_handle:
ent.set_property_short('m_nModelIndex',model.index)


Is it not possible to make it a PostHook here as it's already post_think?

Re: Changing View Model

Posted: Tue Feb 13, 2018 8:46 pm
by satoon101
You'll likely need to do something like what Ayuto did in here:
viewtopic.php?f=10&t=1158&p=7542#p7542

Re: Changing View Model

Posted: Thu Feb 15, 2018 3:42 pm
by Kami
This is what I have now:

Syntax: Select all

from listeners import OnPlayerRunCommand,OnEntityCreated
from players.constants import PlayerButtons
from players.entity import Player
from entities.entity import Entity
from filters.entities import BaseEntityIter
from events import Event
from players.helpers import inthandle_from_userid
from entities.helpers import inthandle_from_index,index_from_inthandle
from entities import BaseEntityHandle
from entities.hooks import EntityPostHook
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
from engines.precache import Model
from memory import make_object
from weapons.entity import Weapon
model = Model("models/weapons/v_pist_deagle.mdl")

player_weapons = {}
ecx_storage = {}


@EntityPreHook(EntityCondition.is_player, 'post_think')
def pre_on_drop_weapon(args):
player = make_object(Player, args[0])
ecx_storage[args.registers.esp.address.address] = player

@EntityPostHook(EntityCondition.is_player, 'post_think')
def pre_post_think(args,ret_value):
player = ecx_storage.pop(args.registers.esp.address.address)
if player.steamid != 'BOT':
if player.userid not in player_weapons:
player_weapons[player.userid] = {}
if player.active_weapon:
if player.active_weapon.classname in player_weapons[player.userid]:
if player_weapons[player.userid][player.active_weapon.classname] == 1:
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
weapon_handle = inthandle_from_index(player.active_weapon.index)
if inthandle_from_userid(player.userid) == ent.owner_handle:
if ent.weapon_handle == weapon_handle:
if not ent.get_property_bool('m_nViewModelIndex'):
ent.set_property_short('m_nModelIndex',model.index)

@Event('player_say')
def _say(ev):
player = Player.from_userid(int(ev['userid']))
if player.userid not in player_weapons:
player_weapons[player.userid] = {}
weapon = player.active_weapon
player_weapons[player.userid][weapon.classname] = 1
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
if inthandle_from_userid(int(ev['userid'])) == ent.owner_handle:
if not ent.get_property_bool('m_nViewModelIndex'):
ent.set_property_short('m_nModelIndex',model.index)

@OnPlayerRunCommand
def _use_command_hook(player,command):
if command.weaponselect:
if Weapon(command.weaponselect).classname in player_weapons[player.userid]:
for entity in BaseEntityIter(class_names='predicted_viewmodel'):
ent = Entity(entity.index)
weapon_handle = inthandle_from_index(command.weaponselect)
if inthandle_from_userid(player.userid) == ent.owner_handle:
if ent.weapon_handle == weapon_handle:
if not ent.get_property_bool('m_nViewModelIndex'):
ent.set_property_short('m_nModelIndex',model.index)


But it still flickers when used. I tried using custom models for the same weapon which does not give better results.

Any ideas why this is happening? The values m_nSequence, m_flPlaybackRate, m_hWeapon and m_flCycle might be useful too, but I could not manage to work them out.

Edit: I think the actual problem is something with m_nSequence, as the original model seems to get through when ever I shoot.

Re: Changing View Model

Posted: Thu Feb 15, 2018 6:45 pm
by Doldol
I've attempted this before and I think you're right, you need to copy at least some of those properties you mentioned from the normal onto your custom model (I figured this out looking at a SM plugin). I don't really remember the details and It was just an experiment for me.

My code got never to the point of being flicker less. But maybe it'll still be able to help you viewtopic.php?f=20&t=703

Good luck!

Re: Changing View Model

Posted: Thu Feb 15, 2018 7:46 pm
by Kami
I think I tried to remake the same plugin as you did :D Thank you for the link I will try the code!

Re: Changing View Model

Posted: Thu Jan 06, 2022 11:51 am
by Doldol
Kami wrote:I think I tried to remake the same plugin as you did :D Thank you for the link I will try the code!

Did you end up figuring this out? I've been recently getting interested in doing something like this again

Re: Changing View Model

Posted: Thu Jan 06, 2022 8:26 pm
by Predz
Somehow missed looking at this for years, lol.

https://github.com/ThomasVieth/SP-Viewmodel-Changer

Dunno if this will still work but here was my approach from years ago.

Re: Changing View Model

Posted: Tue Jan 18, 2022 7:08 pm
by Doldol
Predz wrote:Somehow missed looking at this for years, lol.

https://github.com/ThomasVieth/SP-Viewmodel-Changer

Dunno if this will still work but here was my approach from years ago.


uu cool ty! I'll take a look at it when I find the time