Thread Tools Display Modes
03-28-15, 07:02 PM   #1
Stompa
A Kobold Labourer
Join Date: Mar 2015
Posts: 1
Simple code to display playermodel

Can anyone give/show/share a simple code for addon to display user playermodel, with animation?
Maybe even with custom npc.

Even better to have few different animation looping.
  Reply With Quote
03-29-15, 12:51 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
This thread here has information on displaying a PlayerModel. As for animating, I use this in DUF in a PlayerModel OnUpdate script to display/animate for a unit being either dead, AFK or released as a ghost (.5 alpha running).

Code:
if (UnitIsDead(self.unit)) then
	DUF_AnimatePortrait(self, elapsed, self.deathseq, 1)
elseif (self.unit == "player" and UnitIsAFK(self.unit)) then
	DUF_AnimatePortrait(self, elapsed, 96, 1);
elseif (UnitIsGhost(self.unit)) then	
	DUF_AnimatePortrait(self, elapsed, 5, .5)
end
DUF_AnimatePortrait looks like this:

Code:
local function DUF_AnimatePortrait(self, elapsed, sequence, alpha)
	self.seqtime = self.seqtime + (elapsed * 1000)
	self:SetAlpha(alpha)
	self:SetSequenceTime(sequence, self.seqtime)
end
The reason for using self.deathseq is that for some reason, the sequence for a Draenai is 121 where as for all other races it is 1.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
03-29-15, 05:15 PM   #3
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Lua Code:
  1. local AddonName, Addon = ...
  2.  
  3. local format, math, pi, halfpi = format, math, math.pi, math.pi / 2
  4.  
  5. local frame = CreateFrame("Frame", nil, UIParent)
  6. frame:SetPoint("Center")
  7. frame:SetSize(512, 512)
  8. frame:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", insets = {left = 0, top = 0, right = 0, bottom = 0}, tile = true,})
  9. frame:SetBackdropColor(0, 0.4, 0, 1)
  10. frame:SetMovable(false)
  11.  
  12. local model = CreateFrame("PlayerModel", nil, frame)
  13. model:SetModel("Creature\\Alexstrasza\\LadyAlexstrasa.m2")
  14. model:SetAllPoints(frame)
  15. model:SetSize(512, 512)
  16. model:SetMovable(false)
  17. model:EnableMouse(true)
  18. model:EnableMouseWheel(true)
  19.  
  20. local animation = CreateFrame("Frame")
  21.  
  22. local event = CreateFrame("Frame")
  23. event:RegisterEvent("ADDON_LOADED")
  24.  
  25. event:SetScript("OnEvent", function(self, event, ...)
  26.     model[event](model, ...)
  27. end)
  28.  
  29. function model:ADDON_LOADED(addon)
  30.     if addon == AddonName then
  31.         self:Initialize()
  32.         event:UnregisterEvent("ADDON_LOADED")
  33.     end
  34. end
  35.  
  36. function model:Initialize()
  37.     self:SetCustomCamera(1)
  38.     -- This reset doesn't seems to work properly for some reason
  39.     self:Reset()
  40. end
  41.  
  42. local function OnDragStart(self)
  43.     self:SetMovable(true)
  44.     self:StartMoving()
  45. end
  46.  
  47. local function OnDragStop(self)
  48.     self:StopMovingOrSizing()
  49.     self:SetMovable(false)
  50.     local x = math.floor(self:GetLeft() + (self:GetWidth() - UIParent:GetWidth()) / 2 + 0.5)
  51.     local y = math.floor(self:GetTop() - (self:GetHeight() + UIParent:GetHeight()) / 2 + 0.5)
  52.     self:ClearAllPoints()
  53.     self:SetPoint("Center", x, y)
  54. end
  55.  
  56. local function OnUpdate(self, elapsed)
  57.     local x, y = GetCursorPosition()
  58.     local pitch = model.pitch + (y - self.y) * pi / 256
  59.     local limit = false
  60.     if pitch > halfpi - 0.05 or pitch < - halfpi + 0.05 then
  61.         limit = true
  62.     end
  63.     if limit then
  64.         local rotation = format("%.0f", math.abs(math.deg(((x - self.x) / 64 + self:GetFacing())) % 360))
  65.         if rotation ~= format("%.0f", math.abs(math.deg(self:GetFacing()) % 360)) then
  66.             self:SetRotation(math.rad(rotation))
  67.         end
  68.     else
  69.         local yaw = self.yaw + (x - self.x) * pi / 256
  70.         self:SetOrientation(self.distance, yaw, pitch)
  71.     end
  72.     self.x, self.y = x, y
  73. end
  74.  
  75. local function RightButtonOnUpdate(self, elapsed)
  76.     local x, y = GetCursorPosition()
  77.     local px, py, pz = self:GetPosition()
  78.     if IsAltKeyDown() then
  79.         local mx = format("%.2f", (px + (y - self.y) / 64))
  80.         if format("%.2f", px) ~= mx then
  81.             self:SetPosition(mx, py, pz)
  82.         end
  83.     else
  84.         local my = format("%.2f", (py + (x - self.x) / 64))
  85.         local mz = format("%.2f", (pz + (y - self.y) / 64))
  86.         if format("%.2f", py) ~= my or format("%.2f", pz) ~= mz then
  87.             self:SetPosition(px, my, mz)
  88.         end
  89.     end
  90.     self.x, self.y = x, y
  91. end
  92.  
  93. local function MiddleButtonOnUpdate(self, elapsed)
  94.     local x, y = GetCursorPosition()
  95.     local rotation = format("%.0f", math.abs(math.deg(((x - self.x) / 84 + self:GetFacing())) % 360))
  96.     if rotation ~= format("%.0f", math.abs(math.deg(self:GetFacing()) % 360)) then
  97.         self:SetRotation(math.rad(rotation))
  98.     end
  99.     self.x, self.y = x, y
  100. end
  101.  
  102. local function OnMouseDown(self, button)
  103.     if button == "LeftButton" then
  104.         if IsAltKeyDown() then
  105.             OnDragStart(frame)
  106.         else
  107.             self.x, self.y = GetCursorPosition()
  108.             self:SetScript("OnUpdate", OnUpdate)
  109.         end
  110.     elseif button == "RightButton" then
  111.         self.x, self.y = GetCursorPosition()
  112.         self:SetScript("OnUpdate", RightButtonOnUpdate)
  113.     elseif button == "MiddleButton" then
  114.         if IsAltKeyDown() then
  115.             self:Reset()
  116.         else
  117.             self.x, self.x = GetCursorPosition()
  118.             self:SetScript("OnUpdate", MiddleButtonOnUpdate)
  119.         end
  120.     end
  121. end
  122.  
  123. local function OnMouseUp(self, button)
  124.     OnDragStop(frame)
  125.     if button == "LeftButton" then
  126.         self:SetScript("OnUpdate", nil)
  127.     elseif button == "RightButton" then
  128.         self:SetScript("OnUpdate", nil)
  129.     elseif button == "MiddleButton" then
  130.         self:SetScript("OnUpdate", nil)
  131.     end
  132. end
  133.  
  134. local function OnMouseWheel(self, delta)
  135.     local zoom = 0.1
  136.     if IsControlKeyDown() then
  137.         zoom = 0.5
  138.     elseif IsAltKeyDown() then
  139.         zoom = 1
  140.     end
  141.     local distance = model.distance - delta * zoom
  142.     if distance > 40 then
  143.         distance = 40
  144.     elseif distance < zoom then
  145.         distance = zoom
  146.     end
  147.     self:SetOrientation(distance, model.yaw, model.pitch)
  148. end
  149.  
  150. function model:SetOrientation(distance, yaw, pitch)
  151.     if self:HasCustomCamera() then
  152.         self.distance, self.yaw, self.pitch = distance, yaw, pitch
  153.         local x = distance * math.cos(yaw) * math.cos(pitch)
  154.         local y = distance * math.sin(- yaw) * math.cos(pitch)
  155.         local z = (distance * math.sin(- pitch))
  156.         self:SetCameraPosition(x, y, z)
  157.     end
  158. end
  159.  
  160. function model:Reset(id)
  161.     self:ClearModel()
  162.     if not id then
  163.         local modelfile = self:GetModel()
  164.         self:SetModel(modelfile)
  165.     elseif type(id) == "string" then
  166.         self:SetUnit(id)
  167.     elseif type(id) == "number" then
  168.         elf:SetDisplayInfo(id)
  169.     end
  170.     self:SetRotation(0)
  171.     self:SetPosition(0, 0, 0)
  172.     self:RefreshCamera()
  173.     self:SetCustomCamera(1)
  174.     if self:HasCustomCamera() then
  175.         local x, y, z = self:GetCameraPosition()
  176.         local tx, ty, tz = self:GetCameraTarget()
  177.         -- Optionally tx here
  178.         self:SetCameraTarget(0, ty, tz)
  179.         self:SetOrientation(math.sqrt(x * x + y * y + z * z), - math.atan(y / x), - math.atan(z / x))
  180.     end
  181. end
  182.  
  183. local reset
  184. local elapsed = 0
  185. local function AnimationUpdate(self, elaps)
  186.     elapsed = elapsed + (elaps * 1000)
  187.     if not reset then
  188.         -- Need a force reset here to prevent some bugs
  189.         reset = true
  190.         model:Reset()
  191.     end
  192.     -- Valid animations: -1 to infinity
  193.     model:SetSequenceTime(4, elapsed)
  194. end
  195.  
  196. model:SetScript("OnKeyUp", OnKeyUp)
  197. model:SetScript("OnDragStart", OnDragStart)
  198. model:SetScript("OnDragStop", OnDragStop)
  199. model:SetScript("OnMouseDown", OnMouseDown)
  200. model:SetScript("OnMouseUp", OnMouseUp)
  201. model:SetScript("OnMouseWheel", OnMouseWheel)
  202.  
  203. animation:SetScript("OnUpdate", AnimationUpdate)

Last edited by Resike : 03-29-15 at 05:32 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Simple code to display playermodel


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off