Changing View Model

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Kami
Global Moderator
Posts: 263
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Changing View Model

Postby Kami » Tue Feb 13, 2018 5:40 pm

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.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Changing View Model

Postby L'In20Cible » Tue Feb 13, 2018 5:45 pm

Do it in a post_think hook.
User avatar
Kami
Global Moderator
Posts: 263
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Re: Changing View Model

Postby Kami » Tue Feb 13, 2018 5:46 pm

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?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Changing View Model

Postby satoon101 » Tue Feb 13, 2018 8:46 pm

You'll likely need to do something like what Ayuto did in here:
viewtopic.php?f=10&t=1158&p=7542#p7542
Image
User avatar
Kami
Global Moderator
Posts: 263
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Re: Changing View Model

Postby Kami » Thu Feb 15, 2018 3:42 pm

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.
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: Changing View Model

Postby Doldol » Thu Feb 15, 2018 6:45 pm

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!
User avatar
Kami
Global Moderator
Posts: 263
Joined: Wed Aug 15, 2012 1:24 am
Location: Germany

Re: Changing View Model

Postby Kami » Thu Feb 15, 2018 7:46 pm

I think I tried to remake the same plugin as you did :D Thank you for the link I will try the code!
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: Changing View Model

Postby Doldol » Thu Jan 06, 2022 11:51 am

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
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: Changing View Model

Postby Predz » Thu Jan 06, 2022 8:26 pm

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.
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: Changing View Model

Postby Doldol » Tue Jan 18, 2022 7:08 pm

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

Return to “Plugin Development Support”

Who is online

Users browsing this forum: Google [Bot] and 34 guests