playMaker

Author Topic: Help with webcam custom action  (Read 4245 times)

cel

  • Junior Playmaker
  • **
  • Posts: 96
    • View Profile
Help with webcam custom action
« on: May 20, 2012, 11:20:41 PM »
Hi guys,

For the past 2 weeks i've been trying to make a custom action for the webcam using the webcamera texture feature of unity. with help from jean we were able to get it to work within playmaker, I decided to have a set of actions for this, setup camera, get propereties, play. pause and stop...all is well we can setup the camera, play it... but stopping it doesn't work.... sometimes it does stop but then I can't get it to play again...

To try this, add an object to your scene, give it a material, attach the setupcamera (available fromthe camera section, didn't have time to change the category) cameraid should be 0 for your default camera and then use the play action in the webcamera category... if your image is mirrored scale your object to negative values (ex. if your object is 10x10x10 scale it to -10x-10x-10)

Have a look at the code below... it's a mess I know... I'm just trying to learn at the moment... and if you can fix it or come up with a solution please do....

setupcamera:

Code: [Select]
import System;
import HutongGames.PlayMaker;

@ActionCategory(ActionCategory.Camera)
@Tooltip("Sets up the Webcamera")
public class setupWebcamera extends FsmStateAction{

@UIHint(UIHint.Variable)
public var gameobject : FsmOwnerDefault;

@UIHint(UIHint. Variable)
public var cameraId : FsmInt;

@UIHint(UIHint. Variable)
public var width : FsmInt;

@UIHint(UIHint. Variable)
public var height : FsmInt;

@UIHint(UIHint. Variable)
public var framesPerSecond : FsmInt;




public function Reset(){
gameobject = null;
cameraId = null;
width = null;
height = null;
framesPerSecond = null;
}

public function OnEnter(){

var dev : WebCamDevice[];

var deviceName : String;

var wct : WebCamTexture;

var resultString : String;

dev = WebCamTexture.devices;

       deviceName = dev[cameraId.Value].name;

       wct = new WebCamTexture(deviceName, width.Value, height.Value, framesPerSecond.Value);

var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);

if (go == null)
{
return;
}

      go.renderer.material.mainTexture = wct;

       
 
      }
}

Play camera:

Code: [Select]
import System;
import HutongGames.PlayMaker;

@ActionCategory("Webcamera")
@Tooltip("Streams video from the installed Webcamera(s)")
public class playWebcamera extends FsmStateAction{

   


@UIHint(UIHint.Variable)
public var gameobject : FsmOwnerDefault;

@UIHint(UIHint. Variable)
public var cameraId : FsmInt;



public function Reset(){
gameobject = null;
cameraId = null;

}

public function OnEnter(){

var dev : WebCamDevice[];

var deviceName : String;

var wct : WebCamTexture;

var resultString : String;

dev = WebCamTexture.devices;

       deviceName = dev[cameraId.Value].name;

       wct =  WebCamTexture(deviceName);

var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);

if (go == null)
{
return;
}

      go.renderer.material.mainTexture = wct;

       wct.Play();
 
      }
}

Stop camera:

Code: [Select]
import System;
import HutongGames.PlayMaker;

@ActionCategory("Webcamera")
@Tooltip("Stops video from the installed Webcamera(s)")
public class stopWebcamera extends FsmStateAction{

   


@UIHint(UIHint.Variable)
public var gameobject : FsmOwnerDefault;


public var cameraId : FsmInt;



public function Reset(){
gameobject = null;
cameraId = null;

}

public function OnEnter(){

var dev : WebCamDevice[];

var deviceName : String;

var wct : WebCamTexture;

var resultString : String;

dev = WebCamTexture.devices;

       deviceName = dev[cameraId.Value].name;

       wct =  WebCamTexture(deviceName);

var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);

if (go == null)
{
LogError("Missing gameobject");
return;
}



     
       
            wct.Stop();

       

The pause code is the same as the stop camera action except that the last line is wct.Pause():

thanks for your help

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14483
  • Official Playmaker Support
    • View Profile
Re: Help with webcam custom action
« Reply #1 on: May 22, 2012, 10:41:14 PM »
hi Cel :)

 Ok, the problem with this is that WebCamTexture.devices return the list of "available" web cam, so the one you use will not be listed.

Since Unity can not store Objects ( it can only store components, gameObjects, aside the usual set: float, string, etc), the solution is to build a component proxy that store your webCamTexture so that when you want to pause it you call that component instead, same with stop.

 The other solution would be to use a singleton that would be accessed just like WebCamTexture and would return the list of "busy" devices, and then pause and stop them

Does that make sense?

 bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14483
  • Official Playmaker Support
    • View Profile
Re: Help with webcam custom action
« Reply #2 on: June 18, 2012, 03:01:51 AM »
Hi,

 Have you been able to make some progress on this? else, I'll make it.

 bye,

 Jean

cel

  • Junior Playmaker
  • **
  • Posts: 96
    • View Profile
Re: Help with webcam custom action
« Reply #3 on: June 18, 2012, 09:25:52 AM »
please do!!!  ;D I'm pretty much stuck...

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14483
  • Official Playmaker Support
    • View Profile
Re: Help with webcam custom action
« Reply #4 on: June 20, 2012, 12:25:22 AM »
Hi,

 ok, I found an easier way to get around all this. I simply store the webcamtextue in an FsmObject and you use that reference then to stop it.

 I have attached the play and stop actions, have a go. you need to create a FsmObject of type "webCamTexture" and reference that in both actions ( the play actions will set it, the stop actions will get it)

 I have also cleaned up a bit the actions and implement two event, success and failure, for you to have better feedback in your fsm states and transitions.

Tell me how it goes. If you need a working example let me know.

Bye,

 Jean


DevBoyLars

  • Playmaker Newbie
  • *
  • Posts: 27
    • View Profile
Re: Help with webcam custom action
« Reply #5 on: September 29, 2012, 08:32:58 AM »
Hi guys,

For the past 2 weeks i've been trying to make a custom action for the webcam using the webcamera texture feature of unity. with help from jean we were able to get it to work within playmaker, I decided to have a set of actions for this, setup camera, get propereties, play. pause and stop...all is well we can setup the camera, play it... but stopping it doesn't work.... sometimes it does stop but then I can't get it to play again...

To try this, add an object to your scene, give it a material, attach the setupcamera (available fromthe camera section, didn't have time to change the category) cameraid should be 0 for your default camera and then use the play action in the webcamera category... if your image is mirrored scale your object to negative values (ex. if your object is 10x10x10 scale it to -10x-10x-10)

Have a look at the code below... it's a mess I know... I'm just trying to learn at the moment... and if you can fix it or come up with a solution please do....

setupcamera:

Code: [Select]
import System;
import HutongGames.PlayMaker;

@ActionCategory(ActionCategory.Camera)
@Tooltip("Sets up the Webcamera")
public class setupWebcamera extends FsmStateAction{

@UIHint(UIHint.Variable)
public var gameobject : FsmOwnerDefault;

@UIHint(UIHint. Variable)
public var cameraId : FsmInt;

@UIHint(UIHint. Variable)
public var width : FsmInt;

@UIHint(UIHint. Variable)
public var height : FsmInt;

@UIHint(UIHint. Variable)
public var framesPerSecond : FsmInt;




public function Reset(){
gameobject = null;
cameraId = null;
width = null;
height = null;
framesPerSecond = null;
}

public function OnEnter(){

var dev : WebCamDevice[];

var deviceName : String;

var wct : WebCamTexture;

var resultString : String;

dev = WebCamTexture.devices;

       deviceName = dev[cameraId.Value].name;

       wct = new WebCamTexture(deviceName, width.Value, height.Value, framesPerSecond.Value);

var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);

if (go == null)
{
return;
}

      go.renderer.material.mainTexture = wct;

       
 
      }
}

Play camera:

Code: [Select]
import System;
import HutongGames.PlayMaker;

@ActionCategory("Webcamera")
@Tooltip("Streams video from the installed Webcamera(s)")
public class playWebcamera extends FsmStateAction{

   


@UIHint(UIHint.Variable)
public var gameobject : FsmOwnerDefault;

@UIHint(UIHint. Variable)
public var cameraId : FsmInt;



public function Reset(){
gameobject = null;
cameraId = null;

}

public function OnEnter(){

var dev : WebCamDevice[];

var deviceName : String;

var wct : WebCamTexture;

var resultString : String;

dev = WebCamTexture.devices;

       deviceName = dev[cameraId.Value].name;

       wct =  WebCamTexture(deviceName);

var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);

if (go == null)
{
return;
}

      go.renderer.material.mainTexture = wct;

       wct.Play();
 
      }
}

Stop camera:

Code: [Select]
import System;
import HutongGames.PlayMaker;

@ActionCategory("Webcamera")
@Tooltip("Stops video from the installed Webcamera(s)")
public class stopWebcamera extends FsmStateAction{

   


@UIHint(UIHint.Variable)
public var gameobject : FsmOwnerDefault;


public var cameraId : FsmInt;



public function Reset(){
gameobject = null;
cameraId = null;

}

public function OnEnter(){

var dev : WebCamDevice[];

var deviceName : String;

var wct : WebCamTexture;

var resultString : String;

dev = WebCamTexture.devices;

       deviceName = dev[cameraId.Value].name;

       wct =  WebCamTexture(deviceName);

var  go : GameObject = Fsm.GetOwnerDefaultTarget(gameobject);

if (go == null)
{
LogError("Missing gameobject");
return;
}



     
       
            wct.Stop();

       

The pause code is the same as the stop camera action except that the last line is wct.Pause():

thanks for your help

Looking nice, but how can I implement these to my Unity-Project as PM-Actions?