Toggle xRay for selected object in Maya with script - maya

I'm new to mel script. I know that I can toggle manually xray with the code.
displaySurface -xRay true; //Xray on
displaySurface -xRay false; //Xray off
But I want it to toggle automatically, like
if(xRay on)
set xRay off
else
set xRay on
I know that I can check xRay on or off with the command
displaySurface -query -xRay;
But I just can't put this command into if block. I tried many things like the code below, but nothing works.
if(`displaySurface -query -xRay` == 1) // Error: line 1: Cannot use data of type int[] in a scalar operation. //
print("To be or not to be");

Looks like displaySurface -query -xRay is returning an array. This worked for me:
int $y[] = `displaySurface -query -xRay`;
if( $y[0] == 1)
print("To be or not to be");

The brackets after int in the error Cannot use data of type int[] indicates that the function returns an integer array. So you need to take the first element [0] of the array.
$xRayOnArray = `displaySurface -q -xRay`;
if ($xRayOnArray[0] == 0) {
print("X-Ray is enabled");
} else {
print("X-Ray is disabled");
}
Why it returns an array is a puzzle. It isn't documented and the function can only query a single object at a time.

Related

Google Script is returning the index of the array but I need the value

I have a google spreadsheet that gets data logged to it via a google form.
When the form is logged each time, it triggers a script that gets values from a certain section using:
var tabNumsVal = sheet.getSheetValues(lastRow, tabOneCol.getColumn(), 1, 6)[0];
When I check the array, I can see that the array has the values such as:
0: 12
1: 24
2: 26W
3: 0
4: 0
5: 0
However when I use the following command, it puts the index numbers (0 to 5) into the array instead of the values in the array.
var tabNumsFinal = [];
for (var tabard in tabNumsVal) {
if (tabard !== "") {
tabNumsFinal.push(tabard);
}
}
It used to work but I have had to upgrade my code to Google Script v8 and it seems that this has broken the code.
I had to alter the 'for each' code block to a 'for' code block and it seems this is handling the values differently.
I am quite sure this is simple for many people but I really only touch Google Script 1 time each year. I have tried using Logger.log(tabard) to output the data to the execution log, but it just traverses the code and doesn't output anything. I figured this might be because of the !== "" operator, so I placed it above the if statement but still inside the for statement and it still outputs nothing.
I tried using Logger.log(tabNumsVal) and Logger.log(tabNumsFinal) and again it output nothing.
To recap:
The data from the form is returning correctly into the columns of the spreadsheet, hence it is showing inside the array properly. It's just that the index numbers are being output instead of the values from the array.
Since you're using for in loop, tabard is the index here.
var tabNumsFinal = [];
for (var i in tabNumsVal) {
let val = tabNumsVal[i];
if (val !== "") {
tabNumsFinal.push(val);
}
}
For in loop

in_array() expects parameter 2 to be array, object given

i know this is similar with other question, but.
how if we get the data array from db and try to compare those data ?
$x = Video::where('kursus_id', $data)->pluck('slug');
$z = Video::where('kelas_id', $data_id_kelas)
->where('mapel_id', $data_id_mapel)
->pluck('slug');
foreach ($z as $value) {
if (in_array($value, $x)) {
} else {
echo $value.'<br>';
}
}
this throw me an error like in_array() expects parameter 2 to be array, object given.
but when i try to change $x and $z with
$x=["2"]
$z=["1","2","3"]
Thats work. and output is 1 & 3
i think when i use db and give it pluck, this will became the same output when i use regular array like ["1","2","3"].
please correct me if my opinion goes wrong. cz im in study. Thanks before mates
in_array() it 2nd param req. array and you are giving object that's why this error
to fix this you need to use toArray() function in laravel
$x = Video::where('kursus_id', $data)->pluck('slug')->toArray();
then you can use in_array($value, $x)

Select all joints of a character with Maya MEL

I wrote an MEL script in Maya. So I want to select a joint, then run the MEL script and it selects that joint and all its children. I very new to MEL, So with running the code, it throws a bunch of errors. Can you help me reduce error or better to get rid of all of them?
The Script:
string $joints[];
string $current[] = 'ls -selection';
proc selectJoints (){
if ('searchJoints($joints)' == 0){
$joints['size($joints)'] = $current[0];
pickWalk -d down;
$current[0] = 'ls -sl';
selectJoints();
}
else{
pickWalk -d right;
$current[0] = 'ls -sl';
if('searchJoints($joints)' == 0){
selectJoints();
}
else{
pickWalk -d up;
$current[0] = 'ls -sl';
if($current[0] == $joints[0]){
selectJoints();
}
}
}
return;
}
select ($Joints);
proc int searchJoints (string $jns[]){
int $result = 0;
for ($joint in $jns[]){
if ($current[0] == $joint){
return 1;
}
}
return 0;
}
So, I know your question is about MEL, and I am sorry for not being able to help you with that BUT i think i can help you with python and pymel.
Try this code in a Python tab in the script editor:
import pymel.core as pm
# get selected joint
selectedJoint = pm.selected()[0]
#get all children from the selected joint and puts it in a list
joints = selectedJoint.listRelatives(allDescendents = True)
#adds first selected joint to same list
joints.append(selectedJoint)
#clears selection
pm.select(clear = True)
#loop thru list of joints
for item in joints:
#toggle selection on selected joint and all its descendents
pm.select(item, tgl = True)
I am not sure why to use MEL, I started directly with pymel and it seems more powerful. Could you tell me why MEL?...I think I might be missing out on something. Anyway, I think this short code does the trick. Good luck! Be aware that there are no fail safes there. so, make sure you select ONE joint to run before running the script.
You can simply do:
select -hi;

Wordpress - get_results() - How to know if failed or empty?

I use the Wordpress function $wpdb->get_results()
https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
It says:
"If no matching rows are found, or if there is a database error, the return value will be an empty array."
Then how can I know if the query failed OR if it's empty?
Use
$results=$wpdb->get_results($yoursql);
if (count($results)> 0){
//do here
}
But if you want to know if query failed
$wpdb -> show_errors ();
$wpdb -> get_results ($wpdb -> prepare($sql));
$wpdb -> print_error ();
Bit late to the party here but I'm just looking for the same thing. I've had a browse through the wp-db.php code on version 4.4.2.
On line 1422, inside the method flush() there's a bit of code which resets the last_error property:
$this->last_error = '';
This flush() method is called in the query() method on line 1693:
$this->flush();
The get_results() method calls query() on line 2322:
if ( $query ) {
$this->query( $query );
} else {
return null;
}
With this we can be pretty sure that more or less every time get_results() (Or get_row() too for that matter) is called, query() and flush() are both called, which ensures that last_error is set to the empty string before the query is executed.
So assuming the query runs (If it doesn't, null is returned - if the query is empty for example), last_error should contain an error message if the query was to fail for some reason.
Since last_error is flush()ed/reset each time, it should only contain an error for the last query that was run, rather than the last error for any query that had been run previously. With this in mind it should be safe to rely on last_error to determine whether something went wrong with the query.
$results = $wpdb->get_results($sql);
if (is_null($results) || !empty($wpdb->last_error)) {
// Query was empty or a database error occurred
} else {
// Query succeeded. $results could be an empty array here
}
Not the most intuitive in my opinion, but it seems to be sufficient.
Personally, I've written my own class around wpdb for my own benefit. This is my getResults() method.
public function getResults($query, $bindings = [])
{
// Prepare the statement (My prepare method inspects $query and just returns it if there's no bindings, otherwise it uses $wpdb->prepare()
$prepared = $this->prepare($query, $bindings);
// Execute the statement
$rows = $this->db->get_results($prepared, ARRAY_A);
// If an array was returned and no errors occurred, return the result set
if (is_array($rows) && empty($this->db->last_error)) {
return $rows;
}
// On failure, return false
return false;
}
Hope this helps.
Wpdb->get_results function from wordpress returns the result if successful otherwise it will return null. There can be many reasons if a query get failed.Refer in-depth article on debugging get_results() returning empty results here
Although you can use functions like wpdb->show_error() to check what was the last error after executing the sql query. sometimes this error returns empty
then try to use wpdb->last_query to check the final query that get formed.

Perl associative arrays and variable assignment

I'm having trouble understanding how this is supposed to work. I have defined my two hashes outside of the while loop. And the goal is to be able to detect the shell type, and assign the appropriate hash to a new $shell variable. Here is the code I'm current trying..
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use DateTime;
use Term::ANSIColor qw(:constants);
local $Term::ANSIColor::AUTORESET = 1;
my $dt = DateTime->now; # Stores current date and time as datetime object
my %multicity = (
url => "example.com",
ftpuser => "user",
ftppass => "pass",
remote_dir => "/httpdocs/",
dbname => "database"
);
my %singlecity = (
url => "example2.com",
ftpuser => "user",
ftppass => "pass",
remote_dir => "/httpdocs/",
dbname => "database"
);
open (MYFILE, 'sites.txt');
LINE: while (<MYFILE>) {
next LINE if /^#/;
my ($shelltype, $siteurl, $ftpuser, $ftppass, $dbname) = split /\|/;
# 1|example.com|user|pass|databasename - This should be a singlecity shell type.
if ($shelltype == 1) { my $shell = \%multicity; } else { my $shell = \%singlecity; };
print "Shelltype: $shelltype\n";
print "Should be $shell{url}\n";
}
close (MYFILE);
I've tried many different things with no avail so I'm finally resorting to the pros here at stackoverflow to get some direction!
Any help is very much appreciated. Thank you.
Your my $shell is lexical inside of the if block. Move it outside the if or it is not available there. Adding some indentation helps to spot that.
my $shell;
if ($shelltype == 1) {
$shell = \%multicity;
} else {
$shell = \%singlecity;
};
After that, you will get a warning saying %shell is undefined. That's because you are using $shell{url} but you defined $shell as a hash ref, so you need $shell->{url} or $$shell{url} in your print.
Two issues:
Your my $shell variable is lexically scoped to the if statement. It won't be visible outside.
The $shell variable is a hashref. So you have to access elements using $shell->{url}. Note the arrow.
So the following should work:
my $shell;
if ($shelltype == 1) { $shell = \%multicity; } else { $shell = \%singlecity; };
print "Shelltype: $shelltype\n";
print "Should be $shell->{url}\n";
This line:
print "Should be $shell{url}\n";
should be:
print "Should be $shell->{url}\n";
The reason is that you are assigning a reference to one of your two hashes to $shell, so to access a value in it you need to de-reference (using the -> operator).
In addition, as pointed out by nwellholf, the $shell variable is local to the if statement so you would get a compilation error there.

Resources